题意:有3个杯子,排放一行,刚开始钥匙在中间的杯子,每次操作,将左右两边任意一个杯子进行交换,问n次操作后钥匙在中间杯子的概率

分析:考虑动态规划做法,dp[i]代表i次操作后的,钥匙在中间的概率,由于每次操作独立,dp[i]=(1-dp[i-1)/2;

显然,dp[1]=0;

由刚才那个式子可以得出:dp[i]-1/3=(-1/2)*(dp[i-1]-1/3),这是高中数列知识

然后 设dp[i]=p/q; dp[i]=(2^(n-1)+(-1)^n)/(3*2^(n-1))

它要求p/q是最简分数,令p=(2^(n-1)+(-1)^n)/3,通过打表发现,(2^(n-1)+(-1)^n)可以被3整除,而且p是奇数

q是2^(n-1),是偶数,所以p/q等于dp[i],p,q互质,刚好符合题意,只能说这个题出的很好

然后利用费马小定理求2^(n-1),求出3的逆元就好了,大部分都是推到,反而代码很简单

注:因为a[i]是在1到1e18之间的,所以要先取模,这个地方要注意

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5+;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
LL qpow(LL x,LL y){
LL ret=;
while(y){
if(y&)ret=ret*x%mod;
x=x*x%mod;
y>>=;
}return ret;
}
int main(){
int k;
scanf("%d",&k);
bool flag=false;
bool flag1=false;
LL n=;
for(int i=;i<=k;++i){
LL x;scanf("%I64d",&x);
if(x%==)flag=true;
if(x>)flag1=true;
x%=(mod-);
n=n*x%(mod-);
}
if(!flag1){
printf("0/1\n");
return ;
}
n=(n-+mod-)%(mod-);
LL q=qpow(,n);
LL inv3=qpow(,mod-);
LL p=q;
if(flag)p=(p+)%mod;
else p=(p-+mod)%mod;
p=p*inv3%mod;
printf("%I64d/%I64d\n",p,q);
return ;
}

最新文章

  1. Android 中this、getContext()、getApplicationContext()、getApplication()、getBaseContext() 之间的区别
  2. 如何修改SharePoint列表条数等阈值
  3. app缓存设计-文件缓存
  4. ScalaTour-1.基础
  5. UVa 10801 - Lift Hopping(dijkstra最短路)
  6. 值得珍藏的.NET源码,不保存就没机会了
  7. js字符串长度计算(一个汉字==两个字符)和字符串截取
  8. Hamming code
  9. linux 原生系统发送电子邮件 (在本地与因特网)
  10. ●BZOJ 1692 [Usaco2007 Dec]队列变换
  11. [Swift]LeetCode735. 行星碰撞 | Asteroid Collision
  12. 原生Ajax--XmlHttpRequest对象和jQuery.ajax()
  13. nodejs EventEmitter 发送消息
  14. Pytorch 之 backward
  15. js中的事件轮询(event loop)机制
  16. OSG添加回调更新
  17. Scala的控制结构和函数
  18. 读书笔记 effective c++ Item 52 如果你实现了placement new,你也要实现placement delete
  19. [css]单/多行居中&amp;字体设置
  20. git hooks 的学习使用

热门文章

  1. 荣耀3X畅玩版狙击红米note!
  2. 朴素贝叶斯方法(Naive Bayes Method)
  3. 【mysql的编程专题】触发器
  4. Android:查看应用创建的数据库
  5. 卷积神经网络Convolutional Neural Networks
  6. 数组 寻找最大的第k个数
  7. 从输入 URL 到页面加载完的过程中都发生了什么---优化
  8. bash: ./device/nexell/tools/build.sh: 权限不够
  9. Storm集群的搭建
  10. Android开发之单例模式