http://acm.hdu.edu.cn/showproblem.php?pid=2058
本来想前缀和搞搞的,一看1 <= N, M <= 1000000000,看来是公式题,给出一个M,不可能从1枚举到1000000000,所以要找一个枚举范围,等差数列求和公式:sum=n*a1+n*(n-1)/2,n要最大,就要a1=1,所以sum=n*(n+1)/2,变一下n
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m,n||m)
{
int t=sqrt(2*m);
while(t)
{
int ans=m/t-(t-1)/2;
if(ans*t+(t*(t-1)/2)==m)
printf("[%d,%d]\n",ans,ans+t-1);
t--;
}
printf("\n");
}
return 0;
}
源文章http://www.cnblogs.com/gpsx/p/5197818.html
超时code:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int N,M;
while(cin>>N>>M)
{
if(N==0&&M==0)
break;
int n;
for(int a1=1;a1<=M&&a1<=N;a1++)
{
long long s=(2*a1-1)*(2*a1-1)+8*M;
long long s1=sqrt(s);
if(s1*s1==s)//注意,判断某double型数s是否为正整数,不能用if(!(s-(int)s)
{
long long numerator=s1+1-2*a1;
if(numerator<2)
break;
if(numerator%2==0)
{
n=numerator/2;
printf("[%d,%d]\n",a1,a1+n-1);
}
}
}
printf("\n");
}
return 0;
}