标签:
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).
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. |
5 7
If no sequence is found, a single 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.
1 4 37 4 2 8 29 8 1 12 5 12 13 12 17 12 5 20 2 24
题目大意:给你n和m,n表示目标等差数列的长度(等差数列由一个非负的首项和一个正整数公差描述),m表示p,q的范围,目标等差数列的长度必须严格等于n且其中每个元素都得属于集合{x|x=p^2+q^2}(0<=p<=m,0<=q<=m),按顺序输出所有的目标数列。
思路:其实很简单,就是枚举,枚举起点和公差,一开始有点担心会超时,弄的自己神烦意乱的,但是实际上并没有。。。。。下面附上代码
1 /* 2 ID:fffgrdcc1 3 PROB:ariprog 4 LANG:C++ 5 */ 6 #include<cstdio> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 int a[250*250],cnt=0,n,m; 11 int bo[125001]; 12 struct str 13 { 14 int a; 15 int b; 16 }ans[10005]; 17 int tot=0; 18 bool check(int a,int b) 19 { 20 int temp=a+b+b,tt=m-2; 21 while(tt--) 22 { 23 if(temp>n*n*2||!bo[temp])return 0; 24 temp+=b; 25 } 26 return 1; 27 } 28 bool kong(str xx,str yy) 29 { 30 return xx.b<yy.b||(xx.b==yy.b&&xx.a<yy.a); 31 } 32 int main() 33 { 34 freopen("ariprog.in","r",stdin); 35 freopen("ariprog.out","w",stdout); 36 scanf("%d%d",&m,&n); 37 for(int i=0;i<=n;i++) 38 { 39 for(int j=i;j<=n;j++) 40 { 41 bo[i*i+j*j]=1; 42 } 43 } 44 for(int i=0;i<=n*n*2;i++) 45 if(bo[i]) 46 a[cnt++]=i; 47 for(int i=0;i<cnt;i++) 48 { 49 for(int j=i+1;j<cnt;j++) 50 { 51 if(check(a[i],a[j]-a[i])) 52 { 53 ans[tot].a=a[i]; 54 ans[tot++].b=a[j]-a[i]; 55 } 56 } 57 } 58 sort(ans,ans+tot,kong); 59 if(!tot)printf("NONE\n"); 60 for(int i=0;i<tot;i++) 61 { 62 printf("%d %d\n",ans[i].a,ans[i].b); 63 } 64 return 0; 65 }
USACO 1.4 Arithmetic Progressions
标签:
原文地址:http://www.cnblogs.com/xuwangzihao/p/5000873.html