现在的位置: 首页 > 算法 > 正文

poj 2853

2019年02月28日 算法 ⁄ 共 1315字 ⁄ 字号 评论关闭
Sequence Sum Possibilities
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5434   Accepted: 3578

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

Source

对于k和连续的个数i,以前总喜欢凑使它们满足(k+k+i-1)/2=m,费时啊
这里可以先举出一个i,看k是否符合,大概就这样
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int n,m,num,l,ans;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d",&num,&m);
        l=sqrt(2*m);
        ans=0;
        for(int i=2;i<=l;i++)
        {
            if((2*m)%i==0&&(2*m+i-i*i)%(2*i)==0)ans++;
        }
        cout<<num<<" "<<ans<<endl;
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.