概率的计算答案给出的这张图很清楚了,然后因为要求取模,a/b%M=a*b^-1%M=a*inv(b,M)%M;

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdio> using namespace std;
const int maxn=+;
const int MOD=;
typedef long long LL;
int n,m;
int s1[maxn],s2[maxn],d[maxn];
void gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a;x=;y=;
}else{
gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
LL inv(LL a,LL n){
LL d,x,y;
gcd(a,n,d,x,y);
return d==?(x+n)%n:-;
}
LL dp(int a){
if(a==n+)return ;
if(s1[a]==s2[a]&&s1[a]!=&&s2[a]!=)return dp(a+);
if(s1[a]>s2[a]&&s1[a]!=&&s2[a]!=)return ;
if(s1[a]<s2[a]&&s1[a]!=&&s2[a]!=)return ;
if(s1[a]==&&s2[a]!=)
return (((m-s2[a])%MOD*inv(m,MOD))%MOD+dp(a+)%MOD*inv(m,MOD))%MOD;
if(s1[a]!=&&s2[a]==)
return (((s1[a]-)%MOD*inv(m,MOD))%MOD+(dp(a+))%MOD*inv(m,MOD))%MOD;
if(s1[a]==&&s2[a]==)
return (((m-)%MOD*inv(*m,MOD))%MOD+(dp(a+)%MOD*inv(m,MOD)))%MOD;
} int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&s1[i]);
for(int i=;i<=n;i++)scanf("%d",&s2[i]);
LL P=dp();
cout<<P;
return ;
}

codeforce465DIV2——D. Fafa and Ancient Alphabet的更多相关文章

  1. 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)

    传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi​表示第i ni~ ni n位第一个 ...

  2. Codeforces 935D Fafa and Ancient Alphabet

    题目链接 题意 给定两个\(n\)位的\(m\)进制数\(s1,s2\),所有出现的\(0\)均可等概率地被其他数字替换,求\(s1\gt s2\)的概率. 思路 从高位到低位,根据每一位上相应的\( ...

  3. Codeforces 935E Fafa and Ancient Mathematics dp

    Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...

  4. CodeForces 935E Fafa and Ancient Mathematics (树形DP)

    题意:给定一个表达式,然后让你添加 n 个加号,m 个减号,使得表达式的值最大. 析:首先先要建立一个表达式树,这个应该很好建立,就不说了,dp[u][i][0] 表示 u 这个部分表达式,添加 i ...

  5. Codeforces 935E Fafa and Ancient Mathematics(表达式转树 + 树型DP)

    题目链接  Codeforces Round #465 (Div. 2) Problem E 题意  给定一个表达式,然后用$P$个加号和$M$个减号填充所有的问号(保证问号个数等于$P + M$) ...

  6. 【学术篇】CF935E Fafa and Ancient Mathematics 树形dp

    前言 这是一道cf的比赛题.. 比赛的时候C题因为自己加了一个很显然不对的特判WA了7次但找不出原因就弃疗了... 然后就想划水, 但是只做了AB又不太好... 估计rating会掉惨 (然而事实证明 ...

  7. 紫书例题-Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

  8. uva--1339 - Ancient Cipher(模拟水体系列)

    1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...

  9. Poj 2159 / OpenJudge 2159 Ancient Cipher

    1.链接地址: http://poj.org/problem?id=2159 http://bailian.openjudge.cn/practice/2159 2.题目: Ancient Ciphe ...

随机推荐

  1. Linux SSH的命令详解[转]

    http://www.linuxidc.com/Linux/2008-02/11055.htm前一阵远程维护Linux服务器,使用的是SSH,传说中的secure shell. 登陆:ssh [hos ...

  2. Golang 编译成 DLL 文件

    golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW. windows 64 位系统应下载 MinGW 的 64 位版本: https://sourceforge.net/pro ...

  3. Unity3D开发之Matrix4x4矩阵变换

    在Unity开发中时常会用到Matrix4x4矩阵来变换场景中对象的位置.旋转和缩放.但是很多人都不太理解这儿Matrix4x4变换矩阵.通过DX中的变换矩阵我来讲一讲在unity中这个变换矩阵是怎么 ...

  4. I.MX6 give su command more permission

    /************************************************************************************ * I.MX6 give s ...

  5. Android Animation 动画

    动画类型 Android的animation由四种类型组成  Android动画模式 Animation主要有两种动画模式:一种是tweened animation(渐变动画) XML中 JavaCo ...

  6. manyToManyField理解和用法

    转:http://www.cnblogs.com/linxiyue/p/3667418.html

  7. SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表

    SharePoint创建列表并使用程序管理列表         列表是SharePoint开发者输入数据的方式之中的一个.使用Web界面创建一个列表并加入一些数据.过程例如以下: 1. 打开站点. 2 ...

  8. C#处理不规范的时间字符串

    这样的一个情景,数据中   出生日期 是存的时间方便计算,但是前台来的数据五花八门 20170101 2017.01 2017-01-01 2017年01月1日 由于特殊原因现在确实没办法规范用户输入 ...

  9. Git的其他一些使用案例

    按照格式输出提交号 作者 时间 git log --pretty=format:"%h %an %cd" --date=iso 获取所有远程的tag和他的commit sha1 g ...

  10. CAN总线概述

    基本概念 CAN 是Controller Area Network 的缩写(以下称为CAN),是ISO国际标准化的串行通信协议.在汽车产业中,出于对安全性.舒适性.方便性.低公害.低成本的要求,各种各 ...