CodeForces - 697E PLEASE
2018-01-31 00:10
267 查看
E. PLEASE
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's
heart is under the middle cup.
Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts n turns
and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start.
After n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was
distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right.
Number n of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array a1, a2, ..., ak such
that
in other words, n is multiplication of all elements of the given array.
Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction p / q such
that
,
where
is
the greatest common divisor. Since p and q can
be extremely large, you only need to find the remainders of dividing each of them by 109 + 7.
Please note that we want
of p and q to
be 1, not
of
their remainders after dividing by 109 + 7.
Input
The first line of input contains a single integer k (1 ≤ k ≤ 105) —
the number of elements in array Barney gave you.
The second line contains k integers a1, a2, ..., ak (1 ≤ ai ≤ 1018) —
the elements of the array.
Output
In the only line of output print a single string x / y where x is
the remainder of dividing p by 109 + 7 and y is
the remainder of dividing q by 109 + 7.
Examples
input
output
input
output
题目大意:有三个倒扣着的帽子,中间的帽子里藏着钥匙,告诉你这三个帽子经过N次调换,钥匙还在中间的概率,这里
输入第一行为ai的个数,第二行为a1~an。
这题第一眼看上去没思路,先画个图
从图上可以看出,当一个情况钥匙在中间时,他的子情况钥匙必定不在中间。假设上个情况有p的概率钥匙在中间的帽子里,那么有1-p的概率,不在中间,前面说了,在中间的情况的子情况不可能在中间,而不在中间的情况的下一步有两种选择,所以下一种情况在中间的概率是(1-p)/2
设第N次调换后钥匙在中间的概率是an,那么我们得到递推公式:
两边展开,求特征方程,配方,
设
,则
,
那么
即
通分
.
题目要求用p/q表示,那么下面分n为奇数,偶数讨论
如果n为偶数
q =
.
如果n为奇数
q =
下面的问题就是如何计算了,题目要求p和q取模
首先算好算的q,先用快速幂求出
,但是下一步不能直接除以2,因为我们算快速幂时取了模,直接除以2结果就不对了。这里要用逆元来算,这里取得模是个质数,直接费马小定理求逆元.p也是同理,不能直接除以3,也是要用逆元来算
AC代码
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's
heart is under the middle cup.
Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts n turns
and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start.
After n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was
distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right.
Number n of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array a1, a2, ..., ak such
that
in other words, n is multiplication of all elements of the given array.
Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction p / q such
that
,
where
is
the greatest common divisor. Since p and q can
be extremely large, you only need to find the remainders of dividing each of them by 109 + 7.
Please note that we want
of p and q to
be 1, not
of
their remainders after dividing by 109 + 7.
Input
The first line of input contains a single integer k (1 ≤ k ≤ 105) —
the number of elements in array Barney gave you.
The second line contains k integers a1, a2, ..., ak (1 ≤ ai ≤ 1018) —
the elements of the array.
Output
In the only line of output print a single string x / y where x is
the remainder of dividing p by 109 + 7 and y is
the remainder of dividing q by 109 + 7.
Examples
input
1 2
output
1/2
input
3 1 1 1
output
0/1
题目大意:有三个倒扣着的帽子,中间的帽子里藏着钥匙,告诉你这三个帽子经过N次调换,钥匙还在中间的概率,这里
输入第一行为ai的个数,第二行为a1~an。
这题第一眼看上去没思路,先画个图
从图上可以看出,当一个情况钥匙在中间时,他的子情况钥匙必定不在中间。假设上个情况有p的概率钥匙在中间的帽子里,那么有1-p的概率,不在中间,前面说了,在中间的情况的子情况不可能在中间,而不在中间的情况的下一步有两种选择,所以下一种情况在中间的概率是(1-p)/2
设第N次调换后钥匙在中间的概率是an,那么我们得到递推公式:
两边展开,求特征方程,配方,
设
,则
,
那么
即
通分
.
题目要求用p/q表示,那么下面分n为奇数,偶数讨论
如果n为偶数
q =
.
如果n为奇数
q =
下面的问题就是如何计算了,题目要求p和q取模
首先算好算的q,先用快速幂求出
,但是下一步不能直接除以2,因为我们算快速幂时取了模,直接除以2结果就不对了。这里要用逆元来算,这里取得模是个质数,直接费马小定理求逆元.p也是同理,不能直接除以3,也是要用逆元来算
AC代码
#include<bits/stdc++.h> using namespace std; const long long MOD=1000000007; long long pow2(long long desk,long long upper) { long long out=1; while(upper) { if(upper%2==0) { desk=desk*desk%MOD; upper/=2; } else { out=out*desk%MOD; upper--; } } return out; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long cas; cin>>cas; long long L,R=2; int ok=0; while(cas--) { long long tp; cin>>tp; if(tp%2==0)ok=1; R=pow2(R,tp)%MOD; } R=R*pow2(2,MOD-2)%MOD; if(ok) { L=(R+1)*pow2(3,MOD-2)%MOD; } if(!ok) { L=(R-1)*pow2(3,MOD-2)%MOD; } cout<<L<<'/'<<R<<endl; return 0; }
相关文章推荐
- CodeForces 551 D.GukiZ and Binary Operations(dp+矩阵快速幂)
- CodeForces 547A--找循环节
- codeforces contest 358
- CodeForces 137A
- Codeforces 549F Yura and Developers
- codeforces 617E. XOR and Favorite Number (莫队)
- CSU-ACM2017暑期训练12-KMP G - Next[]树 CodeForces - 432D
- Codeforces 161D Distance in Tree
- CodeForces - 357C C - Knight Tournament 并查集or set模拟
- codeforces 330A 330B 329A 分别是7月20DIV2的前三题
- Codeforces 551C GukiZ hates Boxes(二分)
- Codeforces 766D-Mahmoud and a Dictionary
- codeforces [Gym-100814K]
- CodeForces 688A-Opponents
- CodeForces 735D - Taxes
- CodeForces 154B——Colliders——筛选素数,模拟标记
- Codeforces 552E - Vanya and Brackets【表达式求值】
- 【Codeforces 711 D D. Directed Roads】 + DFS
- CodeForces 828D High Road
- CodeForces 166A Rank List