Files
python/TangDou/AcWing/BeiBao/FangAnShu/P1832_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e3 + 5;
LL f[N][N];
//欧拉筛,下标从1开始
//这个欧拉筛黄海修改过,修改下标从1开始
int primes[N], st[N], cnt;
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[++cnt] = i;
for (int j = 1; primes[j] * i <= n; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
int main() {
int m;
scanf("%d", &m);
get_primes(m); // 1 ~ m以内有cnt个素数
int n = cnt;
for (int i = 0; i <= n; i++) f[i][0] = 1; //初始化,如果刚好只可以选一个数,即j == primes[i],那么只有一种方案。
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
/*
// j足够大,那么对于一个素数可以选,或是不选。两种方案要求总和。
if (j >= primes[i])
f[i][j] = f[i - 1][j] + f[i][j - primes[i]];
else
f[i][j] = f[i - 1][j]; // j不够大,只有不选这一种方案。
*/
//与上面的代码是等价的,一样可以AC
f[i][j] = f[i - 1][j];
if (j >= primes[i]) f[i][j] += f[i][j - primes[i]];
}
}
printf("%lld\n", f[n][m]);
return 0;
}