hdu 4790 Just Random (思路+分类计算+数学)
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.
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( <= a <= b <= , <=c <= d <= , <= m < p <= ).
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).
Case #: /
Case #: /
Case #: /
Case #: /
思路:对于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<=x<=a,0<=y<=b满足条件的结果。这样计算就很方便了。
例如:求f(16,7),p=6,m=2.
对于x有:0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4
对于y有:0 1 2 3 4 5 0 1
很容易知道对于xy中的(0 1 2 3 4 5)对满足条件的数目为p。
这样取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)。
这样就可以分成4部分来计算了。
f(16,7)=A和C满足条件的数+A和D满足条件的数+B和C满足条件的数+B和D满足条件的数。
其中前3个很好求的,关键是B和D满足条件的怎么求!
这个要根据m来分情况。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
ll a,b,c,d,p,m;
ll gcd(ll x,ll y)
{
return y==?x:gcd(y,x%y);
}
ll f(ll x,ll y)
{
if(x< || y<)
return ;
ll mx=x%p;
ll my=y%p;
ll ans=;
ans=ans+(x/p)*(y/p)*p;//
ans=ans+(x/p)*(my+); //
ans=ans+(y/p)*(mx+);// if(mx>m)//
{
ans=ans+min(my,m)+;
ll t=(p+m-mx);
if(t<=my)
ans=ans+my-t+;
}
else//
{
ll t=(p+m-mx)%p;
if(t<=my) ans=ans+min(my-t+,m-t+);
}
return ans;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--)
{
printf("Case #%d: ",++ac);
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
ll ans=f(b,d)-f(b,c-)-f(a-,d)+f(a-,c-);
ll tot=(b-a+)*(d-c+);
ll r=gcd(ans,tot);
printf("%I64d/%I64d\n",ans/r,tot/r);
}
return ;
}
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 数学
链接:pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790 意:从[a.b]中随机找出一个数字x,从[c.d]中随机找出一个 ...
- 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<= ...
- 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 (2013成都J题)
Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdoj 4790 Just Random 【数学】
题目:hdoj 4790 Just Random 题意:给你两个闭区间[a,b],[c,d],分别从中等可能的跳出 x 和 y ,求(x+y)%p == m的概率 分析: 假如是[3,5] [4,7] ...
- HDU 6665 Calabash and Landlord (分类讨论)
2019 杭电多校 8 1009 题目链接:HDU 6665 比赛链接:2019 Multi-University Training Contest 8 Problem Description Cal ...
- HDU 5073 Galaxy (2014 Anshan D简单数学)
HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...
- QuantLib 金融计算——数学工具之数值积分
目录 QuantLib 金融计算--数学工具之数值积分 概述 常见积分方法 高斯积分 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--数学工具之数值积分 载入模 ...
随机推荐
- (原)Apache添加完限速模块后的文件
点我下载 解压后得到apache2文件夹和readme.txt文本 按照readme.txt修改apache2文件夹.
- MySQL删除外键定义的方法
MySQL外键在定以后,如果我们不再需要这个外键,可以进行删除操作,下面就为您介绍MySQL删除外键定义的方法,供您参考. 不知道大家有没有发现,在定义外键的时候articles.member_id外 ...
- Stack and queue.
队列的定义及基本运算 1.定义 队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表 (1)允许删除的一端称为队头(Front). (2)允许插入的一端称为队尾(Rea ...
- intent-filter data Uri 意图过滤器 详解
组件的intent-filter属性 如果一个 Intent 请求在一片数据(Uri)上执行一个动作(Action), Android 如何知道哪个应用程序的哪个组件能用来响应这个请求 ...
- C# Socket传输大文件
1.基础类TransferFiles,client和server都需要 using System; using System.Collections.Generic; using System.Tex ...
- 3 windows环境与shell交互操作
/** * 由SshConfig配置获取一个Session * @param conf * @return */ public static Session createSession(SshConf ...
- UVALive 5792 Diccionário Portuñol
字符串匹配问题 有n个a串个m个b串,讲a的前缀和b的后缀粘在一起有多少个不同的新串. 首先求不同的前缀和后缀肯定好求了,就用字典树分别存一下a个倒过来的b. 那个问题就是解决例如,abcd,和bcd ...
- Web项目中JSP页面的一种调试方法与出现的问题 -- SpringMVC架构测试
在前端开发中,尤其是MVC架构多人开发,负责前端的童鞋总是需要做静态页面,再和后台连接前无法使用变量如EL表达式等测试功能,所以本人引入了一个模板jsp数据测试专用文件,专门配置所有的变量,然后在待测 ...
- memcached全面剖析
memcached介绍如今,越来越多的Web应用程序开始使用memcached这个高速的缓存服务器软件.然而,memcached的基础知识远远未能像其他Web技术那样普及,memcached在国内的大 ...
- [转]NopCommerce MVC 插件机制分析
原文地址:http://www.cnblogs.com/haoxinyue/archive/2013/06/06/3105541.html 基本原理 插件话的应用程序一般都是先定义插件接口,然后把插件 ...