bzoj 4197 [Noi2015]寿司晚宴

3 10000

9

2≤n≤500

0< p≤1000000000

题解

dalao的题解吧。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> Pair;
const int P = 8, N = 510, pr[] = {2, 3, 5, 7, 11, 13, 17, 19};
int n, mod, f[1 << P][1 << P], g[2][1 << P][1 << P];
long long ans;
Pair st[N];
main() {
scanf("%d%d", &n, &mod);
for (int i = 2; i <= n; ++i) {
st[i].second = 0;
int t = i;
for (int j = 0; j < P; ++j)
if (t % pr[j] == 0) {
st[i].second |= 1 << j;
while (t % pr[j] == 0) t /= pr[j];
}
st[i].first = t;
}
sort(st + 2, st + 1 + n);
f[0][0] = 1;
for (int i = 2; i <= n; ++i) {
if (i == 2 || st[i].first == 1 || st[i].first != st[i - 1].first)
memcpy(g[0], f, sizeof f), memcpy(g[1], f, sizeof f);
for (int s1 = (1 << P) - 1; s1 >= 0; --s1)
for (int s2 = (1 << P) - 1; s2 >= 0; --s2) {
if ((s2 & st[i].second) == 0) (g[0][s1 | st[i].second][s2] += g[0][s1][s2]) %= mod;
if ((s1 & st[i].second) == 0) (g[1][s1][s2 | st[i].second] += g[1][s1][s2]) %= mod;
}
if (i == n || st[i].first == 1 || st[i].first != st[i + 1].first)
for (int s1 = (1 << P) - 1; s1 >= 0; --s1)
for (int s2 = (1 << P) - 1; s2 >= 0; --s2)
f[s1][s2] = (long long)((g[0][s1][s2] + g[1][s1][s2] - f[s1][s2]) % mod + mod) % mod;
}
for (int s1 = 0; s1 < (1 << P); ++s1)
for (int s2 = 0; s2 < (1 << P); ++s2)
if ((s1 & s2) == 0) ans += 1ll * f[s1][s2], ans = (ans % mod + mod) % mod;
printf("%lld\n", ans);
}