HDU 4790:Just Random(容斥)
Posted friends-a
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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): 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:
- Coach Pang randomly choose a integer (x) in ([a, b]) with equal probability.
- Uncle Yang randomly choose a integer (y) in ([c, d]) with equal probability.
- If ((x + y) mod p = m), they will go out and have a nice day together.
- 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))个
代码
#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(容斥)的主要内容,如果未能解决你的问题,请参考以下文章
hdu 4790 Just Random 锛?013鎴愰兘J棰橈級 鏁板鎬濊矾棰?瀹规枼