题目描述
算术进展是形式a,a + b,a + 2b,...,a + nb的序列,其中n = 0,1,2,3,...。 对于这个问题,a是非负整数,b是正整数。编写一个程序,可以找到在bisquares的集合S中的长度为n的所有算术进程。 所述二元组的集合被定义为形式为p2 + q2的所有整数的集合(其中p和q是非负整数)。
时间限制:5秒
程序名称:ariporg
输入格式
行1:N(3 <= N <= 25),进行搜索的长度
第2行:M(1 <= M <= 250),上限将搜索限制为0 <= p,q <= M.
输入 (file ariporg.in)
5 7
输出格式
如果没有找到序列,单行读“NONE”。 否则,输出一行或多行,每行有两个整数:找到的序列中的第一个元素和相同序列中的连续元素之间的差异。 这些线应该以最小差异序列排序,首先在那些序列中以最小起始号排序。
将不超过10,000个序列。
输出(file ariporg.out)
1 4 37 4 2 8 29 8 1 12 5 12 13 12 17 12 5 20 2 24
解题代码
/*
ID: 15189822
PROG: ariprog
LANG: C++
*/
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
ifstream fin("ariprog.in");
ofstream fout("ariprog.out");
const int N=125000;
bool f[N+1];
struct sl{
int a,b;
}x[10001];
int m,n,r=0;
int cmp(const void *a,const void *b){
struct sl *k1=(struct sl *)a;
struct sl *k2=(struct sl *)b;
if (k1->b==k2->b) return k1->a-k2->a;
else return k1->b-k2->b;
}
void init(){
int i,j;
for (i=0;i<=m;i++){
for (j=i;j<=m;j++){
f[i*i+j*j]=true;
}
}
}
void res(){
int i,j;
for (i=0;i<=2*m*m;i++){
if (!f[i]) continue;
for (j=1;j<=2*m*m/(n-1);j++){
if (i+j>2*m*m||!f[i+j]) continue;
if (i+j*2>2*m*m||!f[i+j*2]) continue;
if (i+j*(n-1)>2*m*m||!f[i+j*(n-1)]) continue;
int cnt=3;
while (cnt<n){
if (!f[i+cnt*j]) break;
cnt++;
}
if (cnt==n){
x[r].a=i;
x[r].b=j;
r++;
}
}
}
}
int main(){
fin>>n>>m;
init();
res();
if (r==0) fout<<"NONE"<<endl;
else{
qsort(x,r,sizeof(x[1]),cmp);
for (int i=0;i<r;i++){
fout<<x[i].a<<" "<<x[i].b<<endl;
}
}
return 0;
}