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)\)个
具体的\(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(容斥)的更多相关文章
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- HDU 5297 Y sequence 容斥 迭代
Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...
- hdu 6053 trick gcd 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...
- HDU 4609 3-idiots FFT+容斥
一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完 ...
- HDU 4336 Card Collector(容斥)
题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...
- HDU 4135 Co-prime(容斥+数论)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 3970 Harmonious Set 容斥欧拉函数
pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n 求连续整数[0,n), 中随意选一些数使得选出的 ...
- HDU 4135 Co-prime(容斥:二进制解法)题解
题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...
- 多校 HDU 6397 Character Encoding (容斥)
题意:在0~n-1个数里选m个数和为k,数字可以重复选: 如果是在m个xi>0的情况下就相当于是将k个球分割成m块,那么很明显就是隔板法插空,不能为0的条件限制下一共k-1个位置可以选择插入隔板 ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
随机推荐
- A Child's History of England.12
Dunstan, Abbot of Glastonbury Abbey, was one of the most sagacious of these monks. He was an ingenio ...
- C++11的auto自动推导类型
auto是C++11的类型推导关键字,很强大 例程看一下它的用法 #include<vector> #include<algorithm> #include<functi ...
- oracle中的控制语句
一.条件语句1.流程控制-if else(1)ifif 判断条件 then ...end if;(2)if-elseif 判断条件 then ...else ...end ...
- Cx_Oracle 安装
1. 下载安装 2.把oci.ddl oraociei11.dll 放到C:\Python33\Lib\site-packages路径下
- linux 常用清空文件方法
1.vim 编辑器 vim /tmp/file :1,$d 或 :%d 2.cat 命令 cat /dev/null > /tmp/file
- centos 7 重新获取IP地址
1.安装软件包 dhclient # yum install dhclient 2.释放现有IP # dhclient -r 3.重新获取 # dhclient 4.查看获取到到IP # ip a
- html块 布局
可通过<div>和<span>将html元素组合起来. Html块元素 大多数html元素被定义为块级元素或内联元素. 块级元素在浏览器显示时,通常会以新行来开始(和结束).例 ...
- spring jdbc 配置数据源连接数据库
概述 在XML配置数据源,并将数据源注册到JDBC模板 JDBC模板关联业务增删改查 在XML配置数据源 <?xml version="1.0" encoding=" ...
- Centos 7 安装redis,修改配置文件不生效、外网不能访问。
前提: 在用Centos 7 安装 redis 时,遇上一下几个问题 ,记录下 . 1.修改配置文件,按官网步骤启动,不生效. 2.外网无法访问redis. 步骤: 1.打开centos 虚拟机 ,按 ...
- Java值引用和对象引用区别Demo
转自:http://blog.csdn.net/gundsoul/article/details/4927404 以前就知道JAVA对象分对象引用和值引用,并且还知道8种基础数据类型,即引用时是值引用 ...