Educational Codeforces Round 7--A. Infinite Sequence

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

找规律的题目:

1

2 3

4 5 6 

7 8 9 10

11 12 13 14 15

。。。。

输入 一个n,求出第n个位置的数。

思路:

先求出第n个数所在的行,减去第一个数就行了,第一个数显然是个累加法的数列求法.

代码如下:

#include<bits/stdc++.h>
#define mem(x) memset(x,0,sizeof(x));
#define mem1(x) memset(x,-1,sizeof(x));
using namespace std;
const double eps = 1e-8;
const int INF = 1e8;
const int maxn = 10000 + 10;
const int maxt = 100 + 10;
typedef long long ll;
typedef unsigned long long llu;
int main()
{
    ll n,k;
    while(cin >> k){
        double det = sqrt(1 - 4*(2-2*k));
        ll n = (1+det)/2;
        cout << k-(n-1)*n/2 << endl;
    }
    return 0;
}


你可能感兴趣的:(数学,C语言,codeforces)