HDU 4790 Just Random (2013成都J题)
Just Random
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 87 Accepted Submission(s): 34
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.
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).
0 5 0 5 3 0
0 999999 0 999999 1000000 0
0 3 0 3 8 7
3 3 4 4 7 0
Case #2: 1/1000000
Case #3: 0/1
Case #4: 1/1
这题就是要找在[a,b] [c,d] 之间,和模p等于m的对数。
把[a,b] [c,d]所有可能组合的和写成下列形式。
a+c a+c+1 a+c+2 ..................a+d
a+c+1 a+c+2 a+c+3 ........a+d a+d+1
a+c+2 a+c+3 a+d a+d+1 a+d+2
....................
...................
b+c b+c+1 ...............................................b+d;
上面大致形成一个斜的矩阵。
使用b+c 和 a+d两条竖线,就可以分成三部分。前后两部分个数是等差数列,中间个数是相等的。
只需要讨论下b+c 和 a+d的大小。 然后找到%p==m 的位置,求和就可以搞定了。
/* ***********************************************
Author :kuangbin
Created Time :2013-11-16 13:20:40
File Name :E:\2013ACM\专题强化训练\区域赛\2013成都\1010.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; long long gcd(long long a,long long b)
{
if(b == )return a;
return gcd(b,a%b);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long a,b,c,d,p,m;
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
long long ans = ;
if(b+c <= a+d)
{
long long t1 = (a+c)%p;
long long add = (m - t1 + p)%p;
long long cnt1 = (a+c + add-m)/p;
//cout<<t1<<" "<<add<<endl;
long long t2 = (b+c-)%p;
long long sub = (t2 - m + p)%p;
long long cnt2 = (b+c--sub-m)/p;
//cout<<t2<<" "<<sub<<endl;
ans += (cnt2 - cnt1 + )*(+add) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/ * p;
//printf("%I64d %I64d %I64d\n",cnt1,cnt2,ans);
t1 = (b+c)%p;
add = (m - t1 + p)%p;
cnt1 = (b+c+add-m)/p;
t2 = (a+d)%p;
sub = (t2 - m + p)%p;
cnt2 = (a+d-sub-m)/p;
ans += (cnt2 - cnt1 + )*(b-a+);
t1 = (a+d+)%p;
add = (m - t1 + p)%p;
cnt1 = (a+d++add-m)/p;
t2 = (b+d)%p;
sub = (t2 - m + p)%p;
cnt2 = (b+d-sub-m)/p;
ans += (cnt2 - cnt1 + )*(+sub) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/*p;
}
else
{
long long t1 = (a+c)%p;
long long add = (m - t1 + p)%p;
long long cnt1 = (a+c + add-m)/p;
long long t2 = (a+d-)%p;
long long sub = (t2 - m + p)%p;
long long cnt2 = (a+d--sub-m)/p;
ans += (cnt2 - cnt1 + )*(+add) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/ * p;
t1 = (a+d)%p;
add = (m - t1 + p)%p;
cnt1 = (a+d+add-m)/p;
t2 = (b+ c)%p;
sub = (t2 - m + p)%p;
cnt2 = (b+c-sub-m)/p;
ans += (cnt2 - cnt1 + )*(d-c+);
t1 = (b+c+)%p;
add = (m - t1 + p)%p;
cnt1 = (b+c++add-m)/p;
t2 = (b+d)%p;
sub = (t2 - m + p)%p;
cnt2 = (b+d - sub-m)/p;
ans += (cnt2 - cnt1 + )*(+sub) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/*p;
}
long long tot = (b-a+)*(d-c+);
long long GCD = gcd(ans,tot);
ans /= GCD;
tot /= GCD;
printf("Case #%d: %I64d/%I64d\n",iCase,ans,tot);
}
return ;
}
HDU 4790 Just Random (2013成都J题)的更多相关文章
- hdu 4790 Just Random (2013成都J题) 数学思路题 容斥
题意:在[a,b] [c,d] 之间,和模p等于m的对数 详见代码 #include <stdio.h> #include <algorithm> #include < ...
- HDU 4786 Fibonacci Tree (2013成都1006题)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4733 G(x) (2013成都网络赛,递推)
G(x) Time Limit: 2000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契
题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...
- 2014 HDU多校弟六场J题 【模拟斗地主】
这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...
- 2014 HDU多校弟五场J题 【矩阵乘积】
题意很简单,就是两个大矩阵相乘,然后求乘积. 用 Strassen算法 的话,当N的规模达到100左右就会StackOverFlow了 况且输入的数据范围可达到800,如果变量还不用全局变量的话连内存 ...
- HDU 4790 Just Random 数学
链接:pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790 意:从[a.b]中随机找出一个数字x,从[c.d]中随机找出一个 ...
- hdu 4790 Just Random (思路+分类计算+数学)
Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with ...
- hdu 4790 Just Random 神奇的容斥原理
/** 大意: 给定[a,b],[c,d] 在这两个区间内分别取一个x,y 使得 (x+y)%p = m 思路:res = f(b,d) -f(b,c-1)-f(a-1,d)+f(a-1,c-1); ...
随机推荐
- HDU 2066 一个人的旅行 最短路问题
题目描述:输入的第一行有三个数,T,S,D,T表示一共有多少条线路,S表示起点的个数,D表示终点的个数,接下来就是输入T条路的信息了,要你判断从多个起点中任意一个到多个终点中的任意的一个的最短距离是多 ...
- MySQL-视图View
视图:一个非真实存在的,囊括复杂查询在内的表,也可以理解成,视图就是一个查询之后的结果. 补充一个概念:临时表查询(子查询):就是在查询语句中再次嵌套一个查询,并将嵌套中的查询设定别名 SELECT ...
- Maven私服安装及配置——(十二)
0.私服实际是B/S架构的,需要通过浏览器访问.访问地址在 nexus-2.12.0-01\conf\nexus.properties中查看.
- MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
Java面试通关手册(Java学习指南,欢迎Star,会一直完善下去,欢迎建议和指导):https://github.com/Snailclimb/Java_Guide 一 MyISAM 1.1 My ...
- Project Euler Problem4
Largest palindrome product Problem 4 A palindromic number reads the same both ways. The largest pali ...
- 小白学习安全测试(二)——httrack的安装和使用
httrack是一款免费的网站镜像程序,简单理解就是可以在网站结构(网页及一些主要信息文件),下载到本地,可离线浏览,我是按照搭建成功后的console直译过来的 下面说下安装: 我都是在Linux环 ...
- vue之给a标签赋值
<li v-for="(bp,index) in bpLists"> <a class="bidPublicityTitle" :href=& ...
- java 添加自己的工具包
一. 在添加工具包前环境变量要定位到当前目录, export CLASSPATH=.:/home/share/ 添加工具类 我的目录\\192.168.1.101\share\share\net\fe ...
- Linux 基础——权限管理命令chmod
一.Linux中的文件权限与目录权限 Linux中定义了3种访问权限,分别是r.w.x.其中r表示对象是可读的,w表示对象是可写的,x表示对象是可执行的,这3种权限组成一组rwx分别对应对象的3个安全 ...
- SqlServer Case when then用法总结
SELECT d.DicName , --DevelopMode ISNULL(NULL,NULL) , --Orgid b.FullName , --Areid c.DicName , --Inve ...