【BZOJ4197】【UOJ129】【NOI2015】寿司晚宴(状压DP)

Description

为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴。小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴。

在晚宴上,主办方为大家提供了 n−1 种不同的寿司,编号 1,2,3,…,n−1,其中第 i 种寿司的美味度为 i+1 (即寿司的美味度为从 2 到 n)。
现在小 G 和小 W 希望每人选一些寿司种类来品尝,他们规定一种品尝方案为不和谐的当且仅当:小 G 品尝的寿司种类中存在一种美味度为 x 的寿司,小 W 品尝的寿司中存在一种美味度为 y 的寿司,而 x 与 y 不互质。
现在小 G 和小 W 希望统计一共有多少种和谐的品尝寿司的方案(对给定的正整数 p 取模)。注意一个人可以不吃任何寿司。


Solution

由于 n 最大只有 500 ,所以大于 22 的质因子最多只有 1 个。
我们先只处理没有大于 22 的质因子的数:
f s 1 , s 2 表示小G拿到的数的质因子状态为 s 1 ,小W为 s 2 ,转移显然。

我们将大质因子相同的数相同的数一起处理,用 g 0 / 1 / 2 , s 1 , s 2 表示两个人的质因子状态为 s 1 , s 2 0 / 1 / 2 分别表示两个人都没有取大质因子、小G取了大质因子、小W取了大质因子,转移参见代码。


Code

/************************************************
 * Au: Hany01
 * Date: Aug 4th, 2018
 * Prob: BZOJ4197 寿司晚宴
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 505, FULL = 1 << 8;

int d[maxn], id[maxn], big[maxn], n, MOD, f[FULL][FULL], g[3][FULL][FULL], Ans;
vector<int> bigs[maxn];

void ad(int& x,int y) { if ((x += y) >= MOD) x -= MOD; }

int main()
{
#ifdef hany01
    freopen("bzoj4197.in", "r", stdin);
    freopen("bzoj4197.out", "w", stdout);
#endif

    id[2] = 0, id[3] = 1, id[5] = 2, id[7] = 3, id[11] = 4, id[13] = 5, id[17] = 6, id[19] = 7;

    n = read(), MOD = read();
    For(i, 2, n) {
        int t = i;
        for (register int j = 2; j * j <= t; ++ j)
            if (!(t % j)) {
                if (j <= 19) d[i] |= (1 << id[j]); else big[i] = j, bigs[j].pb(i);
                do t /= j; while (!(t % j));
            }
        if (t > 1) {
            if (t <= 19) d[i] |= (1 << id[t]); else big[i] = t, bigs[t].pb(i);
        }
    }

    f[0][0] = 1;
    For(i, 2, n) if (!big[i])
        Fordown(s1, FULL - 1, 0) Fordown(s2, FULL - 1, 0) {
            if (!((s1 | d[i]) & s2)) ad(f[s1 | d[i]][s2], f[s1][s2]);
            if (!((s2 | d[i]) & s1)) ad(f[s1][s2 | d[i]], f[s1][s2]);
        }

    For(t, 2, n) if (SZ(bigs[t])) {
        rep(s1, FULL) rep(s2, FULL) g[0][s1][s2] = f[s1][s2], g[1][s1][s2] = g[2][s1][s2] = 0;
        rep(i, SZ(bigs[t])) Fordown(s1, FULL - 1, 0) Fordown(s2, FULL - 1, 0) {
            if (!((s1 | d[bigs[t][i]]) & s2))
                ad(g[1][s1 | d[bigs[t][i]]][s2], g[1][s1][s2]), ad(g[1][s1 | d[bigs[t][i]]][s2], g[0][s1][s2]);
            if (!((s2 | d[bigs[t][i]]) & s1))
                ad(g[2][s1][s2 | d[bigs[t][i]]], g[2][s1][s2]), ad(g[2][s1][s2 | d[bigs[t][i]]], g[0][s1][s2]);
        }
        rep(s1, FULL) rep(s2, FULL) f[s1][s2] = ((g[0][s1][s2] + g[1][s1][s2]) % MOD + g[2][s1][s2]) % MOD;
    }
    rep(s1, FULL) rep(s2, FULL) ad(Ans, f[s1][s2]);
    printf("%d\n", Ans);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/81415815