Codeforces 935D Fafa and Ancient Alphabet(概率+逆元)

题意:

    有长度为n的两个数字串,数字范围在(1,m)如果被遮盖的数字就是0

    问第一个串比第二个串大的概率是多少。

    概率对1e9+7取膜

分析:

       


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define LL long long
#define N 100005
#define inf 0x3f3f3f3f
#define eps 0.000001
const int Mod = 1e9+7;
char str[N];
LL f(LL x,LL n)
{
    LL res=1;
    x%=Mod;
    while(n)
    {
        if(n&1)res*=x;
        x*=x;
        n/=2;
        x%=Mod;
        res%=Mod;
    }
    return res;
}
int a[N],b[N];
int main()
{
    LL n,m;
    cin>>n>>m;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    for(int i=0; i<n; i++)
    {
        cin>>b[i];
    }
    LL p=0;
    LL r=1;
    for(int i=0; i<n; i++)
    {
        if(a[i]!=0&&b[i]!=0)
        {
            if(a[i]>b[i])
                p+=f(r,Mod-2);
            if(a[i]==b[i])continue;
            break;
        }
        else if(a[i]==0&&b[i]==0)
        {
            p+=(m-1)*f(r*2*m,Mod-2);
        }
        else
        {
           if(a[i]==0)
                p+=(m-b[i])*f(r*m,Mod-2);
            else p+=(a[i]-1)*f(r*m,Mod-2);
        }
        p%=Mod;
        r*=m;
        r%=Mod;
    }
    printf("%lld\n",p%Mod);
}

猜你喜欢

转载自blog.csdn.net/lj130lj/article/details/79996108
今日推荐