Codeforces 935D Fafa and Ancient Alphabet
题目链接
题意
给定两个\(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的更多相关文章
- 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)
传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi表示第i ni~ ni n位第一个 ...
- Codeforces 935E Fafa and Ancient Mathematics dp
Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...
- Codeforces 935E Fafa and Ancient Mathematics(表达式转树 + 树型DP)
题目链接 Codeforces Round #465 (Div. 2) Problem E 题意 给定一个表达式,然后用$P$个加号和$M$个减号填充所有的问号(保证问号个数等于$P + M$) ...
- CodeForces 935E Fafa and Ancient Mathematics (树形DP)
题意:给定一个表达式,然后让你添加 n 个加号,m 个减号,使得表达式的值最大. 析:首先先要建立一个表达式树,这个应该很好建立,就不说了,dp[u][i][0] 表示 u 这个部分表达式,添加 i ...
- codeforce465DIV2——D. Fafa and Ancient Alphabet
概率的计算答案给出的这张图很清楚了,然后因为要求取模,a/b%M=a*b^-1%M=a*inv(b,M)%M; #include <cstdio> #include <cstring ...
- 【学术篇】CF935E Fafa and Ancient Mathematics 树形dp
前言 这是一道cf的比赛题.. 比赛的时候C题因为自己加了一个很显然不对的特判WA了7次但找不出原因就弃疗了... 然后就想划水, 但是只做了AB又不太好... 估计rating会掉惨 (然而事实证明 ...
- 2019暑训第一场训练赛 |(2016-icpc区域赛)部分题解
// 今天下午比赛自闭了,晚上补了题,把AC的部分水题整理一下,记录坑点并吸取教训. // CF补题链接:http://codeforces.com/gym/101291 A - Alphabet 题 ...
- [codeforces 260]B. Ancient Prophesy
[codeforces 260]B. Ancient Prophesy 试题描述 A recently found Ancient Prophesy is believed to contain th ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
随机推荐
- Visual Studio 2017 初次体验
在初次体验中遇到以下问题以及技巧 1. 在出现红色波浪线时为出现错误语法,将鼠标移动到相应位置可以获得相关错误信息 2.在编写代码过程中,行号上出现的小黄灯可以有提示信息 3.List 与 Array ...
- django+xadmin在线教育平台(二)
老话总是没错的,工欲善其事,必先利其器 教你安装pycharm,mysql,navicat,python相关环境. windows下搭建开发环境 2-1 pycharm,mysql,Navicat安装 ...
- php - 去除php代码中的多余空格
<?php class Test{ public function test(){ $tmplContent = file_get_contents('./test.php'); $tmplCo ...
- spring boot 设置tomcat post参数限制
今天传图片,用的base64字符串,POST方法,前端传送的时候总是莫名其妙的崩溃,去网上搜了半天,以为是文件大小被限制了,但是我这个是字符串接收,不是文件接收,于是又继续搜,原来post本身没有参数 ...
- Java Web 基础(一) 基于TCP的Socket网络编程
一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...
- linux 查看CPU内存 网络 流量 磁盘 IO
使用vmstat命令来察看系统资源情况 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? Q: 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? A: 在命 ...
- time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- 小知识(h5 js )
1.如果都为NaN但是他们不相等var a=NaN;var b=NaN;a==b //flase2.javascript 是一种脚本语言,可以创建服务器端和客户端的脚本3.javascript 中有两 ...
- dcpromo(server2012不支持)
dcpromo 编辑 dcpromo命令是一个“开关”命令.如果Windows 2000 Server计算机是成员服务器,则 运行dcpromo命令会安装活动目录,将其升级为域控制器:如果Window ...
- 基于web自动化测试框架的设计与开发(本科论文word)