概率的计算答案给出的这张图很清楚了,然后因为要求取模,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. 类Flask实现前后端交互之代码聊天室

    前言 框架 项目目录及各自功能 流程图 后端 server backend exector 前端 ajax 页面更新 演示 简易应答模式 代理模式处理外部请求 后台日志 总结 前言 这两天老是做梦,全 ...

  2. Leetcode 1013. Partition Array Into Three Parts With Equal Sum

    简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...

  3. Compiling OpenGL games with the Flash C Compiler (FlasCC)

    Compiling OpenGL games with the Flash C Compiler (FlasCC) In this article I show how to use the Flas ...

  4. GO语言打包ICO图标

    1. go get github.com/akavel/rsrc2. 创建manifest文件, 命名:main.exe.manifest : <?xml version="1.0&q ...

  5. hibernate 一对一(One-to-One)

    一对一(one-to-one)实例(Person-IdCard) 一对一的关系在数据库中表示为主外关系.例如.人和身份证的关系.每个人都对应一个身份证号.我们应该两个表.一个是关于人信息的表(Pers ...

  6. noip济南清北冲刺班DAY1

    上午 T1 立方数 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8,27就是最小的3个立方数. 现在给定一个数P,LYK想要知道这个数 ...

  7. ecmall在linux下的安装注意事项(转) ----ecmall系统迁移

    linux+apache+mysql+php,然后自己开始在linux下安装ecmall并做迁移,整理了一下中间碰到的问题.1.系统选择的环境是centos6.3,安装不做介绍. 2.安装 MySQL ...

  8. 南阳OJ 1170 最大的数

    最大的数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个数 在这n个数 ...

  9. (转)oracle嵌套表示例

    本文转载自:http://www.cnblogs.com/gisdream/archive/2012/04/13/2445291.html ----嵌套表:就是把一个表中的字段定义为一个表,这个字段表 ...

  10. H5浏览器播放RTMP直播流

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...