Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up is \frac{q}{p}(\frac{q}{p} \le \frac{1}{2})​p​​q​​(​p​​q​​≤​2​​1​​).

The question is, when Bob tosses the coin kktimes, what's the probability that the frequency of the coin facing up is even number.

If the answer is \frac{X}{Y}​Y​​X​​, because the answer could be extremely large, you only need to print (X * Y^{-1}) \mod (10^9+7)(X∗Y​−1​​)mod(10​9​​+7).

Input Format

First line an integer TT, indicates the number of test cases (T \le 100T≤100).

Then Each line has 33 integer p,q,k(1\le p,q,k \le 10^7)p,q,k(1≤p,q,k≤10​7​​) indicates the i-th test case.

Output Format

For each test case, print an integer in a single line indicates the answer.

样例输入

2
2 1 1
3 1 2

样例输出

500000004
555555560
题意:给定n,m,k,每次抛一个硬币为正面朝上的概率为(m/n),求抛k次硬币之后,硬币朝上的概率为多少(这里输出的是逆元之后的结果,所以很大)
题解:我们把概率转换为事件,一共n^k次事件,我们每扔一次硬币就可以看作有n次操作,m次为硬币朝上,n-m次硬币朝下。
我们设b(n)为抛n次硬币之后,硬币朝上次数为奇数的操作次数;a(n)为抛k次硬币之后,硬币朝上次数为偶数的操作次数。那么有递推式子
b(n)=b(n-1) * (n-m)+ a(n-1) * m;
a(n)=a(n-1) * (n-m)+ b(n-1) * m;
这个构造矩阵就容易多了
ac代码:
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
struct Martix
{
ll mp[][];
int r,c;
};
ll exgcd(ll a,ll b,ll &x,ll &y)// 扩展欧几里得
{
if(b==)
{
x=;
y=;
return a;
}
ll temp=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return temp;
}
ll finv(ll a,ll m)// 求逆元
{
ll x,y;
ll g=exgcd(a,m,x,y);
x=(x%m+m)%m;//
return x;
}
long long quickmod(long long a,long long b,long long m)
{
long long ans = ;
while(b)//用一个循环从右到左便利b的所有二进制位
{
if(b&)//判断此时b[i]的二进制位是否为1
{
ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m
b--;//把该为变0
}
b/=;
a = a*a%m;
}
return ans;
}
Martix mul(Martix a,Martix b)
{
int r=a.r;
int c=b.c;
Martix temp;
temp.r=r;
temp.c=c;
for(int i=;i<r;i++)
{
for(int j=;j<c;j++)
{
temp.mp[i][j]=;
for(int k=;k<r;k++)
{
temp.mp[i][j]=(a.mp[i][k]*b.mp[k][j]+temp.mp[i][j])%mod;
}
}
}
return temp;
}
ll n,m;
ll pow(Martix a,ll k)
{
Martix ans;
ans.r=;
ans.c=;
ans.mp[][]=n-m;//
ans.mp[][]=m;//
while(k)
{
if(k&) ans=mul(a,ans);
k/=;
a=mul(a,a);
}
return ans.mp[][]%mod;
} int main()
{
int t;
ll k;
scanf("%d",&t);
while(t--)
{
scanf("%lld %lld %lld",&n,&m,&k);
ll y=quickmod(n,k,mod);
Martix a;
a.r=a.c=;
a.mp[][]=a.mp[][]=n-m;
a.mp[][]=a.mp[][]=m;
ll key=pow(a,k-);
//cout<<key<<endl;
ll temp=key*finv(y,mod)%mod;
cout<<temp<<endl;
}
return ;
}

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 矩阵快速幂的更多相关文章

  1. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  2. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

  3. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  4. 【UOJ#340】【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划)

    [UOJ#340][清华集训2017]小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划) 题面 UOJ 洛谷 题解 考虑如何暴力\(dp\). 设\(f[i][a][b][c]\)表示当前到了第\(i\) ...

  5. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  6. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  7. 华东交通大学2018年ACM“双基”程序设计竞赛 C. 公式题 (2) (矩阵快速幂)

    题目链接:公式题 (2) 比赛链接:华东交通大学2018年ACM"双基"程序设计竞赛 题目描述 令f(n)=2f(n-1)+3f(n-2)+n,f(1)=1,f(2)=2 令g(n ...

  8. 2017 ACM/ICPC(西安)赛后总结

    早上8:00的高铁,所以不得不6点前起床,向火车站赶……到达西安后已经是中午,西工大距离西安北站大概3小时车程的距离,只好先解决午饭再赶路了……下午3.30的热身赛,一行人在3.35左右赶到了赛场,坐 ...

  9. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

随机推荐

  1. 通过AS提交AndroidLibrary到JCenter仓库

    注意事项: //版本需要一致,如下版本对应gradle-4.4-all.zip dependencies { classpath 'com.android.tools.build:gradle:3.1 ...

  2. js vue --- T Z 去掉 T Z 时间

    export const formatDate = (timestamp) => { return timestamp.replace(/T/g,' ').replace(/Z/g,'') } ...

  3. python 设计模式之备忘录模式

    1.为什么用备忘录模式 假设大战僵尸游戏共10关,越是往后关卡越难,越难就越是费时间费钱费精力. 开始大战僵尸,玩了好久好久终于玩到了第9关,真是不容易. 这个时候开始玩第9关了,哇,好难啊,真不幸, ...

  4. Android下拉涮新第三方通用控件

    Android下拉涮新第三方通用控件https://github.com/chrisbanes/Android-PullToRefresh Pull To Refresh Views for Andr ...

  5. android中SpannableString之富文本显示效果

    SpannableString其实和String一样,都是一种字符串类型,SpannableString可以直接作为TextView的显示文本,不同的是SpannableString可以通过使用其方法 ...

  6. ubuntu 12.04 nfs-server/client安装配置

    由于opennebula的共享存储的方式需要nfs,为了opennebula 3.8在ubuntu 12.04上搭建做铺垫,先介绍下nfs server和client端的安装和配置.   1. nfs ...

  7. Maven 打war包

    使用maven将项目达成war包 右击项目的跟项目,选择run as 选择maven  build.... 进入窗口,在 Goals  输入命令   clean package,选择 skip Tes ...

  8. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  9. 开始学习Docker啦--容器理论知识(一)

    目录 一.容器核心技术 1.容器规范 2.容器 runtime 3.容器管理工具 4.容器定义工具 5.Registry 6.容器 OS 二.说说容器 1.什么是容器 Containers vs. v ...

  10. 【ARM-Linux开发】Rico Board DIY系列实验教程 Day 2——搭建Boa服务器

    一:BOA WebServer简介 BOA WebServer是一款单任务的HTTP服务器,与其他网页服务器不同之处,是当有连接请求到来是,它既不是为每个连接都单独创建进程,也不是采用复制自身进程处理 ...