USACO 1.4 Arithmetic Progressions(枚举)

好纠结的。。。复杂度很高,开始的方法太暴力了,后来搞了一个 快了一点的枚举办法,水过了。。

 1 /*

 2 ID: cuizhe

 3 LANG: C++

 4 TASK: ariprog

 5 */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 #include <algorithm>

10 #include <ctime>

11 using namespace std;

12 #define LL long long

13 int p[1250001],o[1250001];

14 struct node

15 {

16    int x,y;

17 }que[100001];

18 int cmp(const node &a,const node &b)

19 {

20     if(a.y < b.y)

21     return 1;

22     else if(a.y > b.y)

23     return 0;

24     else if(a.x < b.x)

25     return 1;

26     else

27     return 0;

28 }

29 int main()

30 {

31     int i,j,k,n,m,num,t,dis;

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

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

34     scanf("%d%d",&n,&m);

35     num = 1;

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

37     {

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

39         {

40             if(!o[i*i+j*j])

41             {

42                 p[num ++] = i*i+j*j;

43                 o[i*i+j*j] = 1;

44             }

45         }

46     }

47     sort(p+1,p+num);

48     t = 1;

49     for(i = 1;i <= num-n;i ++)

50     {

51        for(j = i+1;j <= num-1;j ++)

52        {

53            dis = p[j] - p[i];

54            for(k = 2;k <= n-1;k ++)

55            {

56                if(dis*k+p[i] > p[num-1])

57                break;

58                if(!o[dis*k+p[i]])

59                {

60                    break;

61                }

62            }

63            if(k == n)

64            {

65                que[t].x = p[i];

66                que[t].y = dis;

67                t ++;

68            }

69        }

70     }

71     sort(que+1,que+t,cmp);

72     for(i = 1;i <= t-1;i ++)

73     printf("%d %d\n",que[i].x,que[i].y);

74     if(t == 1)

75     printf("NONE\n");

76     return 0;

77 }

你可能感兴趣的:(progress)