Infinite Sequence

19 篇文章 0 订阅
15 篇文章 0 订阅

Infinite Sequence
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 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 Input

Input
3
Output
2
Input
5
Output
2
Input
10
Output
4
Input
55
Output
10
Input
56
Output
1
1+2……+m=m*(m+1)/2<n来确定m的大小。 n ( 1 ≤ n ≤ 1014) ,通过for循环来查找m的值太费时,太麻烦。经过化简m*(m+1)<2*n来确定m,m近似等于k=sqrt(2*n)。可以看做是近似直接定位,减少时间。在取k值时要仔细分析.考虑一些特殊情况。如当n=2时,k=2,由于第二个位置是1,所以m=k-1=1,当n=3时,m=k-1=1,第3个位置应是2

My solution:

/*2016.3.12*/

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
	long long n,m,ans,k,k1,k2,k3;
	int i,j;
	while(scanf("%I64d",&n)==1)
	{
		k=sqrt(2*n);
		k=k-1;
		k1=(k+1)*k/2;
		k2=(k+1)*(k+2)/2;
		k3=(k+2)*(k+3)/2;
		if(n>k1&&n<=k2)
		m=n-k1;
		else
		if(n>k2&&n<=k3)
		m=n-k2;
		printf("%I64d\n",m);		
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值