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. lesson8:AtomicInteger源码解析及性能分析

    AtomicInteger等对象出现的目的主要是为了解决在多线程环境下变量计数的问题,例如常用的i++,i--操作,它们不是线程安全的,AtomicInteger引入后,就不必在进行i++和i--操作 ...

  2. emacs快捷键学习(一)--Linux最强大的编辑器

    emacs是一个非常强大的编辑器.经常使用的快捷键总结例如以下: 退出emacs:ctrl+x ctrl+c 移动到下一屏:ctrl+v 移动到上一屏:alt+v 将光标所在行移动到屏幕中间:ctrl ...

  3. windows下php+apache+mysql环境搭建

    在Windows 7下进行PHP环境搭建,首先需要下载PHP代码包和Apache与Mysql的安装软件包. PHP版本:php-5.3.2-Win32-VC6-x86,VC9是专门为IIS定制的,VC ...

  4. 坑爹的vector iterators incompatible错误(VS中属性页-->C/C++-->代码生成-->>运行库)

    之前一直被这个错误折磨着,就是不知道问题在那,后来找了很多资料,大概都是说这是因为多个线程同时操作vector的问题(参考这里).可是我这里的代码并没有问题,因为同样的代码在别的解决方案中已经成功运行 ...

  5. (一)MVP设计模式

    一.MVP介绍      随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互,同时 ...

  6. Ajax&XMLHttpRequest

    XMLHttpRequest 简单省力的方法 将文件编码成base64通过Ajax上传 HTML5学习之FileReader接口 HTML5学习之FileReader接口 通过Ajax方式上传文件,使 ...

  7. 结合tcpdump命令对traceroute深入分析

    昨天突然被问到traceroute的原理,一时竟也说不出来,有些命令平时虽然经常在用,但实际原理确并不了解,趁这次机会就来梳理一下. traceroute:是网络诊断中,用来分析IP包经过那些路由的命 ...

  8. 东软实训3-servlet基础

    Java Servlet技术的核心就是在服务端创建能响应用户请求的对象,被创建的对象习惯上称为一个Servlet对象. 编写一个创建servlet对象的类就是编写一个特殊类的子类,这个特殊的类就是ja ...

  9. ECMAscript v.s. Javascript

    ECMAscript是一种中性的语言,中性表示与所处环境(宿主环境)无关(客户端/服务器/浏览器),它仅仅是一个纯粹意义上的语言. ECMAscript-262定义了这门语言的基础,或者说规则(比如说 ...

  10. ubuntu 快捷键和安装知识知识

    本文节选自“The Official Ubuntu Book, 7th Edition.pdf” 快捷键部分直接引用原书中图片. Linux Folders Learning Unity Keyboa ...