首页 > 其他分享 >Codeforces Round #465 (Div. 2) D. Fafa and Ancient Alphabet 数学概率

Codeforces Round #465 (Div. 2) D. Fafa and Ancient Alphabet 数学概率

时间:2023-04-24 23:04:09浏览次数:42  
标签:Ancient 465 Alphabet ll define int ans include mod

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

相关文章

  • 00465 心理卫生与心理辅导
    1.心理卫生与心理辅导概述2.心理健康概述3.学校心理辅导4.心理辅导的理论与方法5.中小学生个性社会性发展与辅导6.中小学生生涯发展与辅导7.......
  • biopython Bio.Alphabet
    参考:https://biopython.org/wiki/Alphabet作用:1.记录序列的分子类型(DNA,RNA或蛋白质),2.在序列、alignment、motif等中声明预期的字符。在Biopython1.78及以后的版......
  • P5465
    #[P5465[PKUSC2018]星际穿越](https://www.luogu.com.cn/problem/P5465)一道倍增好题。建议先把[P4155[SCOI2015]国旗计划](https://www.luogu.com.cn/problem/P4155)......
  • [LeetCode] 1138. Alphabet Board Path
    Onanalphabetboard,westartatposition (0,0),correspondingtocharacter board[0][0].Here, board=["abcde","fghij","klmno","pqrst","uvwxy","z"],......
  • 1138.alphabet-board-path 字母板上的路径
    问题描述1138.字母板上的路径解题思路考虑到'z'单独在一个地方,因此移动顺序中,左下、右上不能反过来,即不能先往下再往左或者先往右再往上。代码classSolution{publi......
  • DA子卡设计资料原理图保存:FMCJ465-2路 16bit 12.6GSPS FMC DA子卡
    FMCJ465-2路16bit12.6GSPSFMCDA子卡一、板卡概述:   FMCJ465是一款转换速率最高为12.6GSPS的 DAC回放板,DAC位数16bit; 板卡基于ADI的AD9172系列......
  • 邮件端口25、109、110、143、465、995、993、994
    全部常用邮件端口25、109、110、143、465、995、993、994全部常用邮件端口有:25、109、110、143、465、995、993等,常见各大邮箱协议和端口见下方 1)发邮件协议和端口:A......
  • FMC DA子卡设计原理图:FMCJ465-2路 16bit 12.6GSPS FMC DA子卡
    FMCJ465-2路16bit12.6GSPSFMCDA子卡一、板卡概述:   FMCJ465是一款转换速率最高为12.6GSPS的 DAC回放板,DAC位数16bit; 板卡基于ADI的AD9172系列......
  • Luogu P5465 [PKUSC2018] 星际穿越
    观察可以发现一个结论,可以视作每个点\(i\)可以一步到达\(l_i\simn\)的每一个点。发现对于\(a<b<x\),\(dist(a,x)\gedist(b,x)\)第一步是相当特殊的,因为第一步......
  • P4654 [CEOI2017] Mousetrap
    \(\mathcalLink\)为了方便,以目标为根,向深度浅的位置走为“向上走”,否则为“向下走”。考虑到老鼠一旦开始向下走,它就一定会一直向下,直到走到叶子或者唯一向下的路被堵住......