1209. Sequence Sum Possibi

來源:互聯網
上載者:User

1209. Sequence Sum Possibi

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

純數學題。

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;int case_index;int num;for(int i=1;i<=n;i++){int count=0;cin>>case_index>>num;for(int j=2; (j+1)*j <= 2*num;j++){if((num-(j-1)*j/2) % j == 0){count++;}}cout<<i<<" "<<count<<endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.