您的位置:首页 > 产品设计 > UI/UE

Educational Codeforces Round 7-A. Infinite Sequence(模拟)

2016-02-11 20:49 363 查看
A. Infinite Sequence

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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 test(s)

input
3


output
2


input
5


output
2


input
10


output
4


input
55


output
10


input
56


output
1


思路:
   一开始打算用n*n+1/2来做,结果没想到竟然爆long long了,原来直接减就行了。一开始认为这个方法会超时才用公式,结果竟然不超时,看来又想错了时间了。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int T=10000100;
#define inf 0x3f3f3f3fL
#define mod 1000000000
typedef long long ll;
typedef unsigned long long ULL;

int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
ll n,m,i,j=0,k;
while(~scanf("%I64d",&n))
{
for(i=1;i<n;++i)n-=i;
printf("%I64d\n",n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 模拟