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代码:
 #include<bits/stdc++.h>
using namespace std;
int main(){
long long n,k,m;
cin>>n;
k=sqrt(2.0*n+0.25)-0.5;
m=k*(k+)/;
if(n>m)cout<<n-m<<endl;
else cout<<k+n-m<<endl;
return ;
}

A - Infinite Sequence的更多相关文章

  1. CodeForces 622 A.Infinite Sequence

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

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

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

  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. 【arc071f】Infinite Sequence(动态规划)

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

  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. codeforces 622A A. Infinite Sequence (二分)

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

  8. Codeforces Round #353 (Div. 2) A. Infinite Sequence

    Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its fi ...

  9. 【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)/ ...

随机推荐

  1. javaWEB中web.xml配置文件相关

    1.常用节点的执行顺序: context-param -> listenter -> filter -> servlet -> interceptor,其中有多个filter的 ...

  2. 2 Button

    // <summary> /// 设置透明按钮样式 /// </summary> private void SetBtnStyle(Button btn) { btn.Flat ...

  3. 转:Windows Phone 7 设计简介

    英文原文:smashingmagazine 导读:Windows Phone 7 引进了一个全新的内容管理和用户界面,命名为Metro的设计语言和理论.微软这次所看准的市场和用户群也与之前的老一代 W ...

  4. 【模板】RMQ问题 ST表

    洛谷3865 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; ; ...

  5. GeoTrust 企业(OV)型 增强版(EV) 多域名(SAN/UC) SSL证书

    GeoTrust 企业(OV)型 增强版(EV) 多域名(SAN/UC)SSL证书(GeoTrust True BusinessID With EV Multi-Domain(SAN/UC) SSL ...

  6. 【codeforces 767A】Snacktower

    [题目链接]:http://codeforces.com/contest/767/problem/A [题意] 每天掉一个盘子下来;盘子有大小从1..n依次增大n个盘子; 然后让你叠盘子; 最底层为n ...

  7. jenkins简单持续构建

    一.安装jenkins 二.将需要持续构建的java project打包成jar文件 1.选择导出需要运行的main方法所在java类

  8. 在ANGULAR的SERVICE中,哪种才是最基本的实现?(Provider)

    今天刚好看到这一节. 节选一下,稍后,实操完成之后,会补上所有代码 Sometimes, it might be interesting to create configurable services ...

  9. nyoj_218_Dinner_201312021434

    Dinner 时间限制:100 ms  |           内存限制:65535 KB 难度:1   描述 Little A is one member of ACM team. He had j ...

  10. 利用Calendar类测试电脑运行速度

    今天学习了很多新知识! 这里使用了Calender类来获取系统时间,并计算循环1w次的时间,判断电脑处理时间. import java.util.Calendar; public class Arra ...