题目链接

题意

给定两个\(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. 十三、MySQL WHERE 子句

    MySQL WHERE 子句 我们知道从 MySQL 表中使用 SQL SELECT 语句来读取数据. 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中. 语法 以下是 ...

  2. Python_常用模块

    一.内置模块 定义:其实模块简单说就是一堆代码实现某个功能,它们是已经写好的.py文件.只需要用import应用即可. 分类: 1. 自定义模块,就是自己写的.py文件为了实现某个功能. 2. 内置标 ...

  3. 可怕的万圣节 Linux 命令

    虽然现在不是万圣节,也可以关注一下 Linux 可怕的一面.什么命令可能会显示鬼.巫婆和僵尸的图像?哪个会鼓励"不给糖果就捣蛋"的精神? crypt 好吧,我们一直看到 crypt ...

  4. thinkphp 3.2.3 - App.class.php 解析

    class App { public static function init() { load_ext_file(COMMON_PATH); // { // /home/www/www.domain ...

  5. day23-python之日志 re模块

    1.logging import logging #-----------------------------------logging.basicConfig logging.basicConfig ...

  6. notification 使用的基本方法

    当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...

  7. 连续小波变换(CWT)

    整理下时频分析变换的方法,遇见好的文章就记录下来了,本篇博客参考知乎https://www.zhihu.com/topic/19621077/top-answers上的一个回答,自己手敲一遍,增强记忆 ...

  8. js 实现5秒倒计时后跳转页面

    <script type="text/javascript"> function countDown(secs, surl) { var jumpTo = docume ...

  9. laravel5.2总结--响应

      1 基本响应 1.1 返回一个字符串,指定的字符串会被框架自动转换成 HTTP 响应. Route::get('/', function () { return 'Hello World'; }) ...

  10. 【Best Time to Buy and Sell Stock】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...