zoukankan      html  css  js  c++  java
  • Codeforces New Year and Arbitrary Arrangement

    New Year and Arbitrary Arrangement

    time limit per test2 seconds

    You are given three integers k, pa and pb.

    You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.

    You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and . Print the value of .


    大概就是说给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符串末尾+a,有pb/(pa+pb)的可能在字符串末尾+b,求加到组成至少k对子序列“ab"时的期望子序列“ab”数。k<=1000,pa,pb<=10^6。

    (连markdown我都懒得补在原文上了233)


    Input

    The first line will contain three integers integer k, pa, pb (1 ≤ k ≤ 1 000, 1 ≤ pa, pb ≤ 1 000 000).

    Output

    Print a single integer, the answer to the problem.

    Examples

    input
    1 1 1

    output

    2

    input

    3 1 4

    output

    370000006

    Note

    The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.

    The expected amount of times that 'ab' will occur across all valid sequences is 2.

    For the second sample, the answer is equal to .


    emmm.... 期望要倒推。。。。正无穷啥的拿等比数列消去。。。( $A^{inf}=0 (0 #include<bits/stdc++.h> using namespace std; const int maxn = 1e3 + 3, mod = 1e9 + 7; long long k, pa, pb, dp[maxn][maxn]; bool flag[maxn][maxn]; long long inv(int t) { int lin = mod - 2; long long tmp = t, ret = 1; while(lin){ if(lin & 1) ret = ret * tmp % mod; tmp = tmp * tmp % mod; lin >>= 1; } return ret; } long long workk(int a, int b) { if(flag[a][b]) return dp[a][b]; if(a + b >= k){ dp[a][b] = a + b + (pa * inv(pb) % mod); flag[a][b] = true; return dp[a][b]; } dp[a][b] = ((pa * inv(pa + pb) % mod) * workk(a + 1, b) % mod + (pb * inv(pa + pb) % mod) * workk(a, a + b) % mod) % mod; flag[a][b] = true; return dp[a][b]; } int main() { scanf("%I64d%I64d%I64d", &k, &pa, &pb); printf("%I64d", workk(1, 0)); return 0; }
    心如花木,向阳而生。
  • 相关阅读:
    20201220第二周学习总结
    师生关系
    快速浏览教材
    学期2020-2021-1学号20201220《信息安全专业导论》第1周学习总结
    编程将字符串s倒序输出,要求利用函数递归实现
    小学生四则运算随机生成程序
    礼炮问题
    C语言最大公约数
    C语言判断三角形类型
    C语言:一元二次方程解的所有情况
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9150692.html
Copyright © 2011-2022 走看看