[CF1111D] Destory the Colony

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[CF1111D] Destory the Colony相关的知识,希望对你有一定的参考价值。

?????????==   des   func   fpm   names   printf   ems   name   clock   

Portal

????????????: ????????????????????????((n leq 10 ^ 5))????????????, ????????????????????????. ???q((q leq 10 ^ 5))?????????, ????????????????????????, ????????????????????????, ???????????????????????????????????????????????????????????????????????????, ????????????????????????(???????????????????????????, ???????????????????????????????????????), ???????????????(1e9 + 7)

Solution

??????????????????atcoder???

???????????????????????????, ???????????????(52 ^ 2 = 2704)?????????, ??????????????????????????????.

????????????????????????,O(n)???????????????. ???????????????(frac{(frac{n}{2})! ^ 2}{prod {cnt_i !}}) ??????????????????????????????????????????50???????????????(frac{n}{2} - cnt_i - cnt_j)?????????;

????????????????????????????????????????????????, ????????????????????????????????????.

?????????????????????????????????????????????. ????????????????????????????????????, ????????????????????????????????????????????????, ???????????????????????????????????????????????????dp???, ??????, ???????????????????????????????????????????????????.

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s
", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
int read() {
    char ch = getchar();
    int x = 0, flag = 1;
    for(;!isdigit(ch); ch = getchar()) if(ch == ???-???) flag *= -1;
    for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    return x * flag;
}
void write(LL x) {
    if(x < 0) putchar(???-???), x = -x;
    if(x >= 10) write(x / 10);
    putchar(x % 10 + 48);
}

const int Maxn = 100009, Maxk = 60, Mod = (int)1e9 + 7;
char s[Maxn];
int n, q, dp[Maxn], cnt[Maxk];
int predict[Maxk][Maxk], tmpDp[Maxn];
int fac[Maxn], invFac[Maxn];

int fpm(int base, int tims) {
    int r = 1;
    while (tims) {
        if (tims & 1) r = 1ll * base * r % Mod;
        base = 1ll * base * base % Mod;
        tims >>= 1;
    }
    return r;
}

void init() {
    scanf("%s", s + 1); n = strlen(s + 1);
    rep (i, 1, n) 
        if (isupper(s[i])) ++cnt[s[i] - ???A??? + 1];
            else ++cnt[s[i] - ???a??? + 27]; 

    /* Note */
    dp[0] = 1;
    rep (i, 1, 52) {
        if (!cnt[i]) continue;
        drep (j, n, cnt[i]) (dp[j] += dp[j - cnt[i]]) %= Mod;
    }
    /* Note */
    
    fac[0] = 1;
    rep (i, 1, n) fac[i] = fac[i - 1] * 1ll * i % Mod;
    invFac[n] = fpm(fac[n], Mod - 2);
    drep (i, n - 1, 0) invFac[i] = invFac[i + 1] * (i + 1ll) % Mod;
}

void solve() {
    rep (i, 1, 52)
        rep (j, 1, i) {
            if (!cnt[i] || !cnt[j]) continue;
            rep (l, 0, n) tmpDp[l] = dp[l];
            rep (l, cnt[i], n) {
                (tmpDp[l] -= tmpDp[l - cnt[i]]) %= Mod; 
                (tmpDp[l] += Mod) %= Mod;
            }

            if (i == j) {
                predict[i][i] = tmpDp[n / 2];
                continue;
            }
            
            rep (l, cnt[j], n) {
                (tmpDp[l] -= tmpDp[l - cnt[j]]) %= Mod; 
                (tmpDp[l] += Mod) %= Mod;
            }

            predict[i][j] = predict[j][i] = tmpDp[n / 2];
        }

    int W = fac[n / 2] * 1ll * fac[n / 2] % Mod;
    rep (i, 1, 52) 
        if (cnt[i] > 0) W = 1ll * W * invFac[cnt[i]] % Mod;
    
    q = read();
    rep (i, 1, q) {
        int x = read(), y = read();
        if (isupper(s[x])) x = s[x] - ???A??? + 1; else x = s[x] - ???a??? + 27; 
        if (isupper(s[y])) y = s[y] - ???A??? + 1; else y = s[y] - ???a??? + 27; 
        printf("%d
", 2ll * W % Mod * predict[x][y] % Mod);
    }
}

int main() {
    freopen("Cf1111D.in", "r", stdin);
    freopen("Cf1111D.out", "w", stdout);

    init();
    solve();

#ifdef Qrsikno
    debug("
Running time: %.3lf(s)
", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
    return 0;
}

以上是关于[CF1111D] Destory the Colony的主要内容,如果未能解决你的问题,请参考以下文章

CF - 1111D Destroy the Colony DP

Codeforces1111D Destroy the Colony 退背包+组合数

CF1520E Arranging The Sheep

CF1520E Arranging The Sheep

CF1520E Arranging The Sheep

[题解] CF622F The Sum of the k-th Powers