此题可以事先用数组将所有的双平方数都求出来,在暴力枚举a,b,看是否在这个数组里。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int a,b;
}ans[10100];
int n,m,t,k;
int st[300],cnt[22000];
bool b[130000];
int f(int x,int y)
{
for(int i=0;i<n;i++)
if(!b[x+i*y]) return 0;
return 1;
}
int cmp(node x,node y)
{
if(x.b<y.b) return 1;
if(x.a<y.a&&x.b==y.b) return 1;
return 0;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) st[i]=i*i;
for(int i=1;i<=m;i++)
for(int j=0;j<=m;j++)
{
if(b[st[i]+st[j]]) continue;
b[st[i]+st[j]]=1;
t++;cnt[t]=st[i]+st[j];
}
sort(cnt+1,cnt+t+1);b[0]=1;
for(int i=0;i<=t;i++)
{
int m1=(cnt[t]-cnt[i])/(n-1);
for(int j=1;j<=m1;j++)
{
if(f(cnt[i],j))
{
k++;ans[k].a=cnt[i];
ans[k].b=j;
}
}
}
if(k==0) cout<<"NONE";
else
{
sort(ans+1,ans+k+1,cmp);
for(int i=1;i<=k;i++)
printf("%d %d\n",ans[i].a,ans[i].b);
}
return 0;
}