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:

  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

题意

给出两个区间\([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)\)个

具体的\(F(l,r)\)的求法有点晕,看这个博客吧:

代码

#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;
}

原文地址:https://www.cnblogs.com/Friends-A/p/11679456.html

时间: 2024-11-10 01:55:40

HDU 4790:Just Random(容斥)的相关文章

HDU 4790 Just Random 【构造】

题目链接:传送门 题目大意:给你a, b, c, d, p and m(0 <= a <= b <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).让你从 [a, b] 中取出一个x,从[c, d] 中取出一个y,使得(x+y)(mod p)=m,求一共有多少种可能. 看见题目就想到了用构造的想法,讲解构造成二维坐标系,x+y=k*p+m是一条线,求这条线与构造的矩形(由点组成)一共有几个交点,这个矩阵也是有一定

hdu 4790 Just Random 神奇的容斥原理

1 /** 2 大意: 给定[a,b],[c,d] 在这两个区间内分别取一个x,y 使得 (x+y)%p = m 3 思路:res = f(b,d) -f(b,c-1)-f(a-1,d)+f(a-1,c-1); f(b,d ) 表示在[0,b],[0,d] 之间有多少个符合上述要求的数 4 1.将[0,b] 分为两部分, b/p 和 b%p 能整除p的[0,(b/p)*p] 和[(b/p)*p+1,b ] 同理[0,d]也可以这样分, 这样对于[0,b] [0,d ] 分别有两种情况,则一共有四

HDU - 4790 Just Random

题意:求从[a,b],[c,d]两个区间找到两个数使得他们的和%p=m,求概率 思路:我们想办法把区间的左范围化到0,那么结果就相对好弄了,应用容斥原理比直接解答问题简单点,假设f(a,b)是区间[0,a],[0,b]中满足条件的个数,设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

HDU 5321 Beautiful Set 容斥 (看题解)

HDU 5321 感觉有点抗拒这种题目, 看到就感觉自己不会写,其实就是个沙雕题, 感觉得找个时间练练这种题. g[ i ] 表示gcd为 i 的倍数的方案数, f[ i ] 表示gcd为 i 的方案数, 然后先算g[ i ]然后直接容斥. #pragma GCC optimize(2) #pragma GCC optimize(3) #include<bits/stdc++.h> #define LL long long #define LD long double #define ull

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

HDU 3970 Harmonious Set 容斥欧拉函数

链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中任意选一些数使得选出的数和为n的倍数的方法数 ...并不会如何递推.. 思路: 然后这是公式:点击打开链接 a(n) = 1/n * sum_{d divides n and d is odd} 2^(n/d) * phi(d). d最大是n,也就是1e9,要计算1e9的phi,所以容斥来算phi. #pragma comment(linker, "/STA

HDU 4135 Co-prime(组合+容斥)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relatively prime if they have no common positive divisors other tha

HDU 4609 3-idiots FFT+容斥

一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完全可以母函数,无奈数据很大,就用FFT了 然后难点在于最后的统计,要减去自身,两个都大的,一大一小,包含自身,这是用到了容斥,再做相似的题的时候,应该多看看这方面 注:再次高度仰慕kuangbin神,这是我FFT的第二题,也是第二次用kuangbin FFT模板 #include <stdio.h>

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y