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; }
    心如花木,向阳而生。
  • 相关阅读:
    两款命令行下的翻译工具: sdcv & translateshell
    谷歌语言标准中,C++成绝对的佼佼者 狼人:
    12个优秀的云计算操作系统 狼人:
    幻灯片在网页设计中应用的21个优秀案例 狼人:
    对Web开发人员有用的8个网站 狼人:
    网页设计师应向肖像画家吸取的11个理念 狼人:
    揭秘Facebook的系统架构 狼人:
    Chrome或取代Firefox成Ubuntu默认浏览器 狼人:
    Gnome 3.2 发布计划及新功能 狼人:
    MyFaces Core v2.0.7/2.1.1 发布,JSF框架 狼人:
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9150692.html
Copyright © 2011-2022 走看看