Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit
Status
Description
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
Only one positive integer per line n,n less than 32768.
Output
For each input, the output conversion method number.
Sample Input
2934
12553
Sample Output
718831
13137761
Although this is a knapsack problem, but the Vegetableschinese still want to jump out of the backpack, from the traditional thinking DP way to start thinking.
The first thought immediately is f[i]=f[i-1]+f[i-2]+f[i-3] is obviously wrong, so there will be a lot of repetition, the value is very large.
So it can be found that the different solutions in the final analysis of the number of each coin is not the same. This is the question of choosing a few coins for each type of coin, so it can be linked to the backpack. For each kind of coin can choose very many, this is the complete backpack. F[n] Represents the number of options that n can be exchanged for money.
F[n] The recursion a bit clever qwq at the beginning did not think out ...
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MEM (Array) memset (array,0,sizeof (array)
long f[40000];
int e,f,n,t;
int main () {
//freopen ("In.txt", "R", stdin);
MEM (f);
F[0]=1;
for (int i=1;i<=3;i++) for
(int j=i;j<=32768;j++)
f[j]+=f[j-i];
while (cin>>n)
cout<<f[n]<<endl;
return 0;
}
Of course, Baidu after this problem there are other solutions Qwq