题目链接: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

代码如下:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+;
const double eps = 1e-;
const int N = ;
ll inv[N];
int main() {
int n, m, k, i, j;
double p1;
ll p, ans = ; inv[] = ;
for(i = ; i < N; ++i)
inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod; scanf("%d%d%d%lf", &m, &n, &k, &p1); if(m < n || k > n) {puts(""); return ;} p = (ll)(p1 * 1000.0 + eps);
//求组合数C(n-1, k-1)
for(i = ; i <= k-; ++i) ans = (ans * (n-i)) % mod;
for(i = ; i <= k-; ++i) ans = (ans * inv[i]) % mod; for(i = ; i <= k; ++i) ans = (ans * p) % mod;
for(i = ; i <= n-k; ++i) ans = (ans * (-p)) % mod;
//依次乘以1000关于mod的逆元
for(i = ; i <= n; ++i) ans = (ans * inv[]) % mod;
printf("%lld\n", ans);
return ;
}

31ms

Codeforces gym 101343 A. On The Way to Lucky Plaza【概率+逆元+精度问题】的更多相关文章

  1. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  6. Codeforces Gym 100269G Garage 数学

    Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

随机推荐

  1. 3DLut表实现log视频的后期调色原理

    现在越来越多的视频或者图像拍摄设备支持log模式,比如大疆无人机的D-Log模式等等,log模式的起源和发展就不多做介绍,其在普通显示器上显示画面通常看起来是平坦的灰色,因此也常被称为log灰视频. ...

  2. C语言之链表的使用

    C语言链表初学者都说很难,今天就来为大家讲讲链表 讲链表之前不得不介绍一下结构体,在链表学习之前大家都应该已经学了结构体,都知道结构体里面能有许多变量,每个变量可以当做这个结构体的属性,例如: str ...

  3. java中的序列化

    遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题 a,什么叫序列化和反序列化b,作用.为啥要实现这个 Serializable 接口,也就是为啥要序列化c,seri ...

  4. MapReduce核心 - - - Shuffle

    大数据名词(1) -Shuffle     Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每 ...

  5. GDB使用技巧

    最近使用GDB比较多,发现除了最常用的run.break.continue.next等命令的基本用法外,还有一些非常有用的命令和用法,能让你更加得心应手地使用GDB,在这里做了一下简单的总结. 1. ...

  6. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)

    Floor Function Time Limit: 10 Seconds      Memory Limit: 65536 KB a, b, c and d are all positive int ...

  7. Matlab disp()

    matlab中disp()就是屏幕输出函数,类似于c语言中的printf()函数

  8. 关于git的一个错误提示

    一般clone仓库后如果,remote -v或者add出现如下的错误的话: Not a git repository (or any of the parent directories): .git需 ...

  9. meta的日常设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 【读书笔记】iOS-iOS开发之iOS程序偏好设置(Settings Bundle)的使用

    在Android手机上, 在某个程序里,通过按Menu键,一般都会打开这个程序的设置,而在iOS里,系统提供了一个很好的保存程序设置的机制.就是使用Settings Bundle. 在按了HOME键的 ...