cf 370 概率 逆元

问你一个字符串字典序大于另一个字符串的概率.
很容易,没有思维难度,只要知道逆元就可以,注意细节.

//Fafa and Ancient Alphabet
#include <iostream>
#include <vector>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 1e5+17;
const int MOD = 1e9+7;
LL egcd(LL a, LL b, LL& x, LL& y)
{
    LL d = a;
    if(b != 0){
        d = egcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else {
        x = 1;
        y = 0;
    }
    return d;
}
LL inv(LL a, LL m)
{
    LL x, y;
    egcd(a, m, x, y);
    return (m + x % m) % m;
}
LL a[MAXN],b[MAXN];
int main(int argc ,char const *argv[])
{
    #ifdef noob
    freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
    #endif
    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 up=1,down = 1,ans=0;
    for (int i = 0; i < n; ++i)
    {
        if(a[i]==0&&b[i]!=0)
        {
            down = down*m%MOD;
            ans = (ans+(((up*(m-b[i]))%MOD)*(inv(down, MOD)))%MOD)%MOD;
        }
        else if(a[i]!=0&&b[i]==0)
        {
            down = down*m%MOD;
            ans = (ans+((up*(a[i]-1)%MOD)*(inv(down, MOD)))%MOD)%MOD;
        }
        else if(a[i]==0&&b[i]==0)
        {
            down = down*m%MOD;
            LL s = (up*(m-1))%MOD;
            LL x = inv(down*2,MOD);
            ans = (ans+(s*x)%MOD)%MOD;
        }
        else
        {
            if(a[i]==b[i]) continue;
            if(a[i]<b[i]) break;
            ans = (ans+(up*inv(down, MOD))%MOD)%MOD;
            break;
        }
    }
    cout<<ans%MOD<<endl;
    return 0;
}

寒假,完全失败了呢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值