码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4790:Just Random(容斥)

时间:2019-10-15 19:33:50      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:out   abi   unsigned   ems   fir   simple   概率   play   条件   

Just Random

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3932 Accepted Submission(s): 1276

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

题意

给出两个区间\([a,b],[c,d]\),从这两个区间分别任意选出一个数字\(x,y\),求\((x+y)\%p=m\)的概率

思路

容斥,将取出来的两个点看做横纵坐标,然后可以做一些平行线,看平行线在区间内的横纵坐标均为整数的点有多少个

\(F(l,r)\)表示从\([0,l]\)中取\(x\),从\([0,r]\)中取\(y\)的满足条件的点的个数

可以得到所有的符合要求的点的个数有\(F(b,d)-F(b,c-1)-F(a-1,d)+F(a-1,c-1)\)

具体的\(F(l,r)\)的求法有点晕,看这个博客吧:

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
ll p,m;
ll get_num(ll l,ll r)
{
    if(l<0||r<0)
        return 0;
    ll ml=l%p,mr=r%p;
    ll res=0;
    res=(l/p)*(r/p)*p;
    res+=(ml+1)*(r/p)+(mr+1)*(l/p);
    if(ml>m)
    {
        res+=min(mr+1,m+1);
        ll tmp=(m+p-ml)%p;
        if(tmp<=mr)
            res+=mr-tmp+1;
    }
    else
    {
        ll tmp=(m+p-ml)%p;
        if(tmp<=mr)
            res+=min(m-tmp+1,mr-tmp+1);
    }
    return res;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("/home/wzy/in", "r", stdin);
        freopen("/home/wzy/out", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    int _=0;
    while(t--)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d>>p>>m;
        ll sum=(b-a+1)*(d-c+1);
        ll ans=get_num(b,d)-get_num(b,c-1)-get_num(a-1,d)+get_num(a-1,c-1);
        cout<<"Case #"<<++_<<": ";
        cout<<ans/__gcd(ans,sum)<<"/"<<sum/__gcd(ans,sum)<<endl;
    }
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}

HDU 4790:Just Random(容斥)

标签:out   abi   unsigned   ems   fir   simple   概率   play   条件   

原文地址:https://www.cnblogs.com/Friends-A/p/11679456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!