洛谷P1832 A+B Problem(再升级) 题解 完全背包方案计数

时间:2019-12-12 09:55:13   收藏:0   阅读:121

题目链接:https://www.luogu.com.cn/problem/P1832

题目大意:
给定一个正整数n,求将其分解成若干个素数之和的方案总数。

解题思路:

代码实现:

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, p[maxn], cnt;
long long f[maxn];
bool np[maxn];
void init() {
    for (int i = 2; i <= n; i ++) {
        if (!np[i]) {
            p[cnt++] = i;
            for (int j = i; j <= n/i; j ++) {
                np[i*j] = true;
            }
        }
    }
}
void comp_pack(int c) {
    for (int i = c; i <= n; i ++) f[i] += f[i-c];
}
int main() {
    cin >> n;
    init();
    f[0] = 1;
    for (int i = 0; i < cnt && p[i] <= n; i ++)
        comp_pack(p[i]);
    cout << f[n] << endl;
    return 0;
}

原文:https://www.cnblogs.com/quanjun/p/12027152.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!