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. 创建pod索引库(Specs)

    专门用来存放xxx.podspec 的索引文件的库就叫做索引库.我们需要将这些索引文件上传到远程索引库才能保证其他的人能够拿来用. 创建一个远程索引库和本地索引库对应起来,步骤如下: 1.登录开源中国 ...

  2. 踩过的坑:__file__、__package__和__name__

    不说废话,直接上示例结构图 Path.py内容如下: import os path1 = os.path.dirname(os.path.abspath(__file__)) path2 = os.p ...

  3. Jenkins+Ant+Jmeter自动化集成测试实例

    通过学习Jmeter自动化测试,接触到了Ant命令和其构建文件build.xml文件的编写,与此同时,通过将测试项目集成在jenkins上,进一步学习了jenkins的一些环境配置知识.以下是自己的初 ...

  4. Ajax基本写法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. -------------Django-----URLS路由

    一.相约Django. 1.Django的特点:Django定义了服务分布.路由映射.模板编程.数据处理的一套完整的功能. (1)集成数据访问组件:Django的model层自带数据库ORM组件. ( ...

  6. PAT 1104 Sum of Number Segments

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  7. Beetl学习总结(2)——基本用法

    2.1. 安装 如果使用maven,使用如下坐标 <dependency> <groupId>com.ibeetl</groupId> <artifactId ...

  8. springCloud学习-断路器(Hystrix)

    1.问题分析 在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高 ...

  9. nyoj_33_蛇形填数_201308221636

    蛇形填数时间限制:3000 ms  |  内存限制:65535 KB 难度:3描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 13 28 ...

  10. Linux下统计某个目录的文件个数(转)

    1.统计某文件夹下文件个数,不包括子文件夹 比如:统计/home下.jpeg文件的个数 ls -l "/home" | grep ".jpeg" | wc -l ...