Sicily 1209 Sequence Sum Possibi
2015-02-13 20:18
344 查看
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
6 = 1 + 2 + 3
9 = 5 + 4 = 2 + 3 + 4
but 8 cannot be so written.
Write a program which will compute how many different ways an input number may be written as a sum of a sequence of at least two consecutive positive integers.
written as a sequence of consecutive positive integers. The second number will be less than 2^31 (so will fit in a 32-bit integer).
Solution
题目大意是给定n,求n可以由等于多少段连续自然数之和。
数学题,我一开始想的是直接穷举起点和终点,但是无论怎么优化都是会超时的
然后就学到枚举项数,反过来算起点是不是整数来判断是不是一个合法的解,注意项数的上限。
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while ( t -- )
{
int i, j, t_case, n, count = 0;
cin >> t_case >> n;
for ( i = 2; i * i - i < 2 * n; i ++ )//项数枚举
{
if ( (n - i * ( i - 1 ) / 2) % i == 0 ) count ++;
}
cout << t_case << ' ' << count << '\n';
}
return 0;
}
//两个限制条件,整除和枚举的不是起点和终点,而是项数,通过项数反算起点是否为整数
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Most positive integers may be written as a sum of a sequence of at least two consecutive positive integers. For instance,6 = 1 + 2 + 3
9 = 5 + 4 = 2 + 3 + 4
but 8 cannot be so written.
Write a program which will compute how many different ways an input number may be written as a sum of a sequence of at least two consecutive positive integers.
Input
The first line of input will contain the number of problem instances N on a line by itself, (1<=N<=1000) . This will be followed by N lines, one for each problem instance. Each problem line will have the problem number, a single space and the number to bewritten as a sequence of consecutive positive integers. The second number will be less than 2^31 (so will fit in a 32-bit integer).
Output
The output for each problem instance will be a single line containing the problem number, a single space and the number of ways the input number can be written as a sequence of consecutive positive integers.Sample Input
7 1 6 2 9 3 8 4 1800 5 987654321 6 987654323 7 987654325
Sample Output
1 1 2 2 3 0 4 8 5 17 6 1 7 23
Solution
题目大意是给定n,求n可以由等于多少段连续自然数之和。
数学题,我一开始想的是直接穷举起点和终点,但是无论怎么优化都是会超时的
然后就学到枚举项数,反过来算起点是不是整数来判断是不是一个合法的解,注意项数的上限。
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while ( t -- )
{
int i, j, t_case, n, count = 0;
cin >> t_case >> n;
for ( i = 2; i * i - i < 2 * n; i ++ )//项数枚举
{
if ( (n - i * ( i - 1 ) / 2) % i == 0 ) count ++;
}
cout << t_case << ' ' << count << '\n';
}
return 0;
}
//两个限制条件,整除和枚举的不是起点和终点,而是项数,通过项数反算起点是否为整数
相关文章推荐
- sicily 1209. Sequence Sum Possibi
- Sicily1209. Sequence Sum Possibi题解
- sicily 1209. Sequence Sum Possibi
- [sicily]1209. Sequence Sum Possibi
- 1209. Sequence Sum Possibi
- 1209. Sequence Sum Possibi
- Sequence Sum Possibi
- hdu3415 Max Sum of Max-K-sub-sequence
- HDU--杭电--3415--Max Sum of Max-K-sub-sequence--暴力或单调队列
- Sicily 4423. Calculate the Sum 解题报告
- HDU 3415 Max Sum of Max-K-sub-sequence
- POJ 2853 Sequence Sum Possibilities
- sicily 1028. Hanoi Tower Sequence
- sicily 1089. Farey Sequence
- hdu3415 Max Sum of Max-K-sub-sequence 单调队列
- HDU 3415——Max Sum of Max-K-sub-sequence
- [单调队列] hdu 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- Sicily.1028. Hanoi Tower Sequence
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)