CodeForces 622A--F - Infinite Sequence

来源:互联网 发布:矩阵连乘 动态规划 编辑:程序博客网 时间:2024/04/29 01:47
F - Infinite Sequence
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 622A

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 number 1 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 number 10 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 the long 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

Input
3
Output
2
Input
5
Output
2
Input
10
Output
4
Input
55
Output
10
Input
56
Output
1

题目意思:给你一个序列,观察这个序列的特点。然后写出序列中第n个元素是什么。

AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int MAX=1e5+10;const long long MAXN=1e14+10;__int64  n;__int64  seq[MAX];int main(){__int64 i=1;while(scanf("%I64d",&n)!=EOF){__int64  ans;__int64 temp=0,cnt=1;while(temp+cnt<n){temp+=cnt;cnt++;}ans=n-temp;printf("%I64d\n",ans);}return 0;}



0 0
原创粉丝点击