Sequence Sum Possibi

描述

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.

输入

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).

输出

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.

样例输入

7
1 6
2 9
3 8
4 1800
5 987654321
6 987654323
7 987654325

样例输出

1 1
2 2
3 0
4 8
5 17
6 1
7 23

纯数学题。

1.

设 n+.. +(n+k)=X, n>0,k>0

有 (2n+k)(k+1)=2X

k(k+1) <2X

k^2<2X

k<sqrt(2X)

枚举k到sqrt(2X)就行了

2.

假设num = (a + 0) + (a + 1) + ... + (a + i - 1)   其中i个数,则
num = i * a + 1 + 2 + ... + i - 1 = a*i + (i - 1) * i / 2
故num - (i - 1) * i / 2 = a * i
a为整数,num - (i - 1) * i / 2可被i整除

 

代码:

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int a,b,m=0;
        cin>>a>>b;
        for(int i=2;i*(i+1)<=b*2;i++)
        {
            if((b-(i*(i-1)/2))%i==0)
                m++;
        }
        cout<<a<<" "<<m<<endl;
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值