Infinite Sequence
来源:互联网 发布:ubuntu好用的输入法 编辑:程序博客网 时间:2024/05/22 16:54
Description
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first the number1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not digits. For example number10 first appears in the sequence in position 55 (the elements are numerated from one).
Find the number on the n-th position of the sequence.
Input
The only line contains integer n (1 ≤ n ≤ 1014) — the position of the number to find.
Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use thelong long integer type and in Java you can use long integer type.
Output
Print the element in the n-th position of the sequence (the elements are numerated from one).
Sample Input
3
2
5
2
10
4
55
10
56
1
My solution:
/*2016.3.12*/
#include<stdio.h>#include<math.h>#include<algorithm>using namespace std;int main(){long long n,m,ans,k,k1,k2,k3;int i,j;while(scanf("%I64d",&n)==1){k=sqrt(2*n);k=k-1;k1=(k+1)*k/2;k2=(k+1)*(k+2)/2;k3=(k+2)*(k+3)/2;if(n>k1&&n<=k2)m=n-k1;elseif(n>k2&&n<=k3)m=n-k2;printf("%I64d\n",m);}return 0;}
- Infinite Sequence
- Infinite Sequence
- Infinite Sequence
- an infinite sequence
- codeforces_622A. Infinite Sequence
- A. Infinite Sequence
- A. Infinite Sequence
- CF Infinite Sequence
- [arc071f]Infinite Sequence
- Codeforces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A-Infinite Sequence
- CodeForces 622A Infinite Sequence
- codeforces 675A Infinite Sequence
- CodeForces 675 A Infinite Sequence
- 【CodeForces】675A - Infinite Sequence
- 抽象类和接口
- Codeforces 29C Mail Stamps 【离散化 + DFS】
- 10.C进阶之变量和常量
- Java 信号量 Semaphore 介绍
- 合并排序
- Infinite Sequence
- Spring使用util Schema
- POJ 3126 Prime Path 筛法+最短路
- Bellmanford 最短路(1)
- iOS UIAppearance使用详解
- python 实现用ISBN从豆瓣获取图书信息
- Codeforces 14D Two Paths 【树的直径】
- 《Linux内核设计与实现》与《Linux内核源代码情景分析》读书笔记
- 支持向量机分类入门实用指南