hdu 4790 数学

来源:互联网 发布:手机淘宝切换账号登录 编辑:程序博客网 时间:2024/04/29 15:01



链接:戳这里


Just Random

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

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 <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).
 
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


思路:

容斥原理可以得出ans=(b+d)-(a-1,d)-(c-1,b)+(a-1,b-1)

假设 p=6, m=2

第一个区间可以看成:A=[0,1,2,3,4,5]+[0,1,2,3,4,5]+..... B= (0,1,2,3,4)

第二个区间可以看成:C=[0,1,2,3,4,5]+....D=(0,1) 

就可以看成:A+C,A+D, B+D ,C+D的结果和

前三个不难算,关键是第四个:根据与m的大小做对比,假设B最大的是ma,D最大的是mb

那么当ma>m的时候,我们可以尝试从mb的范围里找是否有能与ma中小于m的数中结合成m。

还有的情况是从mb中找一个能与ma中大于m的数结合成m。

另一种情况就相对简单点了,只需要找出ma中和mb中结合成m

具体看代码


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;ll p,m;ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}ll get(ll a,ll b){    if(a<0 || b<0) return 0;    ll ma=a%p,mb=b%p;    ll ans=(a/p)*(b/p)*p;    ans+=(ma+1)*(b/p)+(a/p)*(mb+1);    if(m<ma) {        ans+=min(m,mb)+1;        ll t=(p+m-ma)%p;        if(t<=mb) ans+=mb-t+1;    } else {        ll t=(p+m-ma)%p;        if(t<=mb) ans+=min(m-t,mb-t)+1;    }    return ans;}ll a,b,c,d;int main(){    int T;    scanf("%d",&T);    for(int cas=1;cas<=T;cas++){        scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);        printf("Case #%d: ",cas);        ll A=get(b,d)-get(b,c-1)-get(a-1,d)+get(a-1,c-1);        ll B=(b-a+1)*(d-c+1);        printf("%I64d/%I64d\n",A/gcd(A,B),B/gcd(A,B));    }    return 0;}



0 0
原创粉丝点击