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).
Examples
Input
3
Output
2
Input
5
Output
2
Input
10
Output
4
Input
55
Output
10
Input
56
Output
1
题目思路:由于本题的项数是一个等差数列,所以我们写这个题只需要把项数和求出来和n比较就可以了。只要项数和大于等于n,则n所在位置的数字为n-k*(k-1)/2;注意写此题不能用for循环来写,否则会超限。
ac代码:
#include<stdio.h>
int main()
{
long long n,i;
scanf("%lld",&n);
i=1;
while(i)
{
if(i*(i+1)/2>=n)
break;
i++;
}
printf("%lld\n",n-i*(i-1)/2);
return 0;
}