zoukankan      html  css  js  c++  java
  • A

    Problem 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 1to 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 longinteger 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
    解题思路:简单推导:假设k表示为第k(k>=1)个序列(1,...,k),那么前k(k>=1)个序列一共有n=(k+1)*k/2个数,则第n个数在第floor(√(2*n+0.25)-0.5)(向下取整)个序列中。因为当n必须刚好为前k个序列数字的总个数时,k才会是那个精确的第k个序列。因此,此时只需判断m=(k+1)*k/2是否大于n。如果n大于m,说明第n个数实际在第k+1个序列里面,则这个数为n-m;否则第n个数就在第k个序列里面,则这个数为k-(m-n)=k+n-m。
    AC代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     long long n,k,m;
     5     cin>>n;
     6     k=sqrt(2.0*n+0.25)-0.5;
     7     m=k*(k+1)/2;
     8     if(n>m)cout<<n-m<<endl;
     9     else cout<<k+n-m<<endl;
    10     return 0;
    11 }
    
    
  • 相关阅读:
    级数求和
    c++版a+b问题的各种无聊做法
    2017 Multi-University Training Contest
    2017 Multi-University Training Contest
    [DP] UVA-1626 Brackets sequence
    Codeforces Round #426 (Div. 2) [A、B、C]
    一只弱菜的博客之旅
    关于数据库保存的二进制图片无法在colorbox插件中显示的解决办法
    windows+caffe+vs2013+cuda6.5配置记录
    Ubuntu14.04+cuda6.5+opencv2.4.9+MATLAB2013a+caffe配置记录(五)——安装Caffe
  • 原文地址:https://www.cnblogs.com/acgoto/p/9110405.html
Copyright © 2011-2022 走看看