In a country there are only 1 cents, 2 cents, 3 cents, and there are many ways to exchange money n coins. Please write a procedure to figure out how many kinds of methods are in common.
Input is only one positive integer per line n,n less than 32768. Output corresponds to each input, the number of conversion methods. Sample Input
2934
12553
Sample Output
718831
13137761
Train of thought: This is a simple DP, here is not much to say, directly to serve.
#include <stdio.h>
#include <string.h>
#define MAX (A,B) (A>B?A:B)
#include <stdio.h>
#include <string.h>
#define MAX (a,b) (A>B?A:B)
#define INF 0x3f3f3f3f
long Longint dp[51000];
int n;
Intmain ()
{
while (~scanf ("%d", &n))
{
memset (dp,0,sizeof (DP));
Dp[0]=1;
for (int i=1; i<=3; i++) for
(int j=1; j<=n; j + +)
dp[j]=dp[j]+dp[j-i];
printf ("%lld\n", Dp[n]);
return 0;
}
Topic link
This is the same as the above, but the data is slightly more complex, but the idea and method are the same, the big
Home can have a look.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 100010
#define MOD 1000000007
int DP[MAXN];
int v[13]={1,2,5,10,20,50,100,200,500,1000,2000,5000,10000};
int main ()
{
int n;
while (~SCANF ("%d", &n))
{
memset (dp,0,sizeof (DP));
Dp[0]=1;
for (int i=0, i<13; i++) for
(int j=v[i];j<=n;j++)
dp[j]= (dp[j]+dp[j-v[i)])%mod;
printf ("%d\n", Dp[n]);
return 0;
}