Link: (-_-) ZZ
Long long ago has a store that sells at a price of [1 $... in the range of K $], there are infinite items for each type of price. The current price is N yuan. You need to spend the N yuan to buy the item and find the total number of matching methods for the item you can buy.
Train of Thought: this is the question in the question of the backpack, but it won't be done. Refer to the idea of Daniel.
Code:
#include <stdio.h>#include <string.h>#define inf 10000000000000000int main(){ int i = 0, j = 0, n = 0, k = 0; __int64 a[1002], b[1002]; while(scanf("%d %d", &n, &k) != EOF ) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); a[0] = 1; for(i = 1; i<=k; i++) for(j = i; j<=n; j++) { b[j] = b[j]+b[j-i]+(a[j]+a[j-i])/inf; a[j] = (a[j]+a[j-i])%inf; } if(b[n]) printf("%I64d", b[n]); printf("%I64d\n", a[n]); } return 0;}
Code:
#include <stdio.h>#include <string.h>int main(){ int i = 0, j = 0, k = 0 , l = 0, n = 0, s = 0; __int64 dp[1002][10]; while(scanf("%d %d", &n, &k) != EOF ) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for(i = 1; i<=k; i++) for(j = i; j<=n; j++) { for(l = 0; l<9; l++) { dp[j][l] += dp[j-i][l]; if(dp[j][l]>10000) { dp[j][l] %= 10000; dp[j][l+1]++; } } } i = 8; while(dp[n][i] == 0) i--; printf("%I64d", dp[n][i--]); while(i>=0) { printf("%.4I64d", dp[n][i]); i--; } } return 0;}