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

POJ 2853 Sequence Sum Possibilities的更多相关文章

  1. Poj 2853,2140 Sequence Sum Possibilities(因式分解)

    一.Description Most positive integers may be written as a sum of a sequence of at least two consecuti ...

  2. [POJ 3581]Sequence

    [POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...

  3. Ural 1248 Sequence Sum 题解

    目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...

  4. POJ 2479 Maximum sum POJ 2593 Max Sequence

    d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...

  5. POJ 2442 Sequence

    Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于  a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...

  6. POJ 2442 - Sequence - [小顶堆][优先队列]

    题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...

  7. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

  8. POJ 3415 Max Sum of Max-K-sub-sequence (线段树+dp思想)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

随机推荐

  1. [JS6] 通过用户事件事件执行脚本

    <html> <head> <title>通过用户事件事件执行脚本</title> <SCRIPT TYPE="text/JavaScr ...

  2. paip.powerdesign cdm pdm文件 代码生成器 java web 页面 实现

    paip.powerdesign cdm pdm文件 代码生成器 java web 页面 实现 准备从pd cdm生成java web 页面...但是,ms无直接地生成软件.... 只好自己解析cdm ...

  3. GUID全局唯一标识符相关知识了解

     全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络或系统中.在理想情 ...

  4. js系列(9)js的运用(一)

        本节开始介绍javascript在html页面中的运用.     (1)link样式表的动态绑定:(http://files.cnblogs.com/files/MenAngel/text04 ...

  5. 懂DOS终于发挥了一点作用:phoenix bios密码破解

    手上一个笔记本,不知开机密码,但bios是老phoenix的bios,出错后有溢出码,到网上下载了一个unlock6,满怀希望地进行破解,结果一运行,屏幕就没反应.试了几个都不行.最后怀疑是不是输出的 ...

  6. 报错:ASP.NET Web API中找不到与请求匹配的HTTP资源

    当发出GET请求: GET http://localhost:54176/api/Products 报如下错: {  "message": "找不到与请求 URI“htt ...

  7. AngularJs应用页面切换优化方案(转)

    目录[-] 前言 场景 使用resolve来提前请求数据 为页面加入切换动画 总结 葡萄城的一款尚在研发中的产品,对外名称暂定为X项目.其中使用了已经上市的wijmo中SpreadJS产品,另外,在研 ...

  8. ELK——在 CentOS/Linux 把 Kibana 3.0 部署在 Nginx 1.9.12

    上一篇文章<安装 logstash 2.2.0.elasticsearch 2.2.0 和 Kibana 3.0>,介绍了如何安装 Logstash.Elasticsearch 以及用 P ...

  9. nexus中央仓库中发布自己的jar包

    1.后台上传 通过nexus后台上传第三方包: http://blog.csdn.net/huchunlinnk/article/details/17789175 上面上传的只能上传release版本 ...

  10. nexus安装实例

    Nexus安装 1.下载Nexus:[笔者版本nexus-2.11.2-03-bundle.tar.gz] 2. 安装nexus [root@localhost local]# pwd /usr/lo ...