zoukankan      html  css  js  c++  java
  • CF935D Fafa and Ancient Alphabet

    CF935D Fafa and Ancient Alphabet

    Description

    链接

    Solutuon

    最开始想着正着推,后来发现太复杂了

    注意到末尾到末尾的情况显然更容易讨论,相当于越后放的优先级更高,不用考虑前面放的数的影响

    分类转移即可

    #include<bits/stdc++.h>
    
    using namespace std;
    
    #define LL long long
    
    inline LL read()
    {
        LL f = 1,x = 0;
        char ch;
        do
        {
            ch = getchar();
            if(ch == '-') f = -1;
        }while(ch < '0'||ch > '9');
        do
        {
            x = (x<<3) + (x<<1) + ch - '0';
            ch = getchar();
        }while(ch >= '0'&&ch <= '9');
        return f*x;
    } 
    
    const int MAXN = 1e5 + 10;
    const int MOD = 1e9 + 7;
    
    inline LL Pow(LL x,LL y)
    {
        LL res = 1 ,mul = x;
        while(y)
        {
            if(y&1) res = (res * mul)%MOD;
            mul = (mul * mul) % MOD;
            y>>=1;
        }
        return res;
    }
    
    inline LL inv(LL x)
    {
        return Pow(x,MOD-2);
    } 
    
    LL n,m;
    LL a[MAXN];
    LL b[MAXN];
    LL dp[MAXN];
    LL cnta[MAXN],cntb[MAXN];
    LL Inv;
     
    int main()
    {
        n = read(),m = read();
        Inv = inv(m);
        for(int i=1;i<=n;i++) a[i] = read();
        for(int i=1;i<=n;i++) b[i] = read();
        for(int i=1;i<=n;i++) if(!a[i]) cnta[i]++; 
        for(int i=1;i<=n;i++) if(!b[i]) cntb[i]++;
        for(int i=1;i<=n;i++) cnta[i]=cnta[i-1]+cnta[i],cntb[i]=cntb[i-1]+cntb[i];
        for(int i=n;i>=1;i--)
        {
        //    cout << i << " " << a[i] << " " << b[i] << endl;
            if(a[i]&&b[i])
            { 
            //    cout << "1" << endl; 
                if(a[i] > b[i]) dp[i] = 1;
                else if(a[i] == b[i]) dp[i] = dp[i+1];
                else dp[i] = 0;
            }
            else if(!a[i]&&b[i]) 
            {
        //        cout << "2" << endl;
                dp[i] = ((dp[i+1] + (m - b[i]))%MOD * Inv)%MOD;
            }
            else if(!b[i]&&a[i])
            {
        //        cout << "3" << endl;
                dp[i] = ((dp[i+1] + a[i] - 1)%MOD * Inv)%MOD;
            } 
            else
            {
                //cout << "4" <<endl;
                dp[i] = (((m * dp[i+1]%MOD + (m * (m-1)/2)%MOD)%MOD * Inv)%MOD)*Inv%MOD;
            }
        }
        cout << dp[1] << endl;
    } 
  • 相关阅读:
    左偏树
    论在Windows下远程连接Ubuntu
    ZOJ 3711 Give Me Your Hand
    SGU 495. Kids and Prizes
    POJ 2151 Check the difficulty of problems
    CodeForces 148D. Bag of mice
    HDU 3631 Shortest Path
    HDU 1869 六度分离
    HDU 2544 最短路
    HDU 3584 Cube
  • 原文地址:https://www.cnblogs.com/wlzs1432/p/13778713.html
Copyright © 2011-2022 走看看