一道完全背包的变形,方案累加
题目链接
ACcode
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
bool check(int x) {//素数筛
double m = sqrt(x);
for (int i = 2;i <= m;i++)if (x % i == 0)return false;
return true;
}
const int M = 1e3 + 9;
ll dp[M];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;cin >> n;
dp[0] = 1;
for (int i = 2;i <= n;i++) {
if (check(i)) {
for (int j = i;j <= n;j++) {//凑出的数至少比i大,至多小于n
dp[j] += dp[j - i];
}
}
}
cout << dp[n];
return 0;
}