Ancient Egyptians are known to have used a large set of symbols to write on the walls of the temples. Fafa and Fifa went to one of the temples and found two non-empty words S1 and S2 of equal lengths on the wall of temple written one below the other. Since this temple is very ancient, some symbols from the words were erased. The symbols in the set have equal probability for being in the position of any erased symbol.
Fifa challenged Fafa to calculate the probability that S1 is lexicographically greater than S2. Can you help Fafa with this task?
You know that , i. e. there were m distinct characters in Egyptians’ alphabet, in this problem these characters are denoted by integers from 1 to m in alphabet order. A word x is lexicographically greater than a word y of the same length, if the words are same up to some position, and then the word x has a larger character, than the word y.
We can prove that the probability equals to some fraction , where P and Q are coprime integers, and . Print as the answer the value , i. e. such a non-negative integer less than 109 + 7, such that , where means that a and b give the same remainders when divided by m.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the length of each of the two words and the size of the alphabet , respectively.
The second line contains n integers a1, a2, …, an (0 ≤ ai ≤ m) — the symbols of S1. If ai = 0, then the symbol at position i was erased.
The third line contains n integers representing S2 with the same format as S1.
Output
Print the value , where P and Q are coprime and is the answer to the problem.
Examples
inputCopy
1 2
0
1
outputCopy
500000004
inputCopy
1 2
1
0
outputCopy
0
inputCopy
7 26
0 15 12 9 13 0 14
11 1 0 13 15 12 0
outputCopy
230769233
Note
In the first sample, the first word can be converted into (1) or (2). The second option is the only one that will make it lexicographically larger than the second word. So, the answer to the problem will be , that is 500000004, because .
In the second example, there is no replacement for the zero in the second word that will make the first one lexicographically larger. So, the answer to the problem is , that is 0.
题意:两串符文,0的位置是抹去的,问有多大的概率第一个串字典序>第二个;
遍历一遍即可;
注意取模对除法不封闭,那么用逆元即可;
对于逆元,我们用费马小定理即可解决
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
ll quickpow(ll a, ll b) {
ll ans = 1;
while (b > 0) {
if (b % 2)ans = ans * a%mod;
b = b / 2;
a = a * a%mod;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
ll inv(int x) {
return quickpow(x, mod - 2);
}
ll a[maxn], b[maxn];
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
int i, j;
for (i = 0; i < n; i++)cin >> a[i];
for (i = 0; i < n; i++)cin >> b[i];
ll ans = 0;
ll p = 1;
for (i = 0; i < n; i++) {
if (a[i] && b[i]) {
if (a[i] > b[i]) {
ans += p;
ans %= mod;
break;
}
else if (a[i] < b[i])break;
}
else if (!a[i] && b[i]) {
ans += p * (m - b[i]) % mod*quickpow(m, mod - 2);
ans %= mod;
p *= quickpow(m, mod - 2);
p %= mod;
}
else if (a[i] && !b[i]) {
ans += p * (a[i] - 1) % mod*quickpow(m, mod - 2);
ans %= mod;
p *= quickpow(m, mod - 2);
p %= mod;
}
else if (!a[i] && !b[i]) {
ans += p * (m - 1) % mod*quickpow(2*m, mod - 2);
ans %= mod;
p *= quickpow(m, mod - 2);
p %= mod;
}
}
cout << ans % mod << endl;
}
标签:Ancient,465,Alphabet,ll,define,int,ans,include,mod
From: https://blog.51cto.com/u_15657999/6221999