Timus 1120. Sum of Sequential Numbers 数学题

本文介绍了一种求解连续整数之和的算法实现,通过分解因子的方法来寻找最大可能长度的整数序列,使得这些整数的和等于给定的数值N。该算法使用了数学中的等差数列求和公式,并结合循环结构进行求解。

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

There is no involute formulation concerning factitiously activity of SKB Kontur in this problem. Moreover, there is no formulation at all.

Input

There is the only numberN, 1 ≤N≤ 109.

Output

Your program is to output two positive integersAandPseparated with a space such that:
  1. N=A+ (A+ 1) + … + (A+P− 1).
  2. You are to choose a pair with the maximal possible value ofP.

Sample

input output
14
2 4

这里使用的数学思维:分解两个因子的思想,利用程序特点循环求解

发现自己的数学水平还是不行,要继续修补。-- 这些数学思想其实早就学过的,不过使用的不多,故此容易忘记。

N = A + (A + 1) + … + (A + P − 1) 求出公式:N=(A+A+p-1)*p/2;

2*N=P*(2A+P-1) 然后是分解因子得到:
x=P; y=(2A+P-1)

2*N=x*y

然后循环x,即p,求得符合条件的A就结束循环。

自己推导下,就明白啦。

void SumofSequentialNumbers1120()
{
	long long S = 0;
	cin>>S;
	S <<= 1;
	for (int p = (int)sqrtl(S); p >= 0 ; p--)
	{
		if (S % p == 0)
		{
			int y = int(S / p);
			int a = y + 1 - p;
			if (a % 2 == 0)
			{
				cout<<a/2<<' '<<p;
				break;
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值