USACO 2.2 Subset Sums (DP动态规划)
2015-10-01 13:12
309 查看
#include <stdio.h> #define DEBUG 1 #define TESTCASES 9 #define MAX 39 //partitions[preNums][sum]表示在前preNums个数中选取,使总和为sum的方案数 //注意:第preNums个数,也就是最后一个数,可以选或者不选 //要long long 才不溢出 long long partitions[MAX + 1][800]; int n; 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", &n); int total = ((1 + n) * n) >> 1; if (total & 1){ printf("0\n"); #if DEBUG continue; #else return 0; #endif } total = total >> 1; partitions[1][1] = 1; partitions[1][0] = 1; int max = 1; int preNums, sum; for (preNums = 2; preNums <= n; preNums++){ int lastNum = preNums; max += lastNum; int limitSum = total > max ? total : max; for (sum = 0; sum <= limitSum ; sum++){ if (sum < lastNum) partitions[preNums][sum] = partitions[preNums - 1][sum]; else partitions[preNums][sum] = partitions[preNums - 1][sum] + partitions[preNums - 1][sum - lastNum]; } } printf("%d\n", partitions [total] >> 1); #if DEBUG } #endif return 0; }
相关文章推荐
- 推荐-新云网站管理系统 2.1 商业版(包含商城) 下载
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- 英语听写专家 2.1 Build 0086 注册码
- C#使用动态规划解决0-1背包问题实例分析
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- 动态规划
- C++ 动态规划
- Android px、dp、sp之间相互转换
- android中像素单位dp、px、pt、sp的比较
- DP(动态规划) 解游轮费用问题
- Android对px和dip进行尺寸转换的方法
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划
- Windows server2003下搭建openmeetings2.1视频会议系统