题目链接

题意

给定两个\(n\)位的\(m\)进制数\(s1,s2\),所有出现的\(0\)均可等概率地被其他数字替换,求\(s1\gt s2\)的概率。

思路

从高位到低位,根据每一位上相应的\(0\)的个数进行 分类讨论

计算每一位的时候加上这样一部分答案:比到该位恰能比出大小的情况数。

恰能比出大小意味着:高位全部相同,该位\(s1\gt s2\),低位随便怎么取。

因此,需对两个数目进行记录:1. 前面有多少位是两者皆0;2. 后面还有多少个0没有确定。

另:\(x\)关于\(mod\)的乘法逆元为\(x^{(mod-2)}\),由费马小定理易得。

注意:要对\(m\)的幂次进行预处理。

Code

#include <bits/stdc++.h>
#define maxn 100010
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
int a[maxn], b[maxn];
LL rec[maxn*2];
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b&1) (ret *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ret;
}
LL f(LL p, LL q) {
return p * poww(q, mod-2) % mod;
}
LL GCD(LL a, LL b) { return b ? GCD(b, a%b) : a; }
int main() {
int n, m;
scanf("%d%d", &n, &m);
LL NUM = (1LL*m*m%mod-m+mod)%mod * poww(2, mod-2) % mod;
int tot=0;
F(i, 0, n) { scanf("%d", &a[i]); if (!a[i]) ++tot; }
F(i, 0, n) { scanf("%d", &b[i]); if (!b[i]) ++tot; }
rec[0] = 1;
F2(i, 1, tot) rec[i] = rec[i-1]*m%mod;
LL q = poww(m, tot), p=0;
int cnt=0, prev=0;
F(i, 0, n) {
if (a[i]&&b[i]) {
if (a[i]>b[i]) (p += rec[cnt+tot-prev]) %= mod;
if (a[i]!=b[i]) { printf("%I64d\n", f(p, q)); return 0; }
}
else if (!a[i] && !b[i]) {
prev += 2;
(p += (rec[cnt+tot-prev] * NUM % mod)) %= mod;
++cnt;
}
else {
++prev;
if (a[i]) (p += rec[cnt+tot-prev] * (a[i]-1) % mod) %= mod;
else (p += (rec[cnt+tot-prev] * (m-b[i]) % mod)) %= mod;
}
}
LL gcd = GCD(p, q);
p /= gcd, q /= gcd;
printf("%I64d\n", f(p, q));
return 0;
}

Codeforces 935D 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 935E Fafa and Ancient Mathematics dp

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

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

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

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

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

  5. codeforce465DIV2——D. Fafa and Ancient Alphabet

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

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

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

  7. 2019暑训第一场训练赛 |(2016-icpc区域赛)部分题解

    // 今天下午比赛自闭了,晚上补了题,把AC的部分水题整理一下,记录坑点并吸取教训. // CF补题链接:http://codeforces.com/gym/101291 A - Alphabet 题 ...

  8. [codeforces 260]B. Ancient Prophesy

    [codeforces 260]B. Ancient Prophesy 试题描述 A recently found Ancient Prophesy is believed to contain th ...

  9. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

随机推荐

  1. opengl 学习的链接,以后需要可以再来查需要的

    记录一些好的opengl学习站点,以供日后查阅: modern opengl tutorial : 一个英国的opengl学习网站 上面网站的中文版 日后发现新的再更新

  2. 原生js替换jQuery各种方法-中文版

    原文https://github.com/nefe/You-D... 原生JS与jQuery操作DOM对比 You Don't Need jQuery 前端发展很快,现代浏览器原生 API 已经足够好 ...

  3. HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Oth ...

  4. redis配置密码 redis常用命令

    redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy   #requi ...

  5. mysql中外联和 is null 结合使用

    今天学习mysql ,碰到了一个问题:有部门表,员工表,员工表中有一个部门表的外键,查询没有员工的部门名称. 表结构如下: 员工表employees: 部门表department表: 题目很简单呢,信 ...

  6. easyui-combogrid匹配查询

    用到easyui-combogrid,数据比较少的情况,可以一页就显示完毕,然后直接下拉选择.但是对于数据量比较大的情况,一页显示全部显然不合适,好在从easyui-combogrid的数据加载方式可 ...

  7. 创建数据收集器集(DSC)

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 按类别 ...

  8. C 语言 习题 1-10

    练习 1-10 编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替按为\\.这样可以将制表符和回退符以可见的方式显示出来. #include<stdio. ...

  9. C++ STL map容器的说明测试1

    // maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...

  10. CentOS7 'Username' is not in the sudoers file. This incident will be reported

    新装的 CentOS 需要安装许多软件,但是如果一开始你不是以 root 登入的话,就需要使用 sudo 进行切换,但是通常会报错如下图: 解决方法: 先用 root 用户登入系统, 打开文件 vi ...