题目:三个杯子,物品一开始在中间的杯子里,在n此交换后,问物品在中间的杯子里的概率
设Pn是n轮操作后中间杯子有钥匙的概率,则第n-1轮的时候,中间杯子里必须没钥匙,且有1/2的概率选到边上那个有钥匙的杯子。故:Pn=(1−Pn−1)/2
我们可以用快速幂取模算法求出2^n mod (1e9+7),那么x可以通过减1再乘以3的乘法逆元求得,y可以通过乘以2的乘法逆元求得
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MOD 1000000007
#define MAX 100009
#define ll long long
#define rep(i, j, k) for(int i = j; i <= k; i++)
using namespace std;
ll a[MAX];
int n, num, den, even = 0, mul = 1;
int pow (int x, int y)
{
if (!y)
return 1;
int ret = pow (x, y / 2);
if (y % 2)
return (1LL * ret * ret * x) % MOD;
else
return (1LL * ret * ret) % MOD;
}
int main()
{
cin >> n;
rep (i, 0, n - 1)
{
cin >> a[i];
if (a[i] % 2 == 0)
even = 1;
mul = 1LL * mul * (a[i] % (MOD - 1)) % (MOD - 1);
}
mul = (mul - 1 + (MOD - 1)) % (MOD - 1);
num = pow(2, mul);
den = num;
if (even)
num++;
else
num--;
num = 1LL * num * pow(3, MOD - 2) % MOD;
cout << num << "/" << den << "\n";
return 0;
}