问你一个字符串字典序大于另一个字符串的概率.
很容易,没有思维难度,只要知道逆元就可以,注意细节.
//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;
}
寒假,完全失败了呢.