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. Linux的安装教程

    CentOS 6 + VMWare: 资源:https://pan.baidu.com/s/1AA4gaMpaoVaMor-tRU-n7A  提取码:6e8r    ( 内附 VMWare 14 激活 ...

  2. php 导出Excel 不用安装插件、开启配置

    function export_csv($filename, $data) { header("Content-type:text/csv"); header("Cont ...

  3. 关于单片机编程里面调用sprintf死机的解决方法及原因分析

    好久之前的做的笔记,这里贴出. char String[100];//直接用数组代替指针即可解决 下面代代码下载至单片机中,发现会出现单片机死机问题 #include "stdio.h&qu ...

  4. WEB 移动端 CSS3动画性能 优化

    很多时候,我们在开发移动端的时候要使自己的网页兼容不同的机型,很多时候会采用CSS3动画,但是很多时候在安卓机下,动画明显会出现卡顿,很难看,那么这里我介绍几个CSS 属性进行硬件加速那么就会得到明显 ...

  5. noip模拟赛 可耻

    题目描述 给定一个长度为偶数的排列 p,你每次可以选取 p 排列中相邻的两个元素,假如分别是 x 和 y,那 么把 x 和 y 加入一个序列 q 的末尾,并将 x 和 y 从排列 p 中删除.重复上述 ...

  6. [TJOI2014] [Bzoj3996] 线性代数 [网络流,最小割]

    由原式,可以推出D=Σ(i=1,n,Σ(j=1,n,A[i]*A[j]*B[i][j]))-Σ(i=1,n,A[i]*C[i]) $D=\sum\limits_{i=1}^{n}\sum\limits ...

  7. [bzoj4025]二分图_LCT

    二分图 bzoj-4025 题目大意:给定一个n个节点的图,m条边,每条边有一个产生时间和一个删除时间,询问所有时间点是否是连通图. 注释:$1\le n\le 10^5$,$1\le m\le 2\ ...

  8. I NEED A OFFER! (hdu 1203 01背包)

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. java 基础编程day1

    public class TextCharType{ public static void(Sting[] args){ char c1 = 'a'; char c2 = '了'; System.ou ...

  10. MSP430WARE++的使用2:RSP1 driver的调用方法

        MSP430WARE是一套基于C++语言的开源的MSP430层次化软件架构,支持多种外设.本文将介绍雷达測速芯片RSP1驱动程序的调用方法.     1.硬件原理图      採用下图所看到的 ...