首页 > 代码库 > Timus 1120. Sum of Sequential Numbers 数学题

Timus 1120. Sum of Sequential Numbers 数学题

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 number N, 1 ≤ N ≤ 109.

Output

Your program is to output two positive integers A and P separated 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 of P.

Sample

inputoutput
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;
			}
		}
	}
}