hdu 4790 Just Random题解(数学)
来源:互联网 发布:单片机usb供电电路图 编辑:程序博客网 时间:2024/06/05 17:55
http://acm.hdu.edu.cn/showproblem.php?pid=4790
Just Random
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1034 Accepted Submission(s): 284
Problem Description
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In each game the following will be done:
1. Coach Pang randomly choose a integer x in [a, b] with equal probability.
2. Uncle Yang randomly choose a integer y in [c, d] with equal probability.
3. If (x + y) mod p = m, they will go out and have a nice day together.
4. Otherwise, they will do homework that day.
For given a, b, c, d, p and m, Coach Pang wants to know the probability that they will go out.
1. Coach Pang randomly choose a integer x in [a, b] with equal probability.
2. Uncle Yang randomly choose a integer y in [c, d] with equal probability.
3. If (x + y) mod p = m, they will go out and have a nice day together.
4. Otherwise, they will do homework that day.
For given a, b, c, d, p and m, Coach Pang wants to know the probability that they will go out.
Input
The first line of the input contains an integer T denoting the number of test cases.
For each test case, there is one line containing six integers a, b, c, d, p and m(0 <= a <= b <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).
For each test case, there is one line containing six integers a, b, c, d, p and m(0 <= a <= b <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).
Output
For each test case output a single line "Case #x: y". x is the case number and y is a fraction with numerator and denominator separated by a slash ('/') as the probability that they will go out. The fraction should be presented in the simplest form (with the smallest denominator), but always with a denominator (even if it is the unit).
Sample Input
40 5 0 5 3 00 999999 0 999999 1000000 00 3 0 3 8 73 3 4 4 7 0
Sample Output
Case #1: 1/3Case #2: 1/1000000Case #3: 0/1Case #4: 1/1
Source
2013 Asia Chengdu Regional Contest
题意:a <= x <= b,c <= y<= d,求满足 (x+y)%p==m 的(x,y)的个数。
题解:(x+y)%p==m, (x+y)==p*K+m。假设x 固定,那么对于区间【c,d】,满足条件的最大的Kmax为,Kmax=floor( (x + d-m) / p)。 floor为向下取整。
满足条件的最小的Kmin为:Kmin=ceil( (x+c-m)/p )。ceil为向上取整。所以当x固定是,满足条件的个数为: Kmax-Kmin+1。((x+d-m)<0或(x+c-m)<0时同样满足该式,因为
m<p)。 因为 a<=x<=b ,所以容易求出 sum(Kmax)和 sum(Kmin)。那么答案就sum(Kmax)-sum(Kmin)+(b-a+1)。(具体见代码)
#include<iostream>#include<string.h>#include<stdio.h>#include<algorithm>#include<string>#include<math.h>#define nn 510#define inff 0x3fffffffusing namespace std;typedef __int64 LL;LL a,b,c,d,p,m;LL jie(LL x) //当x==d-m时求 Kmax 的和,当x==c-m+p-1 时求 Kmin的和 (因为向上取整可以转化为向下取整,所以只写了一个求和的函数){ LL z=floor(1.0*(b+x)/p); LL zz=floor(1.0*(a+x)/p); if(zz==z) return z*(b-a+1); LL re=0 ; LL ix=(z-1)*p-x; ix+=p; re+=z*(b-ix+1); ix=(zz+1)*p-x; ix--; re+=zz*(ix-a+1); LL f1=zz+1,f2=z -1; if(f1<=f2) re+=(f1+f2)*(f2-f1+1)/2*p; return re;}LL solve(){ return jie(d-m)-jie(c-m+p-1)+b-a+1;}LL gcd(LL x,LL y){ if(y==0) return x; return gcd(y,x%y);}int main(){ int t,cas=1; scanf("%d",&t); while(t--) { scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m); LL fz=solve(); LL fm=(b-a+1)*(d-c+1); LL ix=gcd(fz,fm); printf("Case #%d: ",cas++); printf("%I64d/%I64d\n",fz/ix,fm/ix); } return 0;}
0 0
- hdu 4790 Just Random题解(数学)
- HDU 4790 Just Random 数学
- hdu - 4790 - Just Random(容斥 + 组合数学)
- hdu 4970 Just Random(数学:推理)
- hdu 4790 Just Random
- hdu 4790 Just Random
- HDU 4790 Just Random
- HDU - 4790 Just Random
- HDU 4790 Just Random
- HDU 4790 Just Random
- hdu 4790 Just Random
- hdu 4790 Just Random
- HDU 4790 Just Random
- HDU 4790 Just Random
- HDU-4790 Just Random
- HDU 4790 Just Random
- hdu 4790 Just Random
- HDU 4790 Just Random
- NPOI创建Excel表
- 啤酒和饮料
- Add Two Numbers
- 采用execSQL()和rawQuery()方法完成数据的添删改查
- Android之ViewGroup实现可拖动的GridView
- hdu 4790 Just Random题解(数学)
- 用Allinea DDT调试OpenACC代码
- 友盟分享快速集成--学习记录
- wireshark工具应用之简单总结
- Android之实现百度云主界面框架
- sql injection note day01
- mysql_fetch_row 与mysql_fetch_array的区别
- UVA - 1356 Bridge
- mysql数据库 message from server: "Host is not allowed to connect to this MySQL server"解决办法