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( <= a <= b <= , <=c <= d <= , <= m < p <= ).
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

 
Sample Output
Case #: / 
Case #: /
Case #: /
Case #: /
 
Source
 

思路:对于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 ;
}
Recommend

hdu 4790 Just Random (思路+分类计算+数学)的更多相关文章

  1. hdu 4790 Just Random (2013成都J题) 数学思路题 容斥

    题意:在[a,b]  [c,d] 之间,和模p等于m的对数 详见代码 #include <stdio.h> #include <algorithm> #include < ...

  2. HDU 4790 Just Random 数学

    链接:pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790 意:从[a.b]中随机找出一个数字x,从[c.d]中随机找出一个 ...

  3. 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<= ...

  4. 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); ...

  5. HDU 4790 Just Random (2013成都J题)

    Just Random Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdoj 4790 Just Random 【数学】

    题目:hdoj 4790 Just Random 题意:给你两个闭区间[a,b],[c,d],分别从中等可能的跳出 x 和 y ,求(x+y)%p == m的概率 分析: 假如是[3,5] [4,7] ...

  7. HDU 6665 Calabash and Landlord (分类讨论)

    2019 杭电多校 8 1009 题目链接:HDU 6665 比赛链接:2019 Multi-University Training Contest 8 Problem Description Cal ...

  8. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  9. QuantLib 金融计算——数学工具之数值积分

    目录 QuantLib 金融计算--数学工具之数值积分 概述 常见积分方法 高斯积分 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--数学工具之数值积分 载入模 ...

随机推荐

  1. [原创]# 玩转nginx系列

    首先先上如何彻底删除nginx 看到这个标题的小伙伴都惊呆了,还不知道怎么搞,却叫我怎么卸载.为什么我要这样,其实,Reset也是一种解决问题的方式嘛. 首先执行下卸载命令 sudo apt-get ...

  2. Android开发之发送短信

    本实例通过SmsManager的sendTextMessage方法实现发送短信关于SmsManager的具体解释大家能够參照:Android开发之SmsManager具体解释 实例执行效果图: 程序代 ...

  3. Web服务的体系架构

    Web简介: Web是WWW(World Wide Web)的简称,又称为万维网,是建立在客户机/服务器上的,以HTML语言和HTML协议为基础,提供面向Internet服务的,有一致用户界面的一种信 ...

  4. Java 多字段排序Comparator(兼容Date,Integer,Doubel,Long)

    Java 反射类:ReflexUtil public class ReflexUtil { static Logger logger = LoggerFactory.getLogger(ReflexU ...

  5. Android开发编码规范(自用)

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!   Android开发编码规范 目的及指导原则 目的 统一规范 Eclipse编辑环境 ...

  6. 通过 yum update 将系统从CentOS 6.2 升级到 CentOS 6.6 及升级过程中的简单排错

    本文说明 本文写于2014年的WP中,后WP停止维护,今天翻到此记录整理下,记录于此,方便日后查看. 话说那时候写博客真是认真啊~哈哈~ 升级前的系统信息 [root@thatsit ~]# unam ...

  7. (三)《Java编程思想》——构造函数初始化

    1.初始化顺序是由变量在类内的定义顺序决定的,并且先初始化变量,然后才调用构造函数. package chapter4; //: OrderOfInitialization.java /** * 初始 ...

  8. ORACLE查询数据库的锁表情况

      查询数据库的锁表情况语句如下: SELECT p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_na ...

  9. Swift 流程控制

    import Foundation ...{ == { print(index) } } // 可选变量 类型后面加? var myName:String?="jikexueyuan&quo ...

  10. UIApplication详解再解-备

    每个app有且只有一个UIApplication对象,当程序启动的时候通过调用UIApplicationMain方法得到的.可以通过sharedApplication方法得到. UIApplicati ...