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<<"\n";
return 0;
}

codeforces 622A A. Infinite Sequence (二分)的更多相关文章

  1. 【CodeForces 622A】Infinite Sequence

    题意 一个序列是, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5....这样排的,求第n个是什么数字. 分析 第n个位置属于1到k,求出k,然后n-i*(i-1)/ ...

  2. CodeForces 622 A.Infinite Sequence

    A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. codeforces 675A A. Infinite Sequence(水题)

    题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...

  4. codeforces 622A Infinite Sequence

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  6. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  7. 【arc071f】Infinite Sequence(动态规划)

    [arc071f]Infinite Sequence(动态规划) 题面 atcoder 洛谷 题解 不难发现如果两个不为\(1\)的数连在一起,那么后面所有数都必须相等. 设\(f[i]\)表示\([ ...

  8. Codeforces 486E LIS of Sequence(线段树+LIS)

    题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...

  9. codeforces 301 E. Infinite Inversions

    题目:   time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

随机推荐

  1. 解决Java工程URL路径中含有中文的情况

    问题: 当Java工程路径中含有中文时,得不到正确的路径 *** 解决: 这其实是编码转换的问题.当我们使用ClassLoader的getResource方法获取路径时,获取到的路径被URLEncod ...

  2. cf 251 B Playing with Permutations 暴力 分类讨论

    题链;http://codeforces.com/problemset/problem/251/B B. Playing with Permutations time limit per test 2 ...

  3. Sitemesh3的使用心得

    项目中用到了sitemesh3,就把使用心得记下来,至于配置之类的,官方网站都有,这里只是写下自己对它的理解,方便再次理解, sitemesh是基于过滤器的原理,拦截到符合配置文件中配置的路径,然后会 ...

  4. vscode 和 atom 全局安装和配置 eslint 像 webstorm 等 ide 一样使用 standard标准 来检查项目

    首先你要安装了 nodejs ,然后在终端命令行输入下面的这堆 npm install eslint eslint-plugin-standard eslint-config-standard esl ...

  5. IoC原理及实现

    什么是IoC  IoC是Inversion of Control的缩写,翻译过来为"控制反转".简单来说,就是将对象的依赖关系交由第三方来控制.在理解这句话之前,我们先来回顾一下I ...

  6. hdu 2036 改革春风吹满地【求多边形面积模板】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2036 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. LookAndFeelInfo

    在读zap源码的时候碰到:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());以下来自百度知道UIManager.s ...

  8. Mybatis sql注入问题

    预编译方式,即PreparedStatement,可以防注入:#{id} <select id="getBlogById" resultType="Blog&quo ...

  9. ABAP下载xml文件

    [转http://www.cnblogs.com/byfhd/archive/2007/08/17/859829.html] ************************************* ...

  10. Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)

    <!-- parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合. parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选. ...