HDU 4790:Just Random(容斥)

Just Random

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

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

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:c++

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

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

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\)的機率.net

思路

容斥,將取出來的兩個點看作橫縱座標,而後能夠作一些平行線,看平行線在區間內的橫縱座標均爲整數的點有多少個code

\(F(l,r)\)表示從\([0,l]\)中取\(x\),從\([0,r]\)中取\(y\)的知足條件的點的個數orm

能夠獲得全部的符合要求的點的個數有\(F(b,d)-F(b,c-1)-F(a-1,d)+F(a-1,c-1)\)blog

具體的\(F(l,r)\)的求法有點暈,看這個博客吧:ip

代碼

#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;
}
相關文章
相關標籤/搜索