链接:

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

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

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 (思路+分类计算+数学)

    Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with ...

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

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

  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

    思路:对于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<= ...

  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 4790:Just Random(容斥)

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

  8. Just Random HDU - 4790 思维题(打表找规律)分段求解

    Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...

  9. hdu 4790 数学

    /* 题意:给你二个区间[a,b]和[c,d] 分别从中选一个数x和y使的(x+y)%p=m; 可以这样来求,先求出(0->b和0->d区间段的值)-(区间0->a-1和0-> ...

随机推荐

  1. C语言sendto()函数-经socket传送数据以及recvfrom函数《转》

    相关函数:send, sendmsg, recv, recvfrom, socket 头文件:#include <sys/types.h>   #include <sys/socke ...

  2. 成都大数据Hadoop与Spark技术培训班

    成都大数据Hadoop与Spark技术培训班   中国信息化培训中心特推出了大数据技术架构及应用实战课程培训班,通过专业的大数据Hadoop与Spark技术架构体系与业界真实案例来全面提升大数据工程师 ...

  3. linux下的php网站放到Windows服务器IIS下.htaccess文件伪静态规则转换

    此办法只适合于linux下的php网站放到Windows服务器IIS下 ,  网站除了主页面正常以外  子页面全部出现404错误    这里子页面出现404 错误是说明伪静态没有开启 什么是.htac ...

  4. [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...

  5. 使MYSQL能被外部访问_xeyuu_新浪博客

    使MYSQL能被外部访问_xeyuu_新浪博客 使MYSQL能被外部访问 (

  6. android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用

    android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用,只要把Imageview的src给去掉就成了,src捕获了bac ...

  7. BZOJ 2096([Poi2010]Pilots-单调队列-差值)

    2096: [Poi2010]Pilots Time Limit: 30 Sec   Memory Limit: 162 MB Submit: 190   Solved: 97 [ Submit][ ...

  8. 很具体GC学习笔记

    GC学习笔记 这是我公司同事的GC学习笔记,写得蛮具体的,由浅入深,循序渐进,让人一看就懂,特转到这里. 一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 V ...

  9. sql,nosql

    1. 关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库. 关系模型是在1970年由IBM的研究员E.F.Codd博士首先提出的,在之后的几十年中,关系模型的概念得到了充分的发展并逐渐成 ...

  10. Pods was rejected as an implicit dependency for &#39;libPods.a&#39; because its architectures &#39;x86_64&#39; didn

    引入cocoaPods后.第一次编译,或者运行update后 可能报这个错误: Pods was rejected as an implicit dependency for 'libPods.a' ...