【UOJ300】【BZOJ4903】【CTSC2017】吉夫特(DP,子集枚举)

1 Description

http://uoj.ac/problem/300


2 Solution

2.1

根据Lucas定理, ( n m ) mod 2 = 1 的充要条件是 n   a n d   m = m
发现很好DP,设 f i 表示以 i 结尾的序列方案数,每次加入、转移即可。

2.2

考虑优化。
f i , j 表示所有结尾的数的前 9 i 为子集、后 9 位为 j 的方案数。
每次加入一个数 x 时,答案加上 Δ = 所有的 f x 9 , j (满足 x 的后 9 位为 j 的子集)
然后将所有的 f i , x 9 加上 Δ 即可(满足 i x 的前 9 位的子集)

记得最后的答案要减去长度为 1 的。


3 Code

3.1

/************************************************
 * Au: Hany01
 * Prob: [BZOJ4903][CTSC2017] 吉夫特
 * Email: [email protected]
************************************************/

inline int ad(int x, int y) { if ((x += y) >= Mod) return x - Mod; return x; }

int main()
{
#ifdef hany01
    File("bzoj4903");
#endif

    static int n = read(), t, x, Ans, f[233335];
    while (n --) {
        x = read();
        for (t = x; t <= 233333; (++ t) |= x) f[x] = ad(f[x], f[t]);
        Ans = ad(Ans, f[x]), f[x] = ad(f[x], 1);
    }
    printf("%d\n", Ans);

    return 0;
}
//高楼目尽欲黄昏,梧桐叶上萧萧雨。
//    -- 晏殊《踏莎行·碧海无波》

3.2

/************************************************
 * Au: Hany01
 * Prob: [BZOJ4903][CTSC2017] 吉夫特
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#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 ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#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()
{
    register int _, __; register 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 _ * __;
}

inline int ad(int x, int y) { if ((x += y) >= Mod) return x - Mod; return x; }

int main()
{
#ifdef hany01
    File("bzoj4903");
#endif

    static int T, n = read(), x, Ans, f[513][513], res, x1, x2;

    for (T = 1; T <= n; ++ T) {
        x = read(), x1 = x >> 9, x2 = x & 511, res = 1;
        register int i;
        for (i = x2; i < 512; (++ i) |= x2) res = ad(res, f[x1][i]);
        for (i = x1; i; (-- i) &= x1) f[i][x2] = ad(f[i][x2], res);
        Ans = ad(Ans, res), f[0][x2] = ad(f[0][x2], res);
    }
    printf("%d\n", ad(Ans, Mod - n));

    return 0;
}
//云移雉尾开宫扇,日绕龙鳞识圣颜。
//    -- 杜甫《秋兴八首》

猜你喜欢

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