Luo Valley P1832 A + B Problem (upgrade) Program Count full backpack explanations

Topic links: https://www.luogu.com.cn/problem/P1832

Title effect:
Given a positive integer n, to find the total number of programs and broken down into a number of prime numbers.

Problem-solving ideas:

  • First of all find (\ le n \) \ prime number;
  • Converting the problem into a capacity \ (n-\) of the backpack, and several pieces of the same volume and value items, their position seeking out all prime numbers.
  • COMPLETE Number backpack program

Code:

  • Is the beginning of prime number sieve to obtain all primes meet the requirements;
  • Then backpack full count

Codes are as follows:

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

Guess you like

Origin www.cnblogs.com/quanjun/p/12027152.html