codeforces 696C C. PLEASE(概率+快速幂)
题目链接:
1 second
256 megabytes
standard input
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, ..., aksuch 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 10^9 + 7.
Please note that we want
of p and q to be 1, not
of their remainders after dividing by 109 + 7.
The first line of input contains a single integer k (1 ≤ k ≤ 10^5) — the number of elements in array Barney gave you.
The second line contains k integers a1, a2, ..., ak (1 ≤ ai ≤ 10^18) — the elements of the array.
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 qby 109 + 7.
1
2
1/2
3
1 1 1
0/1 题意: 三个杯子,物品一开始在中间的杯子里,在n此交换后,问物品在中间的杯子里的概率; 思路: 可以先求出概率的表达式,可以得到一个表达式2*dp[n]+dp[n-1]=1;最后就是一个等比数列的和dp[n]=(2^(n-1)+(-1)^n)/(3*2^(n-1));
再看一下分子是否是3的倍数;然后就是快速幂对这个式子求结果了,中间要用费马小定理哟; AC代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e6+10;
const int maxn=3e6;
const double eps=1e-10; int n;
LL a[N]; LL pow_mod(LL x,LL y,LL mod)
{
LL s=1,base=x;
while(y)
{
if(y&1)s=s*base%mod;
base=base*base%mod;
y>>=1;
}
return s;
} int main()
{
read(n);
For(i,1,n)read(a[i]);
LL temp=1;
For(i,1,n)temp=a[i]%2*temp%2;
int flag=0;
if((pow_mod(2,(temp+1)%2,3)+pow_mod(-1,temp,3))%3==0)flag=1;
LL p=1,q=1;
temp=1;
For(i,1,n)temp=a[i]%(mod-1)*temp%(mod-1);
p=pow_mod(2,(temp-1+mod-1)%(mod-1),mod)+pow_mod(-1,temp,mod);
if(flag)p=pow_mod(3,mod-2,mod)*p%mod;
q=pow_mod(2,(temp-1+mod-1)%(mod-1),mod);
if(!flag)q=3*q%mod;
cout<<p<<"/"<<q<<endl;
return 0;
}
codeforces 696C C. PLEASE(概率+快速幂)的更多相关文章
- codeforces magic five --快速幂模
题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...
- CodeForces - 691E Xor-sequences 【矩阵快速幂】
题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...
- Codeforces 963 A. Alternating Sum(快速幂,逆元)
Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...
- Codeforces 691E题解 DP+矩阵快速幂
题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...
- 【codeforces 623E】dp+FFT+快速幂
题目大意:用$[1,2^k-1]$之间的证书构造一个长度为$n$的序列$a_i$,令$b_i=a_1\ or\ a_2\ or\ ...\ or a_i$,问使得b序列严格递增的方案数,答案对$10^ ...
- Codeforces 691E Xor-sequences(矩阵快速幂)
You are given n integers a1, a2, ..., an. A sequence of integers x1, x2, ..., xk is called a & ...
- Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check
A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...
- Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)
Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...
- Codeforces 446D - DZY Loves Games(高斯消元+期望 DP+矩阵快速幂)
Codeforces 题目传送门 & 洛谷题目传送门 神仙题,%%% 首先考虑所有格子都是陷阱格的情况,那显然就是一个矩阵快速幂,具体来说,设 \(f_{i,j}\) 表示走了 \(i\) 步 ...
随机推荐
- 安装ubuntu 12.04 后遇到的问题
我的笔记本是08年的戴尔,比较老的机子了.给本本安装ubuntu/windows8双系统后,ubuntu系统中出现了一些问题,在网上搜寻许多解决方法,管用的就分享一下,顺便做一下记录,免得下次自己又不 ...
- delphi函数大全
delphi函数大全Abort 函数 引起放弃的意外处理Abs 函数 绝对值函数AddExitProc ...
- 【TFS】TFS2015链接TFS出现TF31002/TF400324问题解决方案
安装VS2015后链接TFS发现出现TF31002错误,然后用浏览器打开TFS URL能正常访问,在TFS online中点击用vs打开按钮,提示TF400324错误 1. VS2015中打开: 2. ...
- linux命令stat,查看文件详细信息
可以查看文件的各类具体信息:文件权限的数字形式0664:uid.gid的权限的数字形式等 更多用法参考stat --help lsattr test.sh 查看文件的其他属性:只读属性.只可以追加写属 ...
- 使用azure send grid发送email
1. create a send grid account 2. remember the username/password of the send grid account watermark/2 ...
- RESTful Web Service
- Effective C++ 条款八 别让异常逃离析构函数
class DBConn //这个class用来管理DBConnction对象 { public: //自己设计一个新的DBConn接口 方法3 void close() { db.close() ...
- 汉澳sinox不受openssl心血漏洞影响并分析修复其漏洞代码
OpenSSL 心血(HeartBleed)漏洞 是openssl 在 2014-04-07 发布的重大安全漏洞(CVE-2014-0160)这个漏洞使攻击者可以从server内存中读取64 KB的数 ...
- Neutron配置Floating IP
下图是由一个虚拟机vm1,一个路由器ext-router,两个网络ext-net及demo-net组成的拓扑结构.要达到的目的很简单,即vm1不仅能使用私网IP和内部的其它虚拟机进行通信,还可以通过e ...
- ZOJ 3874 Permutation Graph 分治NTT
Permutation Graph Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has a permutation {a1, a2 ...