这个搜索有趣,需要限定一些东西,不解释,代码就反映了思想:
/* ID: qq104801 LANG: C++ TASK: ariprog */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define NMAX 125001 int n,m; int k[NMAX],a[NMAX]; void test() { FILE *fin = fopen ("ariprog.in", "r"); FILE *fout = fopen ("ariprog.out", "w"); fscanf(fin,"%d",&n); fscanf(fin,"%d",&m); for (int i=0;i<=2*m*m;i++) { k[i]=0; a[i]=0; } for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) k[i*i+j*j]=1; int index=0; for(int i=0;i<=(2*m*m);i++) { if(k[i]==1) { a[index++]=i; //printf("%d %d ",index-1,i); } } int b=1,bmax=2*m*m/(n-1); int count=0; for(;b<=bmax;++b) { //printf("b:%d ",b); for(int i=0;a[i]+(n-1)*b<=m*m*2 && i<index;++i) { bool f=1; for(int j=1;j<n && f;++j) if(!k[a[i]+j*b])f=0; if(f) { //printf("%d %d ",a[i],b); fprintf(fout,"%d %d ",a[i],b); ++count; } } } if(!count) fprintf(fout,"NONE "); fclose(fin); fclose(fout); } main () { test(); exit (0); }
测试用例:
USER: ll tom [qq104801] TASK: ariprog LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.003 secs, 4472 KB] Test 2: TEST OK [0.003 secs, 4472 KB] Test 3: TEST OK [0.008 secs, 4472 KB] Test 4: TEST OK [0.003 secs, 4472 KB] Test 5: TEST OK [0.022 secs, 4472 KB] Test 6: TEST OK [0.065 secs, 4472 KB] Test 7: TEST OK [0.556 secs, 4472 KB] Test 8: TEST OK [1.258 secs, 4472 KB] Test 9: TEST OK [1.104 secs, 4472 KB] All tests OK. Your program ('ariprog') produced all correct answers! This is your submission #2 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 3 2 ------- test 2 ---- 5 7 ------- test 3 ---- 14 10 ------- test 4 ---- 10 13 ------- test 5 ---- 12 50 ------- test 6 ---- 18 100 ------- test 7 ---- 21 200 ------- test 8 ---- 22 250 ------- test 9 ---- 25 250 Keep up the good work! Thanks for your submission!