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
期末啦,事情多啊,作业一大堆。。。 有很久没有A题啦~
发现不A题好像脑子都变笨了。。。
看样子还是要有经常做题的好习惯~
这是一道搜索+简单剪枝的题目:
题意:
我研究了好半天。。。真难懂。。。
大意是求长度为n的等差数列a+n*b;变为公差,使数列中的每一个数可以是在0~m范围内任意两个数的平方和。
暴搜的话,研究了一下时间复杂度,发现如果有5s说不定可以过。。。
直接枚举a,b的值;结果果然会超时。。。
想了一下,剪枝:
重点是:如果a+x*b>m*m*2,那么就肯定不存在这种等差数列。。。可以直接跳出对这种形式的a,b的研究,枚举下一个a
还有,对于每一个a,先判断它是否可以被表示成平方和,可以的话再往下,不可以就直接判断下一个a。
代码:
1 /* 2 ID: jings_h1 3 PROG: ariprog 4 LANG: C++ 5 */ 6 7 #include<iostream> 8 #include<fstream> 9 #include<stdio.h> 10 #include<string.h> 11 #include<algorithm> 12 using namespace std; 13 int a[250*250*2+10]; 14 struct node{ 15 int start; 16 int end; 17 }; 18 bool cmp (const node & c,const node & d){ 19 if(c.end!=d.end) 20 return c.end<d.end; 21 else 22 return c.start<d.start; 23 } 24 bool istrue(int v,int temp){ 25 if(a[v]==1){ 26 return true; 27 } 28 return false; 29 } 30 int main(){ 31 FILE *fin = fopen("ariprog.in", "r"); 32 FILE *fout = fopen("ariprog.out", "w"); 33 // ofstream fout ("ariprog.out"); 34 // ifstream fin ("ariprog.in"); 35 int n,m; 36 memset(a,0,sizeof(a)); 37 fscanf(fin,"%d%d",&n,&m); 38 // cin>>n>>m; 39 int times=0; 40 node res[10005]; 41 int sum=0; 42 // int a[250*250]; 43 for(int i=0;i<=m;i++){ 44 for(int j=i;j<=m;j++){ 45 a[i*i+j*j]=1; 46 // a[times++]=i*i+j*j; 47 // cout<<a[times-1]<<endl; 48 } 49 } 50 for(int k=0;(k+n-1)<=m*m*2;k++){ 51 if(!istrue(k,times)) 52 continue; 53 for(int g=1;;g++){ 54 if((k+g*(n-1))>m*m*2) 55 break; 56 int j; 57 for(j=0;j<n;j++){ 58 int temp=k+g*j; 59 if(!istrue(temp,times)) 60 break; 61 } 62 if(j>=n){ 63 res[sum].start=k; 64 res[sum].end=g; 65 sum++; 66 } 67 } 68 } 69 if(sum==0){ 70 fprintf(fout,"NONE\n"); 71 // cout<<"NONE"<<endl; 72 // continue; 73 } 74 else{ 75 sort(res,res+sum,cmp); 76 for(int k=0;k<sum;k++){ 77 // cout<<res[k].start<<" "<<res[k].end<<endl; 78 fprintf(fout,"%d %d\n",res[k].start,res[k].end); 79 } 80 } 81 cin>>n; 82 return 0; 83 }