Codeforces gym 101343 A. On The Way to Lucky Plaza【概率+逆元+精度问题】
题目链接:http://codeforces.com/gym/101343/problem/A
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 kth chocolate 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?
The first line contains three integers m, n, k and real number p (1 ≤ m, n, k ≤ 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
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.
5 1 1 0.500
500000004
9 4 2 0.800
417600003
100 5 5 0.200
714240005
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 is 48 / 625, and the answer is 417600003, since (417600003 * 625) % (109 + 7) = 48 % (109 + 7).
题意:去m个超市买东西,在每个超市买东西的概率都为p,并且最多只能买一件。求在第n个超市刚好买第k个商品的概率。设概率为p/q,则输出y使得其满足:
题解:容易得出概率就为:
将公式两边同时乘q的逆元inv(q),可得:
因为题目输入p给的是三位小数,所以直接将p乘以1000化为整数,最后答案再依次乘以1000的逆元就行。但是要注意的是,p化为整数时还要加个eps......然后可以开始补题了...
【有个快速求逆元的公式】:当p是质数时,a关于p的逆元为
【ACM中浮点数精度问题】:http://www.cnblogs.com/crazyacking/p/4668471.html
关于这题浮点数精度问题,我是这样想的,举个例子:
题解参考:https://blog.csdn.net/black_miracle/article/details/70196798
代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll mod = 1e9+7; 5 const double eps = 1e-8; 6 const int N = 100005; 7 ll inv[N]; 8 int main() { 9 int n, m, k, i, j; 10 double p1; 11 ll p, ans = 1; 12 13 inv[1] = 1; 14 for(i = 2; i < N; ++i) 15 inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod; 16 17 scanf("%d%d%d%lf", &m, &n, &k, &p1); 18 19 if(m < n || k > n) {puts("0"); return 0;} 20 21 p = (ll)(p1 * 1000.0 + eps); 22 //求组合数C(n-1, k-1) 23 for(i = 1; i <= k-1; ++i) ans = (ans * (n-i)) % mod; 24 for(i = 1; i <= k-1; ++i) ans = (ans * inv[i]) % mod; 25 26 for(i = 1; i <= k; ++i) ans = (ans * p) % mod; 27 for(i = 1; i <= n-k; ++i) ans = (ans * (1000-p)) % mod; 28 //依次乘以1000关于mod的逆元 29 for(i = 1; i <= n; ++i) ans = (ans * inv[1000]) % mod; 30 printf("%lld\n", ans); 31 return 0; 32 }