hdu 4790 Just Random

題面如下

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

這題第一眼做的時候直接用幾何概率的做法做了,感覺沒毛病但一直wa,然後只能求教大佬了,,

用了容斥+數學思想

 

 #include <iostream>
 #include <algorithm>
 using namespace std;
 long long a,b,c,d,p,m;
 
 long long min(long long a,long long b){
     return a<b?a:b;
 }
 
 long long sol(long long b,long long d){
     ||d<)
         ;
     long long ma,mb;
     ;
     long long tmp;
     ans += (b/p)*(d/p)*p;
     ma = b%p;
     mb = d%p;
     ans += (ma+)*(d/p) + (mb+)*(b/p);
     if(ma>m){
         ans += min(m+,mb+);
         tmp = (p+m-ma)%p;
         );
     }else{
         tmp = (m-ma+p)%p;
         if(tmp<=mb)
             ans += min(m-tmp+,mb-tmp+);
     }
     return ans;
 }
 
 long long gcd(long long a,long long b){
     )
         return a;
     return gcd(b,a%b);
 }
 
 int main()
 {
     int t;
     cin>>t;
     int cnt;
     ;cnt<=t;cnt++){
         cin>>a>>b>>c>>d>>p>>m;
         long long res;
         res = sol(b,d)-sol(b,c-)-sol(a-,d)+sol(a-,c-);
         )*(d-c+));
         long long gcdD = gcd(res ,sum);
         res = res/gcdD;
         sum = sum/gcdD;
         cout<<"Case #"<<cnt<<": ";
         cout<<res<<"/"<<sum<<endl;
     }
     ;
 }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章