zoukankan      html  css  js  c++  java
  • codeforces 622A 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

     题意:这么个数列,问第n个数是多少;

     思路:结合n*(n+1)/2这个式子二分;

     AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    long long bisearch(long long x)
    {
    long long le=0,ri=1e8,mid;
    while(le<=ri)
    {
    mid=(le+ri)/2;
    if(mid*(mid+1)/2>=x)ri=mid-1;
    else le=mid+1;
    }
    return le-1;
    }
    int main()
    {
    long long n;
    cin>>n;
    long long ans=bisearch(n);
    cout<<n-ans*(ans+1)/2<<" ";
    return 0;
    }

  • 相关阅读:
    基于vue的购物车清单
    圣杯布局和双飞翼布局
    正则限制input负数输入
    vue.js devtools图标不亮
    将二维数组转换成一维数组(基于reduce)
    基于PROMISE解决回调地狱问题
    封装AJAX库(参考JQ)
    for in和for of的区别
    抢购倒计时的实现
    git clone --depth=1 后获取其他分支
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5186390.html
Copyright © 2011-2022 走看看