hdu 1868 Consecutive sum
hdu 1868 Consecutive sum
Problem DescriptionEvery body knew that 15 = 1+2+3+4+5 = 4+5+6 = 7+8. Now give you a number N, tell me how many ways to represent N as a sum of consecutive positive integers. For example, 15 have 3 ways to be found.
#include<cmath>
using namespace std;
int main()
{
int n,num,i,j,x,y;
while(cin>>n)
{
num=0;
for(i=(int)sqrt(2*n);i>=2;i--)
{
if(2*n%i==0)
{
x=(2*n/i-i+1)/2;
y=(2*n/i+i-1)/2;
if((y+x)*(y-x+1)==2*n)
{
num++;
}
}
}
cout<<num<<endl;
}
return 0;
}
InputEach line will contain an signed 32-bits integer N. Process to end of file.
OutputFor each case, output the answer in one line.
Sample Input151050
Sample Output311 注: 此题主要公式 (x+y)*(y-x+1)/2=n #include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,num,i,j,x,y;
while(cin>>n)
{
num=0;
for(i=(int)sqrt(2*n);i>=2;i--)
{
if(2*n%i==0)
{
x=(2*n/i-i+1)/2;
y=(2*n/i+i-1)/2;
if((y+x)*(y-x+1)==2*n)
{
num++;
}
}
}
cout<<num<<endl;
}
return 0;
}