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\)条边,使得 ...
随机推荐
- 团队作业4——beta阶段冲刺
Deadline: 2018-12-16 22:00PM,以博客提交至班级博客时间为准 提交: (a) 项目课堂演示: (b) 1篇冲刺准备+5篇冲刺随笔,: (c) 1篇用户使用用报告: (d) 1 ...
- java操作远程共享目录
一.前言 根据客户反馈,在进行文件下载的时候,新增远程共享目录,下载对应的文件到远程共享目录,采用常用的IO操作模式,提示下载成功,但是客户去远程共享目录查看对应的下载文件,反馈说没有找到对应的文件. ...
- IOS获取资源路径
1. 获取已经添加到xcode工程中的某一资源文件 file.txt 的路径 NSString *filePath_= [[NSBundle mainBundle] pathForResource:@ ...
- [Luogu 3613] 睡觉困难综合征
Description 给定一棵 \(n\) 个点的树,每个点上有位运算 \(opt\) 和一个权值 \(x\),位运算有 &,|,^ 三种. 要求支持: 修改点 \(v\) 的 \(opt\ ...
- [转]ionic工作原理
本文转自:https://segmentfault.com/a/1190000011495654 1.Ionic的工作原理 Ionic通过cordova把一个Web应用嵌入原生应用 用户打开一个ion ...
- 数据结构-Python3.7<三>
上一篇:流程控制-Python<二> 因为列表具有pop.append.insert方法,因此列表可以当作堆.栈使用.由于性能问题,不建议当作堆.(堆:队列优先,先进先出(FIFO—fir ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(浙江省赛2015)
Ace of Aces Time Limit: 2 Seconds Memory Limit: 65536 KB There is a mysterious organization c ...
- Linux常用基本命令:三剑客命令之-awk 三元表达式
awk 3元表达式,if...else结构都可以用3元表达式改写 ghostwu@dev:~/linux/awk$ awk -v FS=":" '{ type=$3>=100 ...
- 七牛云java(服务端)通用工具类
前言 需要安装lombok插件. 功能列表 上传本地文件 上传Base64图片 获取文件访问地址 上传MultipartFile 代码 pom.xml <dependency> <g ...
- 【代码笔记】Web-HTML-列表
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...