题目链接: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. MySQL事务笔记

    1.结束事务的方法用什么? 2.事务的最终形态是什么? commit 提交 rollback 回滚 3.事务的四大特征? ⑴ 原子性 一个事务是最小的工作单元,事务包含的所有操作要么全部成功,要么全部 ...

  2. noip之后的一些感受

    你经历过绝望吗,那种希望完全破碎,眼前看不到光亮,不知道下一步怎么走,不知道接下来应该如何生活的那种绝望? 我经历过. 2018.11.12 下午 秦皇岛到石家庄的高铁上 听着同学兴高采烈的讨论,自己 ...

  3. [SPOJ22343] Norma

    Description 现在有一个长度为\(N(N\leq 500000)\)的序列,定义区间\([l,r]\)的价值为\([l,r]\)的最小值乘上\([l,r]\)的最大值乘上\([l,r]\)的 ...

  4. [java]我的数据在哪里?——数据的内存模型

    在编写程序时,我们也许会有这样一个问题,我们编写的程序中的数据运行时,会保存在哪里呢?简单直接的回答可能是——内存.这个回答在多数情况下可能都是对的,但事实上并不准确,我们都知道内存,即随机访问存储器 ...

  5. c++中虚函数和多态性

    1.直接看下列代码: #include <iostream> using namespace std; class base{ public: void who(){ cout<&l ...

  6. LeetCode子域名访问计数-Python3.7<五>

    上一篇:LeetCode 键盘行<四> 题目:https://leetcode-cn.com/problems/subdomain-visit-count/description/ 一个网 ...

  7. .NET JSON 转换 Error ” Self referencing loop detected for type“

    在进行实体转换为Json格式报错如下图: Self referencing loop detected for property 'md_agent' with type 'System.Data.E ...

  8. ModBus通信协议的【功能码】

    Modbus的功能码    ModBus 功能码与数据类型对应表:   RTU 方式读取整数据的例子: 解析一下:主机发送指令,访问从站地址为1,使用功能码03(读保持寄存器),起始地址高8位.低8位 ...

  9. mysql允许所有机器访问

    1.进入到MySQL安装的bin目录. 2.运行mysql -uroot 3. 授权用户,你想root使用密码从任何主机连接到mysql服务器 GRANT ALL PRIVILEGES ON *.* ...

  10. Vue 系列之 基础入门

    背景叙述 渐进式 JavaScript 框架 易用:已经会了 HTML.CSS.JavaScript?即刻阅读指南开始构建应用! 灵活:不断繁荣的生态系统,可以在一个库和一套完整框架之间自如伸缩. 高 ...