You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e3 + 5;
LL f[N][N];
//欧拉筛
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 = 0; 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 - 1]) f[i][j] += f[i][j - primes[i - 1]];
}
}
printf("%lld\n", f[n][m]);
return 0;
}