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 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.

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

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 bool a[125000+10];
 6 int n,m,maxn=0;
 7 bool ok(int x,int y)
 8 {
 9     for(int i=0;i<n;i++){
10         if(a[i*y+x]==false||i*y+x>maxn) return false;
11     }
12     return true;
13 }
14 int main()
15 {    
16     freopen("ariprog.in","r",stdin);
17     freopen("ariprog.out","w",stdout);
18     cin>>n>>m;
19     for(int p=0;p<=m;p++){
20         for(int q=0;q<=m;q++){
21             a[p*p+q*q]=true;
22         }
23     }
24     maxn=m*m+m*m;
25     int y=0;
26     int m=maxn/(n-1);
27     for(int i=1;i<=m;i++){
28         for(int j=0;j<=maxn-i*(n-1);j++){
29             if(ok(j,i)){
30                 cout<<j<<" "<<i<<endl;
31                 y=1;
32             }
33         }
34     }
35     if(!y) cout<<"NONE"<<endl;
36     return 0;
37 }

 

posted on 2016-01-26 13:55  Sunny糖果  阅读(234)  评论(0编辑  收藏  举报