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; }
    心如花木,向阳而生。
  • 相关阅读:
    Kubenetes环境搭建笔记
    Python+Robot Framework实现UDS诊断自动化测试
    Python实现CAN总线J1939报文接收、发送
    [转载]从SQL 2008 复制数据及对像到SQL 2000 的方法
    推荐移动应用:群落(Groupcells)——全球第一款基于图片组的近场社交电子商务平台
    [缓存]迅雷下载原理
    HP大中华区总裁孙振耀退休感言
    [缓存]HTTP协议中的TranferEncoding:chunked编码解析
    [转载]SQL 2008到2005和2000版本的转换
    [学习]SVM入门(一)
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9150692.html
Copyright © 2011-2022 走看看