HDU4790 Just Random 【数学】

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 <= 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
 

Source

定义f(a, b)  能求出 [0, a]与[0, b] 选出两个数加和  % p = m 的个数

则[a, b] [c, d]符合要求的个数res = f(b, d) + f(a-1, c-1) - f(b, c-1) - f(a-1, d);

f函数计算过程见代码


#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define INF 0x1f1f1f1f
#define M 100005
using namespace std;
int Case = 1;

int t, n;

long long a, b, c, d, p, m;

long long f(long long a, long long b) {
    if(a < 0 || b < 0) {
        return 0;
    }
 
    long long res = 0;
    long long x = b / p;
    res += (a+1)*x;
    b = b % p;

    long long y = a / p;
    res += (b+1)*y;
    a = a % p;

    if(b <= m) {
        if(a >= m-b) {
            res += min(a-(m-b)+1, b+1);
        }
    }
    else {
        res += min(a+1, m+1);

        x = (m-b+p)%p;
        if(x <= a) {
            res += (a-x+1);
        }
    }
    return res;
}

long long gcd(long long a, long long b) {
    if(b == 0) {
        return a;
    }
    return gcd(b, a%b);
}

int main()
{
    scanf("%d", &t);
    while(t--) {
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &a, &b, &c, &d, &p, &m);
        printf("Case #%d: ", Case++);

        long long res = f(b, d) + f(a-1, c-1) - f(b, c-1) - f(a-1, d);

        long long x = res;
        long long y = (b-a+1)*(d-c+1);
        long long g = gcd(x, y);
        printf("%I64d/%I64d\n", x/g, y/g);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值