洛谷p1832 a+b problem

一道完全背包的变形,方案累加
题目链接

ACcode


#include

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;
}

你可能感兴趣的:(背包专题训练,完全背包,背包dp,动态规划)