USACO 1.4 Arithmetic Progressions (堆排序 + 枚举)
来源:互联网 发布:板绘软件 编辑:程序博客网 时间:2024/05/20 22:41
#include <stdio.h>#define DEBUG 1#define TESTCASES 9#define MAX 250int lenOfProgressions;int upperBound;//双平方数的哈希int isBisquare[2 * MAX * MAX + 1];int arrayOfBisquares[2 * MAX * MAX + 1];int numOfBisquares;int heapSize;void maxHeapify(int parent){int largest = parent;int left = parent << 1;if (left <= heapSize && arrayOfBisquares[left] > arrayOfBisquares[largest])largest = left;//"<<" 的优先级很低,注意加括号int right = (parent << 1) + 1;if (right <= heapSize && arrayOfBisquares[right] > arrayOfBisquares[largest])largest = right;if (largest != parent){int tempBisquare = arrayOfBisquares[largest];arrayOfBisquares[largest] = arrayOfBisquares[parent];arrayOfBisquares[parent] = tempBisquare;maxHeapify(largest);}}void buildMaxHeap(){int indexOfBisquare;for (indexOfBisquare = heapSize >> 1; indexOfBisquare >= 1; indexOfBisquare--)maxHeapify(indexOfBisquare);}void heapSortBisquares(){buildMaxHeap();int indexOfBisquare;for (indexOfBisquare = heapSize; indexOfBisquare > 1; indexOfBisquare--){int tempBisquare = arrayOfBisquares[indexOfBisquare];arrayOfBisquares[indexOfBisquare] = arrayOfBisquares[1];arrayOfBisquares[1] = tempBisquare;heapSize--;maxHeapify(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 max = upperBound * upperBound * 2;int num;for (num = 0; num <= max; num++)isBisquare[num] = 0;numOfBisquares = 0;int p, q;for (p = 0; p <= upperBound; p++)//因为p^2 + q^2跟q^2 + p^2是一样大的,所以q的枚举只要从p值开始就可以了for (q = p; q <= upperBound; q++){int bisquare = p * p + q * q;arrayOfBisquares[++numOfBisquares] = bisquare;isBisquare[bisquare] = 1;}heapSize = numOfBisquares;heapSortBisquares();int hasNoProgressions = 1;//因为a + (n - 1) * b = p ^ 2 + q ^ 2 = max,所以b < max / (lenOfProgressions - 1)int limitOfB = max / (lenOfProgressions - 1);int b;//因为输出要先按b排序所以先枚举bfor (b = 1; b <= limitOfB; b++){int indexOfBisquare;//再按双平方数表枚举afor (indexOfBisquare = 1; arrayOfBisquares[indexOfBisquare] <= (max - (lenOfProgressions - 1) * b); indexOfBisquare++){int a = arrayOfBisquares[indexOfBisquare];int isProgressions = 1;int n;//从数列的最后一项开始往前枚举for (n = lenOfProgressions - 1; n > 0; n--)if (!isBisquare[a + n * b]){isProgressions = 0;break;}if (isProgressions){hasNoProgressions = 0;printf("%d %d\n", a, b);}}}if (hasNoProgressions)printf("NONE\n");#if DEBUG}#endifreturn 0;}
0 0
- USACO 1.4 Arithmetic Progressions (堆排序 + 枚举)
- USACO-Section 1.4 Arithmetic Progressions(枚举)
- USACO 1.4-Arithmetic Progressions
- [usaco]1.4 Arithmetic Progressions
- usaco 1.4 Arithmetic Progressions
- USACO 1.4 Arithmetic Progressions
- USACO 1.4 Arithmetic Progressions
- USACO 1.4-Arithmetic Progressions
- USACO 1.4 Arithmetic Progressions
- usaco.section1.4.Arithmetic Progressions(枚举)
- USACO-Arithmetic Progressions(枚举剪枝)
- USACO-Section1.4 Arithmetic Progressions【暴力枚举】
- USACO-Section1.4 Arithmetic Progressions [枚举]
- USACO 1.4.4 Arithmetic Progressions
- [USACO 1.4.3] Arithmetic Progressions
- usaco 1.4.4 Arithmetic Progressions
- USACO 1.4.4 Arithmetic Progressions
- USACO Section 1.4 Arithmetic Progressions
- 【线性代数公开课MIT Linear Algebra】 第一课 矩阵的行图像与列图像
- Java基础-super关键字与this关键字
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 二分查找交点个数
- 绝对路径和相对路径在javaee工程下的区别
- Java基础-super关键字与this关键字
- USACO 1.4 Arithmetic Progressions (堆排序 + 枚举)
- 擅长排列的小明
- java编写的仓库管理系统源代码或详细设计
- java中加载声音文件的方法
- 测试相关问题
- iOS开发小白学习体验-9
- hadoop 自学指南六之IO /HDFS 操作API
- hdu2647拓扑排序(逆向)
- Android学习之路-ListView常用使用方法