• 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;
    } 
  • 相关阅读:
    List<string>里的集合和字符串互转
    黑马程序员学习9
    黑马程序员学习7
    黑马程序员学习11
    黑马程序员学习10
    黑马程序员学习8
    黑马程序员学习12
    为什么Huffman编码不会发生冲突
    mule esb 配置maven 何苦
    php实现kafka功能开发 何苦
  • 原文地址:https://www.cnblogs.com/wlzs1432/p/13778713.html
Copyright © 2020-2023  润新知