Codeforces 935D Fafa and Ancient Alphabet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 935D Fafa and Ancient Alphabet相关的知识,希望对你有一定的参考价值。

?????????c++   ??????   ??????   ??????   pos   ??????   pow   test   std   

????????????

??????

????????????\(n\)??????\(m\)?????????\(s1,s2\)??????????????????\(0\)?????????????????????????????????????????????\(s1\gt s2\)????????????

??????

????????????????????????????????????????????????\(0\)??????????????? ???????????????

???????????????????????????????????????????????????????????????????????????????????????????????????

?????????????????????????????????????????????????????????\(s1\gt s2\)???????????????????????????

??????????????????????????????????????????1. ??????????????????????????????0???2. ?????????????????????0???????????????

??????\(x\)??????\(mod\)??????????????????\(x^{(mod-2)}\)??????????????????????????????

???????????????\(m\)???????????????????????????

Code

#include <bits/stdc++.h>
#define maxn 100010
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
int a[maxn], b[maxn];
LL rec[maxn*2];
LL poww(LL a, LL b) {
    LL ret = 1;
    while (b) {
        if (b&1) (ret *= a) %= mod;
        (a *= a) %= mod;
        b >>= 1;
    }
    return ret;
}
LL f(LL p, LL q) {
    return p * poww(q, mod-2) % mod;
}
LL GCD(LL a, LL b) { return b ? GCD(b, a%b) : a; }
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    LL NUM = (1LL*m*m%mod-m+mod)%mod * poww(2, mod-2) % mod;
    int tot=0;
    F(i, 0, n) { scanf("%d", &a[i]); if (!a[i]) ++tot; }
    F(i, 0, n) { scanf("%d", &b[i]); if (!b[i]) ++tot; }
    rec[0] = 1;
    F2(i, 1, tot) rec[i] = rec[i-1]*m%mod;
    LL q = poww(m, tot), p=0;
    int cnt=0, prev=0;
    F(i, 0, n) {
        if (a[i]&&b[i]) {
            if (a[i]>b[i]) (p += rec[cnt+tot-prev]) %= mod;
            if (a[i]!=b[i]) { printf("%I64d\n", f(p, q)); return 0; }
        }
        else if (!a[i] && !b[i]) {
            prev += 2;
            (p += (rec[cnt+tot-prev] * NUM % mod)) %= mod;
            ++cnt;
        }
        else {
            ++prev;
            if (a[i]) (p += rec[cnt+tot-prev] * (a[i]-1) % mod) %= mod;
            else (p += (rec[cnt+tot-prev] * (m-b[i]) % mod)) %= mod;
        }
    }
    LL gcd = GCD(p, q);
    p /= gcd, q /= gcd;
    printf("%I64d\n", f(p, q));
    return 0;
}

以上是关于Codeforces 935D Fafa and Ancient Alphabet的主要内容,如果未能解决你的问题,请参考以下文章

CF935D Fafa and Ancient Alphabet 概率dp(递推)

Codeforces 935 C Fifa and Fafa

Codeforces Round #465 (Div. 2) CFifa and Fafa

CodeForces 935E Fafa and Ancient Mathematics (树形DP)

Codeforces Round #465 &935C. Fifa and Fafa计算几何

Fafa and the Gates(模拟)