SPOJ FUNPROB - Yanu in Movie theatre

题目链接http://www.spoj.com/problems/FUNPROB/

题目大意:N+M个人排队买票,N个人手里有10元,M个人手里有5元,票价5元。但售票厅没有零钱了。问其能正常售票的概率(即不会出现10元买票但没零钱找)。

解题思路:参考:http://stackoverflow.com/questions/25281005/calculating-probability-for-funprob

当N < M 时的结果 res = (M - N + 1) / (M + 1) ,证明的话可以使用数学归纳法:

当N = 1 的时候 res = (M + 1 - 1) / (M + 1)。

假设当N = K的时候有res = (M + K - 1) / (M + 1),则当N = K + 1时,我们将第K+1个人放到已经能够正常工作的队列中时,每个10元的都能和前面一个5元的相互抵消,那么

res = (M - K + 1) / (M + 1) * (M - (K + 1) + 1) / (M - K + 1) = (M - (K + 1) + 1) / (M + 1)

即证。

推导过程详见参考链接。

代码:

 1 #include <cstdio>
 2 typedef long long ll;
 3 ll n, m;
 4
 5 void solve(){
 6     if(n > m) printf("%.6f", 0);
 7     else if(n == m) printf("%.6f", 1.0 / (m + 1));
 8     else printf("%.6f", (double)(m - n + 1) / (m + 1));
 9     puts("");
10 }
11 int main(){
12     while(scanf("%lld %lld", &n, &m) && (n || m)){
13         solve();
14     }
15 }

题目:

FUNPROB - Yanu in Movie theatre

#math

Yanu is a great fan of Harry Potter.So on the day of the movie release, Yanu rushes to the movie theatre to watch the movie. On the release of the 6th movie, on reaching the theatre she found a long queue for tickets already.As the distirbution was about to start, the ticket counter did not have any change to start with. There are N+M people in queue,where N have Rs 10.00 and M have Rs 5.00. The ticket costs Rs 5.00.

Now Yanu is math geek too , now she wonders What is the probability that the theatre can always provide change.

Input

Each line contain N and M , seperated by a space , End of Input is marked by 0 0 which should not be processed. Both N and M fits in integer range.

Output

For each input , output the respective probability upto 6 decimal digits.

Example

Input:1 00 141 410 0

Output:0.0000001.0000000.023810
时间: 2024-08-13 16:49:25

SPOJ FUNPROB - Yanu in Movie theatre的相关文章

SPOJ 705 Distinct Substrings(后缀数组)

[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每个后缀对答案的贡献为n-sa[i]+1-h[i], 因为排名相邻的后缀一定是公共前缀最长的, 那么就可以有效地通过LCP去除重复计算的子串. [代码] #include <cstdio> #include <cstring> #include <algorithm> usi

SPOJ 3273

传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 1 //SPOJ 3273 2 //by Cydiater 3 //2016.8.31 4 #include <iostream> 5 #include <cstring> 6 #include <ctime> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include

SPOJ CRAN02 - Roommate Agreement

题目链接:http://www.spoj.com/problems/CRAN02/ 题目大意:N个数字组成的序列,和为0的连续子序列的个数.N<1e6 解题思路:计算前缀和,统计每个数字出现的次数,那么对于数字sum[i], 如果存在k个sum[i],则代表有C(k, 2)个序列和为0,而如果sum[i] = 0,则还要累加上对应的k值. 代码: 1 ll n; 2 int a[maxn]; 3 ll sum[maxn]; 4 map<int, int> mmp; 5 6 void so

spoj GCJ1C09C Bribe the Prisoners

题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall wi

SPOJ QTREE Query on a tree ——树链剖分 线段树

[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20005 int T,n,fr[maxn],h[maxn],to[maxn],ne[maxn]

Theatre Square 题解代码

Theatre Square CodeForces - 1A 水题水题... 最开始的程序是这个,可是AC不了我也不知道为什么......QAQ... #include <stdio.h>#include<stdlib.h>int main(){    int m,n,a,ans,tmp,k,h,tep,i,j;    scanf("%d %d %d",&m,&n,&a);    if(m%a==0)    {        if(n%a

BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

2588: Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文. Input 第一行两个整数N,M. 第二行有N个整数,其中第i个整数表示点i的权值. 后面N-1行每行两个整数(x,y),表示点x到点y有一条边. 最后M行每行两个整数(u,v,k),表示一组询问.

BZOJ 1002 + SPOJ 104 基尔霍夫矩阵 + 一个递推式。

BZOJ 1002 高精度 + 递推 f[1] = 1; f[2] = 5; f[i] = f[i - 1] * 3 - f[i - 2] + 2; SPOJ 104 裸 + 不用Mod 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 7 using namespace std;

SPOJ QTREE 系列解题报告

题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 using namespace s