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)\)的求法有点晕,看这个博客吧:https://blog.csdn.net/SCNU_Jiechao/article/details/40818903

代码

#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(容斥)的更多相关文章

  1. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  2. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  3. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  4. HDU 4609 3-idiots FFT+容斥

    一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完 ...

  5. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

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

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  8. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

  9. 多校 HDU 6397 Character Encoding (容斥)

    题意:在0~n-1个数里选m个数和为k,数字可以重复选: 如果是在m个xi>0的情况下就相当于是将k个球分割成m块,那么很明显就是隔板法插空,不能为0的条件限制下一共k-1个位置可以选择插入隔板 ...

  10. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

随机推荐

  1. UE4之Slate: SImage

    概述 距离上次记录<UE4之Slate:纯C++工程配置>后已经好长时间了: 这个随笔来记录并分享一下SImage控件的使用,以在屏幕上显示一张图片: 目标 通过SImage控件的展示,学 ...

  2. UE4之Slate:纯C++工程配置

    概述: Slate是UE4提供的UI框架,整个UE4 Editor UI都是使用Slate构建的: Slate的官方文档:[Slate UI框架] Slate底层内容,中文环境下能搜索到的有效资源也不 ...

  3. Android 获取html中指定标签

    有时我们并不需要全部的html页面,而只是需要其中的部分标签,我们可以通过jsoup来完成这一操作. 官网:https://jsoup.org/ 1 Document document = Jsoup ...

  4. HDFS01 概述

    HDFS 概述 目录 HDFS 概述 HDFS的产生背景和定义 HDFS产生背景 HDFS定义 优缺点 优点 缺点 组成 NameNode DataNode Secondary NameNode(2n ...

  5. 零基础学习java------day12------数组高级(选择排序,冒泡排序,二分查找),API(Arrays工具类,包装类,BigInteger等数据类型,Math包)

    0.数组高级 (1)选择排序 它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的起始位置 ...

  6. js 如何全部替代一个子串为另一个子串

    更多描述: 假设有一个字符串 `hello. hello. hello. ` 需要替换为 `AAA`,即把 `hello. ` 替换为 `A` 如果需要全量替换字符串,可以使用 String.prot ...

  7. 【AWS】【TroubleShooting】EC2实例无法使用SSH远程登陆(EC2 failure for SSH connection)

    1. Login AWS web console and check the EC2 instance.

  8. Copy elision in C++

    Copy elision (or Copy omission) is a compiler optimization technique that avoids unnecessary copying ...

  9. MyBatis常用批量方法

    <!-- 批量添加派车单子表数据 --> <insert id="addBatch" parameterType="java.util.List&quo ...

  10. 【力扣】146. LRU缓存机制

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果关键字 (key) ...