ICPC2017网络赛(西安)B coin (概率计算)

本文探讨了在特定概率条件下,进行多次硬币翻转后正面朝上次数为偶数的概率计算方法。采用矩阵快速幂技术提高计算效率,并通过逆元处理极大数值,确保结果准确性。

Bob has a not even coin, every time he tosses the coin, the probability that the coin’s front face up is qp (qp≤12)
The question is, when Bob tosses the coin kk times, what’s the probability that the frequency of the coin facing up is even number.

If the answer is XY​​ , because the answer could be extremely large, you only need to print(X∗Y−1)mod(109+7).

Input Format

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

Then Each line has 3 integer p,q,k(1≤p,q,k≤107) 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

题目题意:题目给了一种特殊的硬币,其中正面朝上的概率是q/p,我们丢硬币k次,问硬币朝上的次数是偶数的概率为多少 (要 mod 1e9+7)(*逆元)

https://www.jisuanke.com/contest/876/44177
B题,队友用矩阵快速幂的推的公式做的..
还有别人的做法http://blog.youkuaiyun.com/winter2121/article/details/78005036?locationNum=10&fps=1
用的是普通的数学技巧..

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<set>

using namespace std;

const int maxn = 20;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll mod= 1e9+7;

mat mul(mat& A,mat& B){
    mat C(2,vec(2));
    for(int i=0;i<2;i++)
        for(int k=0;k<2;k++)
            for(int j=0;j<2;j++){
            C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
    }
    return C;
}

ll Pow(ll a,ll b){
    ll sum=1;
    while(b){
        if(b&1) sum=sum*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return sum;
}

mat Pow(mat A,ll b){
    mat B(2,vec(2));
    B[0][0]=B[1][1]=1;
    while(b){
        if(b&1) B=mul(A,B);
        b>>=1;
        A=mul(A,A);
    }
    return B;
}

ll inv(ll x){
    return Pow(x,mod-2);    
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,k;
        scanf("%lld%lld%lld",&p,&q,&k);
        ll a=q;
        ll b=p-q;
        mat A(2,vec(2));
        A[0][1]=A[1][0]=a;
        A[0][0]=A[1][1]=b;
        A=Pow(A,k-1);
        ll Ans=(a*A[1][0]+b*A[1][1])%mod;
        ll cnt=Pow(p,k);
        Ans=(Ans*inv(cnt))%mod;
        printf("%lld\n",Ans);
    }
    return 0;
}
ICPC网络ICPC的重要组成部分,为参队伍提供了参与区域选拔的机会。 从程安排来看,第48届(2023年)ICPC的第一次网络选拔于2023年9月17日举行,第二次网络选拔于2023年9月23日举行[^1]。 规则方面,ACM - ICPC以团队形式代表各学校参,每队由至多3名队员组成,每位队员必须是在校学生,有一定年龄限制,并且每年最多可以参加2站区域选拔。比期间,每队使用1台电脑,需在5个小时内使用C/C++、Java和Python中的一种编写程序解决7到13个问题。程序完成后提交评测机运行,运行结果会判定为正确或错误两种并及时通知参队。每队在正确完成一题后,组织者将在其位置上升起一只代表该题颜色的气球,每道题目第一支解决掉它的队还会额外获得一个“FIRST PROBLEM SOLVED”的气球。最后的获胜者为正确解答题目最多且总用时最少的队伍。每道试题用时将从竞开始到试题解答被判定为正确为止,其间每一次提交运行结果被判错误的话将被加罚20分钟时间,未正确解答的试题不记时[^2]。 ```python # 这里可以简单模拟一个ICPC的计分逻辑 def calculate_score(correct_problems, wrong_submissions, total_time): penalty = sum(wrong_submissions) * 20 total_score = sum(correct_problems) total_time_with_penalty = total_time + penalty return total_score, total_time_with_penalty # 示例使用 correct_problems = [1, 1, 1] # 假设解决了3道题 wrong_submissions = [0, 2, 1] # 每道题的错误提交次数 total_time = 240 # 总用时240分钟 score, time = calculate_score(correct_problems, wrong_submissions, total_time) print(f"总得分: {score}, 总用时: {time} 分钟") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值