用一个bool数组标记一下,然后暴力....最长3秒多,题目要求5秒以内即可。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ariprog.in");
ofstream fout("ariprog.out");
#ifdef _DEBUG
#define out cout
#define in cin
#else
#define out fout
#define in fin
#endif
bool mark[250*250*2+1];
void solve()
{
int n,m;
in>>n>>m;
memset(mark,0,sizeof(mark));
for(int i=0;i<=m;++i)
for(int j=i;j<=m;++j){
mark[i*i+j*j] = true;
}
int max = m*m*2+1;
vector<int>v;
v.reserve(max);
for(int i=0;i<max;++i)
if(mark[i])
v.push_back(i);
int size = v.size();
int max_len = (v[size-1]-v[0])/(n-1);
bool find = false;
for(int len=1;len<=max_len;len++){
for(int i=0;i<size;++i){
int cnt = n-1;
int t =v[i];
while(cnt){
t+=len;
if(t>v[size-1])
break;
if(!mark[t])
break;
cnt--;
}
if(cnt==0){
find = true;
out<<v[i]<<' '<<len<<endl;
}
}
}
if(!find)
out<<"NONE"<<endl;
}
int main(int argc,char *argv[])
{
solve();
return 0;
}