Codeforces #362(Div.2)-> E.PLEASE(快速幂+费马小定理)
来源:互联网 发布:如何用python做贪吃蛇 编辑:程序博客网 时间:2024/05/18 12:30
传送门:点击打开链接 (数论)
题意:
三个杯子,硬币一开始在中间的杯子里,每次操作可能是左边和中间或右边和中间交换,问n次操作后,硬币在中间的概率
思路:
设
展开化简得到可以得到
显然
互质,所以不用约分了,幂的操作可以用快速幂来处理,再用费马小定理转除为乘,这题就搞定了
#include <cstdio>#include <iostream>using namespace std;const int maxn = 1e5+10;const int mod = 1e9+7;typedef long long ll;ll num;ll pow_mod(ll x, ll n){ ll res=1; while(n>0){ if(n&1)res=res*x%mod; x=x*x%mod; n>>=1; } return res;}int main (){ int n; cin >>n; ll b = 2; ll flag = -1; for(int i = 0 ; i < n ; i ++){ scanf("%I64d",&num); b = pow_mod(b,num); if(num %2 == 0) flag = 1; } b = b*pow_mod(2LL,mod-2)%mod; // b = 2^(n-1)=2^n/2 ll a = (b+flag+mod)%mod*pow_mod(3LL,mod-2)%mod; //a = (2^(n-1) -or+ 1) / 3 printf("%I64d/%I64d",a,b); return 0;}注:一道经典的数论题,费马小定理求逆元就用这道题来练手了
另附:费马小定理求逆元
0 0
- Codeforces #362(Div.2)-> E.PLEASE(快速幂+费马小定理)
- 【Codeforces Round 362 (Div 2)E】【公式推导+快速幂+费马小定理】PLEASE a[i]=(1-a[i-1])除2下n次项 n为连乘数
- Codeforces Round #362 (Div. 2) E. PLEASE
- Codeforces 696C. PLEASE(快速幂+费马小定理)
- Codeforces Round #362 (Div. 2) E. PLEASE(数论 + 递推)
- Codeforces Round #334 (Div. 2) E(抽屉原理+逆元+费马小定理 )
- Educational Codeforces Round 33 E Counting Arrays(欧拉素筛+费马小定理+快速幂)
- codeforces #373(div 2) E (线段树+矩阵快速幂)
- Codeforces Round #226 (Div. 2) E(矩阵快速幂)
- Codeforces Round #338 (Div. 2) D Multipliers(费马小定理,快速幂)
- Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(dp + 矩阵快速幂)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(DP+矩阵快速幂)
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩阵快速幂)
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩阵快速幂)
- Codeforces #369 (Div. 2) E. ZS and The Birthday Paradox (勒让德定理+逆元)
- Codeforces Round #309 (Div. 2) E DFS 染色 + 快速乘
- Codeforces Round #291 (Div. 2)E. Darth Vader and Tree——dp+矩阵快速幂
- keepalived 个人解析
- HDU1046 POJ1450 UVALive2334 Gridland
- 2016--07--23
- AtCoder Regular Contest 058 C (水题)
- ---Jsp(六)标签
- Codeforces #362(Div.2)-> E.PLEASE(快速幂+费马小定理)
- JDNI配置数据库连接池
- Codeforces-B.Soldier and Badges - Codeforces Round #304 (Div. 2)
- JAVA集合体系回顾
- JAVA面向对象-----抽象类
- HDU3507 Print Article
- PAT (Basic Level) Practise-1016
- 四大组件之Activity
- 初窥渗透--IIS6.0解析漏洞