USACO 1.4 Arithmetic Progressions (暴力搜索)

来源:互联网 发布:java jdk 32官网下载 编辑:程序博客网 时间:2024/06/14 02:16
#include <stdio.h>#define DEBUG 1#define TESTCASES 9#define MAX 250int lenOfProgressions;int upperBound;//双平方数的哈希int isBisquare[2 * MAX * MAX + 1];int main(){#if DEBUGint 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);#endifscanf("%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,再枚举afor (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}#endifreturn 0;}

0 0
原创粉丝点击