CodeForces - 622A A. Infinite Sequence

探讨一个特殊整数序列的生成规律,并提供一种高效算法来确定任意位置上的具体数值。此序列从1开始,逐渐扩展至更大的数列,如1, 1, 2, 1, 2, 3...直至无穷。文章详细解析了如何通过数学方法快速定位到序列中任一位置的数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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).

Examples
input
3
output
2
input
5
output
2
input
10
output
4
input
55
output
10
input
56
output
1
解题思路


1,1,2,1,2,3,1,2,3,4,1,2,3,4,5…观察可得出规律1,2,3,4,5。。。第n项就是前面的和,所以用n减去sum,就是对应的数。由于数据太大,所以不能直接用for()换求和,求和公式s=(首项+末项)*项数/2;

AC代码:

#include<cstdio>
typedef long long ll;
int main(){
	ll n, sum = 0, mod;
	scanf("%lld", &n);
	for(ll i = 1; ; i++){
		sum = 0; 
		sum = ((1 + i) * i) / 2;
		if(sum > n) {
			sum = ((1 + i-1) * (i-1)) / 2;
			mod =  n - sum;
			if(mod == 0){
				printf("%lld", i-1);
				break;
			}
			else{
				printf("%lld", mod);
				break;
			}
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值