HDU 4790 Just Random

Just Random

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


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 <= 10 9, 0 <=c <= d <= 10 9, 0 <= m < p <= 10 9).
 

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

在[a,b],[c,d]这两个区间内各找一个数,使得它们的和mod p=m,求概率是多少。
设A(a,b)为0<=x<=a,0<=y<=b满足条件的结果,则根据容斥原理ans=A(b,d)-A(a-1,d)-A(c-1,b)+A(a-1,c-1).
如果求f(16,10),p=6,m=2
则x有:0 1 2 3 4 5    0 1 2 3 4 5    0 1 2 3 4
对于y有:0 1 2 3 4 5    0 1 2 3 4
那么对于x,y集合中的(0,1,2,3,4,5)对满足条件数目为p。
设集合A=(0 1 2 3 4 5 01 2 3 4 5) B=(0 1 2 3 4) C=(01 2 3 4 5) D=(0 1 2 3 4)
则A(16,8)=A,C满足条件+A,D满足条件+B,C满足条件+B,D满足条件。
求B,D满足条件,首先判断a%p是否大于m
如果大于m:ans+=min(m,b%p),然后求出满足条件最小的y,ans+=b%p-y+1,即在上面例子中,满足条件的最小y为4,因此也要加上。
如果小于等于m:则直接求出满足条件的最小y,然后ans+=min(b%p-y+1,m-y+1)。
参考链接:点击打开链接
//46MS	212K
#include<stdio.h>
#include<algorithm>
#define LL long long
using namespace std;
LL m,p;
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
LL A(LL a,LL b)
{
    if(a<0||b<0)return 0;
    LL ma=a%p,mb=b%p;
    LL na=a/p,nb=b/p;
    LL ans=na*nb*p;
    ans+=na*(mb+1)+nb*(ma+1);
    if(ma>m)
    {
        ans+=min(mb,m)+1;
        LL t=(p+m-ma)%p;//求满足条件的最小y
        if(t<=mb)ans+=(mb-t+1);
    }
    else
    {
        LL t=(p+m-ma)%p;
        if(t<=mb)ans+=min(m-t+1,mb-t+1);
    }
    return ans;
}
int main()
{
    LL a,b,c,d;
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
        LL ans=A(b,d)-A(a-1,d)-A(c-1,b)+A(a-1,c-1);
        LL sum=(b-a+1)*(d-c+1);
        LL g=gcd(ans,sum);
        printf("Case #%d: %I64d/%I64d\n",cas++,ans/g,sum/g);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值