problem Description
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. in each game the following is done:1. Coach Pang Randomly choose a integer xinch[A, b] with equal probability. 2. Uncle Yang randomly choose a integer yinch[C, d] with equal probability. 3. If (x + y) mod p = m, they'll go outand has a nice day together. 4. Otherwise, they. Dohomework. For given a, B, C, D, p and M, Coach Pang wants to know the probability that they would go out.
Input
Case is one line containing six integers a, B, C, D, p and m (010901090
109
Output
Case " Case #x: y " is Case is a fraction with numerator and denominator separated by a slash ('/'as
out
in
if
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 #11/3
Case #21/1000000
Case #30/1
Case #41/1
SourceAsia Chengdu Regional Contest
Idea: For A<=x<=b,c<=y<=d, the result of satisfying the condition is ans=f (b,d)-F (b,c-1)-F (a-1,d) +f (a-1,c-1).
The function f (A, B) is the result of calculating the 0<=x<=a,0<=y<=b satisfying condition. It is convenient to calculate it.
For example: Ask F (16,7), p=6,m=2.
For x: 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4
For y: 0 1 2 3 4 5 0 1
It is easy to know that for XY (0 1 2 3 4 5) The number of satisfied conditions is p.
This takes a set for (0 1 2 3 4 5 0 1 2 3 4 5), and B sets for (0 1 2 3 4).
The C set is (0 1 2 3 4 5), and D is set to (0 1).
This can be divided into 4 sections to calculate.
F (16,7) =a and C satisfies the number of conditions +a and D satisfies the condition of the number of +b and C satisfies the condition of the number of +b and D satisfies the condition.
The first 3 are very good, the key is B and D to meet the conditions of how to beg!
This will be based on M to divide the situation.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #definell Long Long6 ll A,b,c,d,p,m;7 ll gcd (ll X,ll y)8 {9 returny==0? X:GCD (y,x%y);Ten } One ll F (ll X,ll y) A { - if(x<0|| y<0) - return 0; thell mx=x%p; -ll my=y%p; -ll ans=0; -ans=ans+ (x/p) * (y/p) *p;//1 +ans=ans+ (x/p) * (my+1);//2 -ans=ans+ (y/p) * (mx+1);//3 + A if(mx>m)//4 at { -Ans=ans+min (my,m) +1; -ll t= (p+m-MX); - if(t<=my) -ans=ans+my-t+1; - } in Else//4 - { toll t= (P+M-MX)%p; + if(t<=my) Ans=ans+min (my-t+1, m-t+1); - } the returnans; * } $ intMain ()Panax Notoginseng { - intT; the intAc=0; +scanf"%d",&t); A while(t--) the { +printf"Case #%d:",++AC); -scanf"%i64d%i64d%i64d%i64d%i64d%i64d",&a,&b,&c,&d,&p,&m); $ll Ans=f (B,d)-F (b,c-1)-F (A-1, d) +f (A-1, C-1); $ll tot= (b-a+1) * (d-c+1); -ll r=gcd (ans,tot); -printf"%i64d/%i64d\n", ans/r,tot/R); the } - return 0;Wuyi}
View Code
Recommend
Hdu 4790 Just Random (train of thought + classified calculation + math)