USACO1.4.3--Arithmetic Progressions

Arithmetic Progressions

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

 解题思路:时限5S,果断爆搜。用一个数组has[i]记录数字i是否是bisquares,然后再用一个数组f记录所有的bisquares,这个预处理能够减少很多时间,因为在极限数据下has数组长度将达到125000,而实际bisquares总数才20000多。接下来的任务就是枚举a和b了,如果a+k*b>2*m*m或者a+k*b不是bisquares,那么可以break 循环,这个剪枝也能够减少时间。

程序写得比较丑陋o(╯□╰)o。

View Code
 1 /*

 2 ID:spcjv51

 3 PROG:ariprog

 4 LANG:C

 5 */

 6 #include<stdio.h>

 7 void qsort(long aa[],long bb[],long l,long r)

 8 {

 9     long i,j,mid1,mid2,temp;

10     i=l;

11     j=r;

12     mid1=aa[(l+r)/2];

13     mid2=bb[(l+r)/2];

14     while(i<=j)

15     {

16         while(aa[i]<mid1||(aa[i]==mid1&&bb[i]<mid2)) i++;

17         while(aa[j]>mid1||(aa[j]==mid1&&bb[j]>mid2)) j--;

18         if(i<=j)

19         {

20             temp=aa[i];

21             aa[i]=aa[j];

22             aa[j]=temp;

23             temp=bb[i];

24             bb[i]=bb[j];

25             bb[j]=temp;

26             i++;

27             j--;

28         }

29     }

30     if(i<r) qsort(aa,bb,i,r);

31     if(j>l) qsort(aa,bb,l,j);

32 

33 }

34 

35 int main(void)

36 {

37     freopen("ariprog.in","r",stdin);

38     freopen("ariprog.out","w",stdout);

39     long has[150000],f[30000],aa[10005],bb[10005];

40     memset(has,0,sizeof(has));

41     long i,j,ans,m,n,total,a,b,k;

42     scanf("%ld",&n);

43     scanf("%ld",&m);

44     ans=0;

45     total=0;

46     for(i=0; i<=m; i++)

47         for(j=0; j<=m; j++)

48             if(has[i*i+j*j]==0)

49                 has[i*i+j*j]=1;

50     for(i=0;i<=2*m*m;i++)

51     if(has[i])

52     {

53         f[ans]=i;

54         ans++;

55 

56     }

57     for(i=0; i<ans-1; i++)

58         for(j=i+1; j<ans; j++)

59         {

60             a=f[i];

61             b=f[j]-f[i];

62             for(k=1; k<n; k++)

63             {

64                 if((a+b*k)>f[ans-1]) break;

65                 if(has[a+b*k]==0) break;

66             }

67             if(k==n)

68             {

69                 aa[total]=a;

70                 bb[total]=b;

71                 total++;

72 

73             }

74         }

75     qsort(bb,aa,0,total-1);

76     if(total==0) printf("NONE\n");

77     for(i=0; i<total; i++)

78         printf("%ld %ld\n",aa[i],bb[i]);

79     return 0;

80 }

 

你可能感兴趣的:(progress)