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

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

Timus 1120. Sum of Sequential Numbers 数学题,码迷,mamicode.com

时间: 2024-12-28 17:01:25

Timus 1120. Sum of Sequential Numbers 数学题的相关文章

zoj 2358,poj 1775 Sum of Factorials(数学题)

题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n能否表示成几个数的阶乘之和 //这里有一个重要结论:n!>(0!+1!+……+(n-1)!), //证明很容易,当i<=n-1时,i!<=(n-1)!,故(0!+1!+……+(n-1)!)<=n*(n-1)!=n!. // 由于题目规定n<=1000000,而10!=362880

【Leetcode_easy】985. Sum of Even Numbers After Queries

problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) { vector<int> res; int sum = 0; for(auto a:A) if(a%2==0) sum +=a; f

1-Two sum 2-Add two numbers

1.Two sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please

周刷题第一期总结(two sum and two numbers)

由于深深的知道自己是事件驱动型的人,一直想补强自己的薄弱环节算法,却完全不知道从哪里入手.所以只能采用最笨的办法,刷题.从刷题中遇到问题就解决问题,最后可能多多少少也能提高一下自己的渣算法吧. 暂时的目标是一周最少两道,可能会多做多想,工作再忙也会完成这个最低目标. Two sum: Given an array of integers, return indices of the two numbers such that they add up to a specific target. Y

HDU 1492 The number of divisors(约数) about Humble Numbers(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1492 Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the firs

11.总数字作为数字Sum Strings as Numbers

总的来说这是一个将字符串转化为数字进行计算再转化为字符串的计算,醉生与死: Given the string representations of two integers, return the string representation of the sum of those integers. For example: sumStrings('1','2') // => '3' A string representation of an integer will contain no ch

LC 985. Sum of Even Numbers After Queries

We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum of the even values of A. (Here, the given index = q

[Solution] 985. Sum of Even Numbers After Queries

Difficulty: Easy Question We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A. (

【leetcode】985. Sum of Even Numbers After Queries

题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum of the even values of A. (Here, the given ind