HDU 4790 Just Random 数学
链接: pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790
意:从【a。b】中随机找出一个数字x,从【c。d】中随机找出一个数字y。给出p。m,假设(x+y)%p==m则算成功,问成功的概率是多少。
思路:【a。b】中连续p个数。【c,d】中连续p个数。用这2*p个数进行组合能找到p种的成功组合(详细不证),所以找到【a。b】中p循环的个数x1,【c,d】中p循环的个数y1,则它们组成的成功组合数为p*x1*y1。
然后是处理边界的问题,首先x或y处于边界的数的数量一定不超过p-1个,设分别有x2,y2个,这x2。y2个数一定能在相应的每一个循环中找到相应的成功组合。所以还要多出x2*y1+y2*x1个成功组合数。
最后还要在这x2,y2个数里找到互相相应的数。由于要求x2+y2 ≡ m(mod p),所以(m-x2) ≡ y2(mod p),然后找到它们重叠的部分就可以。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#define PI acos(-1.0)
#define maxn 10005
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int main()
{
int T;
scanf("%d",&T);
for(int ii=1; ii<=T; ii++)
{
LL a,b,c,d,p,m;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
LL x=b-a+1,y=d-c+1;
LL xx=(b-a+1)/p;
LL yy=(d-c+1)/p;
LL xxx=x-xx*p;
LL yyy=y-yy*p;
LL t=xx*yy*p;
LL fm=x*y;
t+=xxx*yy;
t+=yyy*xx;
if(xxx!=0&&yyy!=0)
{
LL head=a%p;
LL tail=b%p;
LL head1=((m-tail)%p+p)%p;
LL tail1=((m-head)%p+p)%p;
LL head2=c%p;
LL tail2=d%p;
//cout<<head1<<" "<<tail1<<" "<<head2<<" "<<tail2<<endl;
if(tail1>=head1&&tail2>=head2)
t+=(max(0LL,(min(tail2,tail1)-max(head1,head2))+1));
else if(tail1>=head1&&tail2<head2)
{
if(tail1<=tail2)
t+=max(0LL,tail1-head1+1);
else if(tail1>tail2&&tail1<head2)
t+=max(0LL,tail2-head1+1);
else t+=max(0LL,tail1-max(head1,head2)+1)+max(0LL,tail2-head1+1);
}
else if(tail2>=head2&&tail1<head1)
{
if(tail2<=tail1)
t+=max(0LL,tail2-head2+1);
else if(tail2>tail1&&tail2<head1)
t+=max(0LL,tail1-head2+1);
else t+=max(0LL,tail2-max(head1,head2)+1)+max(0LL,tail1-head2+1);
}
else t+=(p-max(head1,head2)+min(tail1,tail2)+1+max(0LL,tail1-head2+1)+max(0LL,tail2-head1+1));
}
printf("Case #%d: ",ii);
if(t==0)
printf("0/1\n");
else
{
LL ttt=__gcd(t,fm);
printf("%I64d/%I64d\n",t/ttt,fm/ttt);
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 4790 Just Random 数学的更多相关文章
- hdu 4790 Just Random (2013成都J题) 数学思路题 容斥
题意:在[a,b] [c,d] 之间,和模p等于m的对数 详见代码 #include <stdio.h> #include <algorithm> #include < ...
- 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 (2013成都J题)
Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 4790 Just Random
思路:对于a<=x<=b,c<=y<=d,满足条件的结果为ans=f(b,d)-f(b,c-1)-f(a-1,d)+f(a-1,c-1). 而函数f(a,b)是计算0<= ...
- hdoj 4790 Just Random 【数学】
题目:hdoj 4790 Just Random 题意:给你两个闭区间[a,b],[c,d],分别从中等可能的跳出 x 和 y ,求(x+y)%p == m的概率 分析: 假如是[3,5] [4,7] ...
- HDU 4790:Just Random(容斥)
Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Just Random HDU - 4790 思维题(打表找规律)分段求解
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...
- hdu 4790 数学
/* 题意:给你二个区间[a,b]和[c,d] 分别从中选一个数x和y使的(x+y)%p=m; 可以这样来求,先求出(0->b和0->d区间段的值)-(区间0->a-1和0-> ...
随机推荐
- WordPress的用户系统总结
原文发表自我的个人主页,欢迎大家訪问~转载请保留本段,或注明原文链接:http://www.hainter.com/wordpress-user-module keyword:WordPress,用户 ...
- RobotFramework 数据分离
RobotFramework 数据分离分为三种 1. Run 添加参数 2. 外部的Python文件 3. 读取Excel Run 添加参数 (适用于jenkins) 使用-v 参数名:参数值 使用外 ...
- 基于Spring提供支持不同设备的页面
基于Spring来检测访问Web页面的设备是很简单的,在这个经验中我们讲到过.通常不同的设备访问我们是通过响应式设计来统一处理各种设备的尺寸的.但是如果希望针对不同的设备,显示不同的内容呢? Spri ...
- Android 百度地图 SDK v3.0.0 (三) 加入覆盖Marker与InfoWindow使用
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37737213 上篇博客已经实现了地图的定位以及结合了方向传感器用户路痴定位方向, ...
- 什么是防盗链设置中的空Referer
设置防盗链时候指明和不指明空Referer的差别及实现后的效果? 什么是Referer? 这里的 Referer 指的是HTTP头部的一个字段,也称为HTTP来源地址(HTTP Referer).用来 ...
- Linux红黑树(二)——访问节点
核心对红黑树使用两点说明 1.头文件 <Documentation/rbtree.txt> Linux's rbtree implementation lives in the file ...
- qsettings 保存自定义结构体(QVariant与自定义结构体相互转化)
参考博文:QVariant与自定义数据类型转换的方法. 这里摘取其关键内容: 1.将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别. 2.在插入对象的时候,声明QVa ...
- 【Visual C++】绘图函数BitBlt的使用方法
BitBlt 该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境. 原型: BOOL BitBlt( HDC hdcDest, int nXDest, ...
- 转]解析C语言中的sizeof
解析C语言中的sizeof 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出 了其操作数的存储大小.操作数 ...
- “HTTP 错误 401.1 - 未授权:登录失败” iis配置和权限问题
今天,将项目发布到IIS服务器上,出现此问题,本地IIS访问正常. 登录失败说明根本登录不了,谈何访问网页,所以要从两方面下手,一.账户:二.账户权限: 一.设置你网站属性的时候,目录安全性-匿名访问 ...