Sequence Sum Possibilities
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5537   Accepted: 3641

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 be written as a sequence of consecutive positive integers. The second number will be less than 231 (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
题目大意:输入一个整数n,问总共有多少个连续序列之和为这个数。
解题方法:如果直接从0开始遍历依次肯定超时,在这里这个序列肯定为一个公差为1的等差数列,假设首项为a1,长度为i,如果满足条件,则n = a1 * i + i * (i - 1) / 2;
即n -i * (i - 1) / 2 = a1 * i;也就是说n的值为长度为i,首项为a1的等差数列之和,所以只要判断(n -i * (i - 1) / 2) % i是否为0即可,当然长度i有一个范围,假设a1为最小值1,那么长度i肯定为最大值,n = i + i * (i - 1) / 2,即n = i * (i + 1) / 2,所以i的最大值不会超过sqrt(n * 2.0)。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std; int main()
{
int nCase, index, n;
scanf("%d", &nCase);
while (nCase--)
{
int ans = ;
scanf("%d%d", &index, &n);
for (int i = ; i <= sqrt((double)n * 2.0); i++)
{
if ((n - i * (i - ) / ) % i == )
{
ans++;
}
}
printf("%d %d\n", index, ans);
}
return ;
}
 

最新文章

  1. nlssort函数的用法以及参数
  2. Flyer(二分 HDU4768)
  3. 上传图片shell绕过过滤的方法
  4. Word删除复制后产生空行
  5. POJ 2886 Who Gets the Most Candies?(反素数+线段树)
  6. Maven的使用--Eclipse在线安装Maven插件m2e
  7. 《JS权威指南学习总结--6.2属性的查询和设置》
  8. Ceres Solver for android
  9. vijos1325 桐桐的糖果计划
  10. Bootstrap table使用心得---thead与td无法对齐的问题
  11. selenium实现自动下载文件
  12. tp5中设置指定的log日志,可单独建立文件夹和文件名
  13. 程序员的自我救赎---12.2.3: 虚拟币交易平台(区块链) 下 【C#与以太坊通讯】
  14. android插件化之路
  15. Vue项目用了脚手架vue-cli3.0,会报错You are using the runtime-only build of Vue where the template compiler is not available.....
  16. Java String相关
  17. django之全局默认设置查看及admin语言设置
  18. padding属性
  19. SQL记录-PLSQL日期与时间
  20. bzoj5470 / P4578 [FJOI2018]所罗门王的宝藏

热门文章

  1. [stm32] 利用uC-BmpCvt软件生成uc-gui可调用的bmp图片
  2. [BTS] SQL Adapter. New transaction cannot enlist in the specified transaction coordinator
  3. JS几种数组遍历方式以及性能分析对比
  4. js常规日期格式处理、月历渲染、倒计时函数
  5. atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
  6. atitit.提升软件开发的效率and 质量的那些强大概念and方法总结
  7. Hello.class所在路径下, 输入命令:java Hello.class,会出现什么结果,为什么?
  8. AndroidStudio的一些坑
  9. 用非管理员权限启动主程序,并用管理员权限启动子程序,导致WM_COPYDATA消息发送失败的问题
  10. 教你如何删除WIN7系统文件以及无法删除的文件