概率的计算答案给出的这张图很清楚了,然后因为要求取模,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. NGINX 配置文件配置url重写

    1.项目在根目录: location / {            index  index.html index.htm index.php l.php;            autoindex  ...

  2. httpwebrequest webrequest webresponse 总结

    http://blog.csdn.net/flymorn/article/details/6769722 使用,总结,深入,全通,指正

  3. HihoCoder 1067 最近公共祖先(ST离线算法)

    最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个 ...

  4. SSM框架——Spring+SpringMVC+Mybatis的搭建

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  5. python 使用sk_learn 遇到 问题ValueError: Expected 2D array, got 1D array instead:

    这里我找到我的问题是: 使用的是一个新的scikit学习版本,它抛出了一个错误,因为在新版本中,所有东西都必须是一个二维矩阵,甚至是一个列或行. 它甚至说:用数组来重塑你的数据.如果您的数据有一个单独 ...

  6. LeetCode 616. Add Bold Tag in String

    原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...

  7. centos6.5 安装nginx

    安装之前先安装VMware tools(方便于从windows上拷贝文件到linux) 1. nginx安装环境 nginx是C语言开发,建议在linux上运行,本次使用Centos6.5作为安装环境 ...

  8. python中的单元测试pyUnit

    python中的单元测试pyUnit   在Python中进行单元测试时需要用到PyUnit模块,Python 2.1及其以后的版本都将PyUnit作为一个标准模块,但如果你使用的是较老版本的Pyth ...

  9. Spring AOP声明式事务异常回滚

    近日测试用例,发现这样一个现象:在业务代码中,有如下两种情况,比如:throw new RuntimeException("xxxxxxxxxxxx"); 事物回滚throw ne ...

  10. redis的安装和类型及基本命令

    一.memcached和redis区别 1. redis 可以存储,memcached用来缓存, 2. 数据类型,memcached只有string:redis有string,链表,哈希结构,集合,有 ...