CF935D Fafa and Ancient Alphabet 概率dp(递推)

D. Fafa and Ancient Alphabet

(简洁题意请往下翻)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

input

Copy

1 201

output

Copy

500000004

input

Copy

1 210

output

Copy

0

input

Copy

7 260 15 12 9 13 0 1411 1 0 13 15 12 0

output

Copy

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.

题意:给两个长度为n的数列,每个数列的数字范围是1-m,有可能出现缺失的部分(用0表示),在缺失的部分每个数字出现的概率相同(都是1/m),问最后a的字典序比b的大的概率.输入是 n,m 之后两行每行n个数,分别代表序列a,b.最后输出概率对1e9+7取模

提示:分数无法取模,所以要用逆元~另外要注意一下取模不然会爆

貌似有更简单的转移方式,只用一维就可以,也可以滚动数组写,但是我太弱了qwq

分几种情况讨论然后直接转移就行啦,这道题其实就是一个递推

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 100010
 5 #define mod 1000000007
 6 #define ll long long
 7 using namespace std;
 8 int n,m;
 9 int a[maxn],b[maxn];
10 ll inv[maxn],f[maxn][2];//f[i][1]表示到i位为止,a>b的概率
11 ll fp(ll x,ll a){//在jms学到的神奇写法
12     ll ret=1;
13     for(x%=mod;a;a>>=1,x=x*x%mod)
14         if(a&1) ret=ret*x%mod;
15     return ret;
16 }
17 int main(){
18     scanf("%d%d",&n,&m);//长度为n 字母表中有m个字母
19     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
20     for(int i=1;i<=n;i++) scanf("%d",&b[i]);
21     f[0][0]=1;
22     ll invm=fp(m,mod-2),inv2=fp(2,mod-2);
23     for(int i=1;i<=n;i++){
24         if(a[i]>b[i]&&b[i]) //这一位都已经确定,直接转移即可
25             f[i][1]=(f[i-1][1]+f[i-1][0])%mod;
26         else if(a[i]<b[i]&&a[i])
27             f[i][1]=f[i-1][1];//f[i][0]=0
28         else if(a[i]==b[i]&&a[i])
29             f[i][1]=f[i-1][1],f[i][0]=f[i-1][0]%mod;
30         else if(!a[i]&&!b[i]){
31             f[i][0]=f[i-1][0]*invm%mod;
32             f[i][1]=(f[i-1][1]+((f[i-1][0]*((1-invm)%mod+mod)%mod)%mod*inv2)%mod)%mod;//相等的概率是1/m,剩下的对半分
33         }
34         else if(!a[i]){
35             f[i][0]=f[i-1][0]*invm%mod;
36             f[i][1]=(f[i-1][1]+(f[i-1][0]*(invm*((m-b[i])%mod+mod)%mod)%mod)%mod)%mod;
37         }
38         else if(!b[i]){
39             f[i][0]=f[i-1][0]*invm%mod;
40             f[i][1]=(f[i-1][1]+(f[i-1][0]*(invm*((a[i]-1)%mod+mod)%mod)%mod)%mod+mod)%mod;
41         }
42     }
43     printf("%lld\n",(f[n][1]%mod+mod)%mod);
44     return 0;
45 }

原文地址:https://www.cnblogs.com/al76/p/9583478.html

时间: 2024-07-31 14:37:11

CF935D Fafa and Ancient Alphabet 概率dp(递推)的相关文章

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

hdu2089(数位DP 递推形式)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25802    Accepted Submission(s): 8967 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以

CodeForces 372B 脑洞大开的DP递推

题目: 做了两个多小时,脑洞大开,给了一个01矩阵,求以a,b,为左上角,c,d为右下角的矩阵内有多少包含部分全为0的子矩阵 对于这道题目,一开始就想到了DP递推,感觉而已,虽然准,可是做不出啊,想好了递推式子可是细节部分没办法去处理.看了CF上的题解,顿时脑洞大开,这个做法真的是太厉害了,这方法代码简洁明了,同时也提醒到了我,在方程假设出来后,对于转移的细节处理, 其实一开始我想到过这个递推式子 dp[i][j][k][l] += dp[i][j][k - 1][l] + dp[i][j][k

D. Caesar&#39;s Legions 背包Dp 递推DP

http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k2 如果这样写,用dfs写是很简单的.但是超时,我记忆化不到 如果用递推写,对于每一个状态,更新到下一个状态. 如果放的是1,那么新的状态是dp[i + 1][j][k1 + 1][0]也就是,用多了一个1,而且连续的个数也增加了.同时,2的连续个数就打破了,变成了0 这种枚举旧状态,更新下一个状态

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

玲珑杯 #10 A dp递推

A. Black and White 题意:n个格子排在一行,每个格子里都有一枚白棋或一枚黑棋.限制:不能有连续a枚黑棋或连续b枚白棋,问有多少种方案. tags:一开始还以为是组合数学,没想到又是dp.这mmp的还是签到题 考虑长度为 i 的合法序列与长度为 i−1 的合法序列有什么关系.定dp[i][black] 为有 i 枚棋子且最后一枚是黑色的合法方案数量,g[i]为有 i 枚棋子的合法方案数量.现假设前面 i-1 枚棋都是合法的,在 i 位置加一枚黑棋,唯一不合法的情况就是最后的a枚棋

Luogu P1057 传球游戏(dp 递推)

P1057 传球游戏 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同学可以把球传给自己左右的两个同学中的一个(左右任意),当老师再次吹哨子时,传球停止,此时,拿着球没有传出去的那个同学就是败者,要给大家表演一个节目. 聪明的小蛮提出一个有趣的问题:有多少种不同的传球方法可以使得从小蛮手里开始传的球,传了m次以后,又回到小蛮手里.两种传球方法被视

hdu 1723 DP/递推

题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacci 所举的例子:一个人每次能够跨一或二阶台阶,问到 n 阶台阶有几种跨法.理念是一样的,只不过跨得台阶数可能会变而已.根据 Fibonacci 数列类比过来,每次最多能传 m 人,则 A [ i ] = A [ i - m ] + A [ i - m + 1 ] +  …… + A [ i - 1

poj1163 - DP递推、递归写法

本题超链接:http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50977   Accepted: 30888 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5(Figure 1) Figure 1 shows a number triangle. Write a program that calculat