地址:http://hustoj.sinaapp.com/problem.php?id=1831
先枚举公差,再枚举首项,这样可以直接输出
在初始化中已经知道哪些数是双平方数,哪些不是
1 #include <iostream> 2 #include <algorithm> 3 #define MAX 250 4 using namespace std; 5 6 int n,m; 7 bool s[MAX*MAX*2+1]; 8 9 void ini() 10 { 11 int i,j; 12 for(i=0;i<=m;i++) 13 for(j=0;j<=m;j++) 14 s[i*i+j*j]=true; 15 } 16 17 bool check(int a,int b) 18 { 19 for(int i=0;i<n;i++) 20 if(!s[a+i*b]) return false; 21 return true; 22 } 23 24 int main() 25 { 26 int a,b,lim; 27 bool flag=true; 28 ios::sync_with_stdio(false); 29 cin>>n>>m; 30 ini(); 31 lim=2*m*m; 32 for(b=1;b<=lim;b++) 33 for(a=0;a+(n-1)*b<=lim;a++) 34 { 35 if(check(a,b)) 36 { 37 cout<<a<<" "<<b<<endl; 38 flag=false; 39 } 40 } 41 if(flag) cout<<"NONE\n"; 42 return 0; 43 }