钱币兑换问题
钱币兑换问题
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 114 Accepted Submission(s) : 57
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Sample Input
2934
12553
Sample Output
718831
13137761
//这题目用母函数要超时,可以用多重背包解
#include <stdio.h>#include <string.h>#include <time.h>#define _MAX 32790long c1[_MAX], c2[_MAX];int m[4] = {0, 1, 2, 3};int main(){int nNum;int i, j, k;while(~scanf("%d", &nNum)){for(i = 0; i <= nNum; i++){c1[i] = 0;c2[i] = 0;}c1[0] = 1;//1 *(1 + x + x^2 + ...) *( 1 + x ^2 + x^4 + ...) * (1 + x^3 + x^ 6 + ...)for(i = 1; i <= 3; i++){for(j = 0; j <= nNum; j++)for(k = 0; k + j <= nNum; k+=m[i])c2[k + j] += c1[j];for(j = 0; j <= nNum; j++){c1[j] = c2[j];c2[j] = 0;}}printf("%ld\n", c1[nNum]);//printf("Time Used = %.2lf\n", (double)clock() / CLOCKS_PER_SEC);} return 0;}