sicily 1209. Sequence Sum Possibi
2015-11-14 16:21
435 查看
1209. Sequence Sum Possibi
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
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
题目分析
求给定的数可以写成连续几个数(至少2个)的和的方法
暴力枚举超时
可以利用等差数列公式,枚举项数。因为假设有i项相加可以得到,则这i项唯一确定
设有 i 项,为 l,l+1,l+2....l+i-1;
则(2l + i-1 ) * i = 2n
即2l*i + (i-1)*i = 2n
若l为整数则成立
#include <stdio.h>
int main()
{
int test;
scanf("%d", &test);
int id, num;
while (test--) {
scanf("%d%d", &id, &num);
int count = 0;
for (int i = 2; i*(i-1) < 2*num; ++i) {
if ( (2*num - i*(i-1)) % (2*i) == 0 )
count++;
}
printf("%d %d\n", id, count);
}
}
相关文章推荐
- Guest Speaker on 2015 WinHEC Shenzhen 秋季大会
- ural 1247. Check a Sequence
- [Leetcode]Range Sum Query 2D - Immutable
- GPUImage API文档之GPUImageFramebufferCache类
- UITableView插入Section
- 关于UITableView的间隔问题(header和footer)和分割线
- iOS UITableView head view与cell view文字垂直居中,文字右对齐
- 随意细解:UI -- 事件处理
- Java小程序之GUI开发简单前台登录界面
- UI基础-事件处理
- 5.UIButton(按钮)知识总结
- sicily 1242. Suit Distribution
- 重写ENqUEUE和DEQUEUE,使之能处理队列的下溢和上溢。
- parquet格式的表用HiveContext写入用Spark thriftserver查不到更新
- 4.UIAlertView和UIActionSheet(警报视图)知识总结
- 爬爬爬之路:UI(三)自定义视图 MVC浅谈 视图控制器 屏幕旋转事件获取
- UI基础-自定义视图、视图控制器
- 随意细解:UI -- 自定义视图、视图控制器
- 进击的KFC:UI(四)实现划屏效果,用View实现Button的效果
- AlertDialog.Builder setCancelable用法