codeforces_622A. Infinite Sequence

来源:互联网 发布:ip更换软件win10 编辑:程序博客网 时间:2024/05/22 19:16
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).

Examples
input
3
output
2
input
5
output
2
input
10
output
4
input
55
output
10
input
56
output
1
水题来着,用sqrt乱搞都过了(也类似于二分),看网上有两种思路,纯数学和二分,都学习一下吧。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>using namespace std;//sqrt乱搞int main(){    long long n,ans;    scanf("%lld",&n);    long long s=(long long)sqrt(2*n);    long long sum=s*(s+1)/2;    if(sum>n)    {        ans=s-(sum-n);    }    else if(sum==n)    {        ans=s;    }    else if(sum<n)    {        ans=n-sum;    }    printf("%lld\n",ans);    return 0;}

#include<cstdio>  #include<cstring>  #include<iostream>  #include<algorithm>  using namespace std;  //数学int main()  {      long long n;      while(cin>>n)      {          long long temp;          for(long long i=1;n>0;i++)          {              temp=n;              n-=i;          }          cout<<temp<<endl;      }      return 0;  }  
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;//二分long long solve(long long n){    long long l=0,r=10000000,mid;    while(l<=r)    {        mid=(l+r)/2;        if(mid*(mid+1)/2>=n)r=mid-1;        else l=mid+1;    }    return l-1;}int main(){    long long n;    scanf("%lld",&n);    long long ans=solve(n);    printf("%lld\n",n-(ans)*(ans+1)/2);    return 0;}





0 0
原创粉丝点击