HDU 4790 Just Random (概率 + 容斥思想+数学)

Just Random

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3415    Accepted Submission(s): 1103


 

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.

 

 

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).

 

 

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

 

4 0 5 0 5 3 0 0 999999 0 999999 1000000 0 0 3 0 3 8 7 3 3 4 4 7 0

 

 

Sample Output

 

Case #1: 1/3 Case #2: 1/1000000 Case #3: 0/1 Case #4: 1/1

 

 

Source

2013 Asia Chengdu Regional Contest

 

 

Recommend

We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long

const int  maxn =3e3+5;
const int mod=1e9+7;
/*
题目大意:就是给定a和b区间,c和d区间,
要求计数x和y满足(x+y)%p==m的对数。
并以概率形式输出.

如何计数对数呢?令f(a,b)函数为【0,a】和【0,b】两区间的答案,
那么如何求f函数呢?对p进行分块,即每个区间可以分解为
这样的特征,k个[0,p-1]区间和[0,p1]区间(p1<p-1),
那么求解f函数根据组合常识明显分为四块,当然每块其目标值为m和m+p,
只有两种目标值。

对于两个区间和一个目标值,确定其对数用点简单的数学知识即可,
分类讨论下边界和第二个区间的相交情况,取其交集个数进行计数。
*/
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}

ll a,b,c,d,p,m;
ll compute(int a,int b,int t)///区间只讨论原子性
{
    if(a==-1||b==-1) return 0;
    int tp1=t-a;
    if(tp1<0) tp1=0;
    if(tp1>b) return 0;
    int tp2=t;if(t>b) tp2=b;
    return tp2-tp1+1;
}
ll f(int a,int b)///0到a区间和0到b区间有多少对满足条件的数
{
    if(a<0||b<0) return 0LL;
    int k1=(a+1)/p , k2 = (b+1)/p;
    int a1=a%p;if(a1==p-1) a1=-1;
    int b1=b%p;if(b1==p-1) b1=-1;
    ll ans=0;
    ans+=1LL*k1*k2*(compute(p-1,p-1,m)+compute(p-1,p-1,m+p));
    ans+=1LL*k1*(compute(b1,p-1,m)+compute(b1,p-1,m+p));
    ans+=1LL*k2*(compute(a1,p-1,m)+compute(a1,p-1,p+m));
    ans+=1LL*compute(a1,b1,m)+compute(a1,b1,m+p);
    return ans;
}

int main()
{
    int t;scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&p,&m);
        ll ans1=(b-a+1)*(d-c+1);
        ll ans2=f(b,d)-f(b,c-1)-f(a-1,d)+f(a-1,c-1);
        ll gd=gcd(ans1,ans2);
        ans1/=gd,ans2/=gd;
        printf("Case #%d: %lld/%lld\n",ca,ans2,ans1);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值