P1474 [USACO2.3] Money System / [USACO07OCT]Cow Cash G
有点水,但是细究还是有点意思的题目
https://www.luogu.com.cn/problem/P1474
一开始的代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;
const int N = 30;
int w[N];
int dp[10010];
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int v, n; cin >> v >> n;
for (int i = 1; i <= v; i++)cin >> w[i];
dp[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int k = 1; k <= v; k++)
{
if (i >= w[k])dp[i] += dp[i - w[k]];
}
}
cout << dp[n];
return 0;
}
但是运行下来发现样例是128,没办法过,仔细分析了一下,发现这样的方法是把1 2 1
和1 1 2
都计数了,但是这样实际上是不行的,如果顺序有影响也应该是“走电梯”的题目(一步走1/2/3格楼梯,走到x楼梯需要几步),所以:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;
const int N = 30;
ll w[N];
ll dp[10010];
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll v, n; cin >> v >> n;
for (int i = 1; i <= v; i++)cin >> w[i];
dp[0] = 1;
for (int k = 1; k <= v; k++)//注意这里!!!!!!
{
for (int i = w[k]; i <= n; i++)//注意这里!!!!!!!!!!
{
dp[i] += dp[i - w[k]];
}
}
cout << dp[n];
return 0;
}
顺序无关,那么就采用铺硬币的方式,按硬币的面值一点一点铺过去,这样就不会重复
以及,开longlong,/(ㄒoㄒ)/~~,白交两次