CF697E && CF696C PLEASE
题意:给你三个杯子,一开始钥匙放在中间的杯子里,然后每一回合等概率将左右两个杯子中的一个与中间杯子交换。求n回合之后钥匙在中间杯子的概率。这里要求概率以分数形式输出,先化成最简,然后对1e9 + 7取模。
题解:首先我们可以轻易得到一个递推式:$ d[i] = \frac{{1 - d[i - 1]}}{2} $
但递推式是不行的,我们要得到一个封闭形式。
运用数列技巧,我们可以进行如下变换:$d[i] - \frac{1}{3} = - \frac{1}{2}(d[i - 1] - \frac{1}{3})$
那么我们有 $d[n] = \frac{{{{( - 1)}^n} + {2^{n - 1}}}}{{3 \times {2^{n - 1}}}}$
其中我们发现,${{{( - 1)}^n} + {2^{n - 1}}}$ 一定是3的倍数,且商一定是奇数,所以与剩下部分互质
也即 $p = \frac{{{{( - 1)}^n} + {2^{n - 1}}}}{3}$ $q = {2^{n - 1}}$
带进去算就好了。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mod 1000000007 inline LL read() {
LL x = , f = ; char a = getchar();
while(a < '' || a > '') { if(a == '-') f = -; a = getchar(); }
while(a >= '' && a <= '') x = x * + a - '', a = getchar();
return x * f;
} int n, p = , q, f = ; inline int fpow(int x, LL k) {
int ret = ;
while(k) {
if(k & ) ret = 1LL * ret * x % mod;
k /= ; x = 1LL * x * x % mod;
}
return ret;
} int main() {
n = read();
LL tmp; int inv2 = fpow(, mod -), inv3 = fpow(, mod - );
for(int i = ; i <= n; i++) {
tmp = read();
f = 1LL * f * tmp % ;
p = fpow(p, tmp);
}
q = 1LL * p * inv2 % mod;
p = 1LL * (q + (f ? - : )) * inv3 % mod;
printf("%d/%d\n" ,p ,q);
return ;
}
CF697E && CF696C PLEASE的更多相关文章
- CF696C PLEASE
矩阵快速幂+扩展欧拉定理 对于一个矩阵\(A\),我们有\(A^n \equiv A^{n\% \phi(m)+\phi(m)}(\%m)\) 经过简单的列举或推导可得 设目前进行了\(x\)轮,\( ...
随机推荐
- hibernate 懒加载图解
- Splay_Tree 模板(区间修改,旋转操作)
1.旋转操作 #define MAXN 100100 bool Add[MAXN];//延迟标记 struct Splay_Tree { int cnt, rt;//cnt为节点数,rt == roo ...
- <2013 12 17> 专业技能
Specialties: • Mechanical design modeling using Pro/ENGINEER and SolidWorks.• Robot control, path pl ...
- 任务05—学习 MARKDOWN 语言
我的简历地址: https://github.com/jinxiaohang/MyResume/blob/master/ForJavaJob.md 本任务主要目的掌握markdown. 1.首先是工具 ...
- 分界线<hr/>
<hr align="center" noshade="noshade" width="90px" color="#1DAB ...
- JdbcUtils 小工具
// 第一版 // src 目录下 dbconfig.properties 配置文件, 用来配置四大参数 // 注意 properties 配置文件中没有分号结尾, 也没有引号 driverClass ...
- js内置数据类型
JS 中分为七种内置类型,七种内置类型又分为两大类型:基本类型和对象(Object). 基本类型有六种: number , string , boolean , null , undefined , ...
- python面试题(八)
1 Python中如何使用线程池和进程池? 需要注意一下 不能无限的开进程,不能无限的开线程 最常用的就是开进程池,开线程池.其中回调函数非常重要 回调函数其实可以作为一种编程思想,谁好了谁就去掉 只 ...
- DOM 查找标签
1.直接查找 document.getElementById // 根据ID获取一个标签 document.getElementsByClassName //根据class属性获取 document. ...
- Linux用户相关文件之组文件
组信息文件: 1.文件地址: /etc/group -rw-r--r--. 1 root root 492 10月 6 21:56 /etc/group 2.文件内容: xiaol:x:500: 3. ...