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

最新文章

  1. SQL Server中的GUID
  2. Weblogic部署项目过程中的一些问题
  3. [课程设计]Scrum 1.7 多鱼点餐系统开发进度
  4. C#联合Union的实现方式
  5. jq选中问题
  6. android network develop(2)----network status check
  7. Byte History
  8. Java--&gt;分割文件
  9. const 成员方法
  10. 使用Python扫描端口情况
  11. UNIX时间与本地时间的转换
  12. 三维偏序-二维LIS
  13. LinearLayout具体解释一:LinearLayout的简单介绍
  14. 企业为什么要实行ERP系统,它到底有什么好处呢?
  15. jquery 中时间的运用
  16. Visual Studio 环境路径答疑!
  17. 设备指纹(Device Fingerprinting)是什么?
  18. 前端安全之CSRF
  19. DOS下的安全空间
  20. vue2.0学习笔记之路由(二)路由嵌套+动画

热门文章

  1. Difference between &lt;? super T&gt; and &lt;? extends T&gt; in Java
  2. JS中的Math.ceil和Math.floor函数的用法
  3. js控制select数据绑定下拉列表
  4. c# winform InvokeRequired 解决跨线程访问控件
  5. linux中文乱码问题及locale详解
  6. 理解JavaScript中作用域链的关系
  7. js中substring和substr的用法 (转)
  8. iOS_SN_CocoaPods使用详细说明( 转)
  9. C#使用多态求方形面积周长和圆的面积周长
  10. ZOJ 1633