Educational Codeforces Round 7 A
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.
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.
Print the element in the n-th position of the sequence (the elements are numerated from one).
3
2
5
2
10
4
55
10
56
1
没什么好说的,先是一个等差数列求和,然后判断一下就行,如果等于一个和,说明一个循环结束,不是就减去
#include<stdio.h> //#include<bits/stdc++.h> #include<string.h> #include<iostream> #include<math.h> #include<sstream> #include<set> #include<queue> #include<map> #include<vector> #include<algorithm> #include<limits.h> #define inf 0x3fffffff #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define ULL unsigned long long using namespace std; int t; int n,m; int sum,ans,flag; int a,b,c,d; int main() { LL n; LL b; LL ans; LL i=0; LL sum=0; cin>>n; while(sum<=n) { i++; sum=1*i+(i)*(i-1)/2; // i++; } // cout<<i-1<<endl; ans=i-1; // cout<<1*ans+(ans)*(ans-1)/2<<endl; b=1*ans+(ans)*(ans-1)/2; if(n==1*ans+(ans)*(ans-1)/2) { cout<<ans<<endl; } else { cout<<n-b<<endl; } return 0; }