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
代码如下:
#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【概率+逆元+精度问题】的更多相关文章
- 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 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces Gym 100269G Garage 数学
Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
随机推荐
- Guava初识
1. 是什么 开源Java库,提供了用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法 2. 开发它的最初目的是什么? 方便编码,减少编码错误 3. 好处 标准化 - Gu ...
- 技术人员在小公司成长 vs 大公司成长路径和建议
我们经常听到这样的对话: 大公司猿A:真不想干了,每天都做类似的工作,学不到什么东西,会议也多,浪费不少时间,想去小公司多做些事情,多学些东西. 小公司猿B:累死了,什么都做,太乱太杂,没系统不规范, ...
- 牛刀小试MySQL--日志文件
牛刀小试MySQL--日志文件 MySQL Server的日志文件一共有五种类型的日志.(Innodb redo log除外,它属于Innodb存储引擎实现的日不在此篇讨论) The Error Lo ...
- hive 表类型
Hive表有受控表(内部表).外部表.分区表.桶表四种. 内部表,就是一般的表,前面讲到的表都是内布标,当表定义被删除的时候,表中的数据随之一并被删除. 外部表,数据存在与否和表的定义互不约束 ...
- openCV 调用摄像头
OpenCV调用摄像头 环境 python:python3.6 摄像头:网络摄像头 Python库:openCV # -*- coding: utf-8 -*- # @author leone # @ ...
- canvas-6font.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- The open source JavaScript graphing library that powers Plotly
https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...
- 原生JS强大DOM选择器querySelector与querySelectorAll
在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...
- 【工具相关】Web-Sublime Text2-注释
按Command+/ 会出现<!----> 如图所示: 参考资料:<菜鸟教程>
- ionic cordova 安装指定版本
安装ionic 及 cordova npm install -g cordova ionic npm 淘宝镜像(GFW,导致很多插件下载失败) npm install -g cnpm --regist ...