An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer.
Write a program that finds all arithmetic progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).
TIME LIMIT: 5 secs
PROGRAM NAME: ariprog
INPUT FORMAT
Line 1: | N (3 <= N <= 25), the length of progressions for which to search |
Line 2: | M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M. |
SAMPLE INPUT (file ariprog.in)
5 7
OUTPUT FORMAT
If no sequence is found, a singe line reading `NONE'. Otherwise, output one or more lines, each with two integers: the first element in a found sequence and the difference between consecutive elements in the same sequence. The lines should be ordered with smallest-difference sequences first and smallest starting number within those sequences first.
There will be no more than 10,000 sequences.
SAMPLE OUTPUT (file ariprog.out)
1 4 37 4 2 8 29 8 1 12 5 12 13 12 17 12 5 20 2 24
题意:
给出N(3-25)和M(1-250),N代表需要找出一个长度为N的等差数列,M代表p和q的范围最大值,即 0 <= p, q <= M。有一个集合S,这个集合S里面的数是由两个数的平方和构成的,即每个数都可以表示成 p^2 + q^2。要求在S集合中找出所有满足长度为N的等差序列,输出 a1首项 和 d公差。先按 d 大到小排序输出,再按 a1 大到小排序输出。
思路:
简单枚举。注意超时。
注意几点:
1.一个数可以由不同的 p 和 q 平方和构成;
2.用一个 num 数组记录这个数是否可以由 p 和 q 构成,另外用一个 dou 数组记录这些数是什么数(节省时间);
3.得出来的 dou 数组不一定是由小到大顺序排序的,故得出来的 dou 数组要处理两步:排序,去重;
4.不可以先预处理0到250范围的平方和数,而是输入 m 时候再处理 0 到 m ;
TLE:
/* TASK:ariprog LANG:C++ ID:sum-g1 */ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef struct { int a,b; }node; node seq[15000]; int num[150000],ans,n,m; int cmp(node i,node j) { if(i.b==j.b) return i.a<j.a; else return i.b<j.b; } void solve() { for(int i=0;i<=2*m*m;i++) { if(num[i]) //枚举首项 a1 { for(int j=i+1;j<=2*m*m;j++) { if(num[j]) //再枚举第二项求出公差 d { int k; for(k=2;k<=n-1;k++) if((i+k*(j-i))>2*m*m||!num[i+k*(j-i)]) break; //这里的判断语句要小心应该先判断有没有超过范围,再判断这个数符不符合双平方和 //不然先判断 num 数组的话,如果超过了范围,就会导致RE if(k==n) { ans++; seq[ans].a=i,seq[ans].b=j-i; } } } } } } int main() { freopen("ariprog.in","r",stdin); freopen("ariprog.out","w",stdout); ans=0; memset(num,0,sizeof(num)); scanf("%d%d",&n,&m); for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) num[i*i+j*j]=1; solve(); if(ans==0) printf("NONE\n"); else { sort(seq+1,seq+ans+1,cmp); //存起来后排序输出 for(int i=1;i<=ans;i++) printf("%d %d\n",seq[i].a,seq[i].b); } return 0; }
直接枚举导致TLE;
AC:
/* TASK:ariprog LANG:C++ ID:sum-g1 */ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int num[150000],dou[150000],ans,n,m,sum; void solve() { for(int d=1;d*(n-1)<=2*m*m;d++) //枚举公差 d { for(int i=1;i<sum;i++) { int j; for(j=1;j<=n-1;j++) if(dou[i]+j*d>2*m*m||!num[dou[i]+j*d]) break; //直接break也可省时 if(j==n) { ans++; printf("%d %d\n",dou[i],d); //直接输出,不需要再另外保存 } } } if(!ans) printf("NONE\n"); } int main() { freopen("ariprog.in","r",stdin); freopen("ariprog.out","w",stdout); ans=0,sum=0; memset(num,0,sizeof(num)); scanf("%d%d",&n,&m); for(int i=0;i<=m;i++) for(int j=i;j<=m;j++) if(!num[i*i+j*j]) //去重 { sum++; dou[sum]=i*i+j*j; num[i*i+j*j]=1; } sort(dou+1,dou+1+sum); //排序 solve(); return 0; }