链接:

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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. [转]MySQL innodb buffer pool
  2. cAdvisor0.24.1+InfluxDB0.13+Grafana4.0.2搭建Docker1.12.3 Swarm集群性能监控平台
  3. [原][CSS3]会动的盒子机器人
  4. ACM训练计划建议(写给本校acmer,欢迎围观和指正)
  5. Oracle数据库入门——高水位线详解
  6. Java Classloader原理分析
  7. log4j中存在日志无法打印问题解决
  8. Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
  9. 关于用POI和EXCEL交互的问题
  10. .net FrameWork4.0安装未成功
  11. 事件CEvent的使用 .
  12. Linux下实现秒级定时任务的两种方案(crontab 每秒运行)
  13. .net core 注入中的三种模式:Singleton、Scoped 和 Transient
  14. 衡量GDP,哪种夜间灯光数据更靠谱?
  15. echarts生成的图表大小怎么随屏幕的大小改变自适应
  16. mybatis 中 使用 allowMultiQueries=true
  17. ArcGIS for qml -添加自由文本
  18. Python enumerate() 函数
  19. 2111: [ZJOI2010]Perm 排列计数
  20. android studio样式文件汇总

热门文章

  1. Mac下MAMP初试体验
  2. Swift - 使用Core Data进行数据持久化存储
  3. appium 真机测试问题 出现 instruments crashed on startup
  4. js / ajax 成功提交后怎么跳转到另外一个页面?
  5. 基于visual Studio2013解决面试题之0405和最大的子矩阵
  6. TTL 超时问题
  7. android在eclipse中打包(签名包)方法及常见问题解决
  8. Java基础:泛型及其擦除性、不可协变性
  9. 小言C指针
  10. C++ operator overload -- 操作符重载