#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9
#define MAX 250
int lenOfProgressions;
int upperBound;
//双平方数的哈希
int isBisquare[2 * MAX * MAX + 1];
int main(){
#if DEBUG
int testCase;
for (testCase = 1; testCase <= TESTCASES; testCase++){
char inputFileName[20] = "inputx.txt";
inputFileName[5] = '1' + (testCase - 1);
freopen(inputFileName, "r", stdin);
printf("\n#%d\n", testCase);
#endif
scanf("%d%d", &lenOfProgressions, &upperBound);
int i;
for (i = 0; i < max; i++)
isBisquare[i] = 0;
int p, q;
for (p = 0; p <= upperBound; p++)
for (q = 0; q <= upperBound; q++)
isBisquare[p * p + q * q] = 1;
int max = 2 * upperBound * upperBound;
int hasNoProgressions = 1;
int a, b;
//因为输出要先按b排序再按a排序,所以先枚举b,再枚举a
for (b = 1; b <= max; b++)
for (a = 0; a + (lenOfProgressions - 1) * b <= max; a++){
int isProgressions = 1;
int n;
for (n = 0; n < lenOfProgressions; n++)
if (isBisquare[a + n * b] == 0){
isProgressions = 0;
break;
}
if (isProgressions){
hasNoProgressions = 0;
printf("%d %d\n", a, b);
}
}
if (hasNoProgressions)
printf("NONE\n");
#if DEBUG
}
#endif
return 0;
}
USACO 1.4 Arithmetic Progressions (暴力搜索)
最新推荐文章于 2018-12-22 13:57:37 发布