Codeforces gym 101343 A 数论

来源:互联网 发布:对人工智能的看法 编辑:程序博客网 时间:2024/05/14 00:18

On The Way to Lucky Plaza
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alaa is on her last day in Singapore, she wants to buy some presents to her family and friends. Alaa knows that the best present in the world will be a chocolate plate for each one of her family members and friends.

Alaa goes to Lucky Plaza shopping mall in Orchard road in order to find all chocolate she needs. Lucky Plaza is a big mall and have many shops that sell chocolate.

On the entrance of Lucky Plaza Alaa wondered if she wants to buy k chocolate plates, what is the probability that she will buy the kthchocolate plate from the nth shop she will visit, knowing that she can visit each shop at most one time. Also she can buy at most one chocolate plate from each shop, and the probability to do that is p. (This probability is the same for all shops in Lucky Plaza)

Alaa wants to finish her mission as soon as possible, so she starts visiting the shops, also she asked you to calculate the answer of her hard question. Can you?

Input

The first line contains three integers mnk and real number p (1  ≤  mnk  ≤  105) (0  ≤  p  ≤  1), where m is the number of shops that sell chocolate in Lucky Plaza, n is the number of shops Alaa will visit, k is the number of chocolate plates Alaa wants to buy, and p is the probability that Alaa will buy a chocolate plate from any shop that sell chocolate.

The probability p is given with exactly three digits after the decimal point

Output

On a single line print y, where y is the sought probability computed modulo 109 + 7.

The answer y is defined precisely as follows. Represent the probability that Alaa will buy the kth chocolate plate from the nth shop she will visit as an irreducible fraction p / q. The number y then must satisfy the modular equation y × q ≡ p (mod 109 + 7), and be between 0 and 109 + 6, inclusive. It can be shown that under the constraints of this problem such a number y always exists and is uniquely determined.

Examples
input
5 1 1 0.500
output
500000004
input
9 4 2 0.800
output
417600003
input
100 5 5 0.200
output
714240005
Note

In the first test case there are 5 shops that sell chocolate in Lucky Plaza, and Alaa wants to buy only 1 chocolate plate. In this case Alaa wants to know what is the probability that she will buy the 1st chocolate plate from the 1st shop she will visit. The probability is 1 / 2, and the answer is 500000004, since (500000004 * 2) % (109 + 7) = 1 % (109 + 7).

In the second test case there are 9 shops that sell chocolate in Lucky Plaza, and Alaa wants to buy only 2 chocolate plates. In this case Alaa wants to know what is the probability that she will buy the 2nd chocolate plate from the 4th shop she will visit. The probability is48 / 625, and the answer is 417600003, since (417600003 * 625) % (109 + 7) = 48 % (109 + 7).



题意:有m个超市  每个超市买到东西的概率为P  且只能买一个东西

问在第n个超市买到第k个东西的概率为多少

假设概率是p/q   那么输出x  使得  x*q=p(mod 1e9+7)


题解:

判掉不可能的情况

答案是 C(m-1,k-1)*p^k*(1-p)^(n-k)

用逆元搞一搞

那么x=p*q^(1e9+5)%(1e9+7)

ps:如果用*1000把p转化为整数  不加eps会wa的很惨


#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<math.h>using namespace std;#define scanff(x) scanf("%d",&x)#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)#define drep(i,a,b) for(int i=(int)a;i>=(int)b;i--)long long mod=1000000007;long long inv[100100];int main(){    int m,n,k;    inv[1]=1;    rep(i,2,100000){        inv[i]=((mod-mod/i)*inv[mod%i])%mod;    }    double dp;    scanf("%d%d%d",&m,&n,&k);    if(m<n||k>n){        printf("0\n");        return 0;    }    scanf("%lf",&dp);    long long p=(long long)floor(dp*1000.0+1e-10);    long long ans=1;    rep(i,1,k-1){        ans=(ans*(n-i))%mod;    }    rep(i,1,k-1){        ans=(ans*inv[i])%mod;    }    rep(i,1,k){        ans=(ans*p)%mod;    }    rep(i,1,n-k){        ans=(ans*(1000-p))%mod;    }    rep(i,1,n){        ans=(ans*inv[1000])%mod;    }    printf("%lld\n",ans);    return 0;}




1 0
原创粉丝点击