Codeforces gym 101343 A. On The Way to Lucky Plaza【概率+逆元+精度问题】

 

题目链接:http://codeforces.com/gym/101343/problem/A

A. On The Way to Lucky Plaza
time limit per test:1.0 s
memory limit per test:256 MB
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 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?

Input

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

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 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

关于这题浮点数精度问题,我是这样想的,举个例子:

  输入数p为0.001,但是你浮点数变量p存的值可能就变成了0.0010000001,或者变成了0.0009999999,所以这题乘以1000还要再加个eps,加0.2和加1e-10都没关系(看这题卡的精度吧),但是因为是浮点数,其低位的不确定性使得你运算时必须加个eps...

 

题解参考: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 }
31ms

 

posted @ 2018-05-06 13:13  GraceSkyer  阅读(506)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……