usaco 1.43Arithmetic Progressions

直接枚举a1和d。

View Code
 1 /*
 2   ID: your_id_here
 3   PROG: ariprog
 4   LANG: C++
 5   */
 6 #include <iostream>
 7 #include<cstdio>
 8 #include<cstring>
 9 using namespace std;
10 int f[200000];
11 int main()
12 {
13     freopen("ariprog.in","r",stdin);
14     freopen("ariprog.out","w",stdout);
15     int i,j,n,m,k,a,d,flag;
16     f[0] = 1;
17     int ff = 0;
18     cin>>n>>m;
19     for(i = 0 ; i <= m ; i++)
20     for(j = 1; j <= m ; j++)
21     f[i*i+j*j] = 1;
22     for(d = 1 ; d<=2*m*m ; d++)
23     for(a = 0; ; a++)
24     {
25         if(a+(n-1)*d>2*m*m)
26         break;
27         flag = 1;
28         for(i = 0; i < n ; i++)
29         {
30             if(!f[a+i*d])
31             {
32                 flag = 0;
33                 break;
34             }
35         }
36         if(flag)
37         {
38             ff = 1;
39             cout<<a<<" "<<d<<endl;
40         }
41     }
42     if(!ff)
43     cout<<"NONE\n";
44     fclose(stdin);
45     fclose(stdout);
46     return 0;
47 }

 

posted @ 2012-11-12 20:40  _雨  阅读(184)  评论(0编辑  收藏  举报