Just Random

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 87    Accepted Submission(s): 34

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:
  1. Coach Pang randomly choose a integer x in [a, b] with equal probability.
  2. Uncle Yang randomly choose a integer y in [c, d] with equal probability.
  3. If (x + y) mod p = m, they will go out and have a nice day together.
  4. 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(0 <= a <= b <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).
 
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
4
0 5 0 5 3 0
0 999999 0 999999 1000000 0
0 3 0 3 8 7
3 3 4 4 7 0
 
Sample Output
Case #1: 1/3
Case #2: 1/1000000
Case #3: 0/1
Case #4: 1/1
 
Source
 

这题就是要找在[a,b]  [c,d] 之间,和模p等于m的对数。

把[a,b] [c,d]所有可能组合的和写成下列形式。

a+c  a+c+1  a+c+2   ..................a+d

a+c+1  a+c+2  a+c+3 ........a+d  a+d+1

a+c+2  a+c+3         a+d   a+d+1   a+d+2

....................

...................

b+c   b+c+1   ...............................................b+d;

上面大致形成一个斜的矩阵。

使用b+c  和 a+d两条竖线,就可以分成三部分。前后两部分个数是等差数列,中间个数是相等的。

只需要讨论下b+c 和 a+d的大小。  然后找到%p==m 的位置,求和就可以搞定了。

  1. /* ***********************************************
  2. Author :kuangbin
  3. Created Time :2013-11-16 13:20:40
  4. File Name :E:\2013ACM\专题强化训练\区域赛\2013成都\1010.cpp
  5. ************************************************ */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. using namespace std;
  20.  
  21. long long gcd(long long a,long long b)
  22. {
  23. if(b == )return a;
  24. return gcd(b,a%b);
  25. }
  26. int main()
  27. {
  28. //freopen("in.txt","r",stdin);
  29. //freopen("out.txt","w",stdout);
  30. long long a,b,c,d,p,m;
  31. int T;
  32. int iCase = ;
  33. scanf("%d",&T);
  34. while(T--)
  35. {
  36. iCase++;
  37. scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
  38. long long ans = ;
  39. if(b+c <= a+d)
  40. {
  41. long long t1 = (a+c)%p;
  42. long long add = (m - t1 + p)%p;
  43. long long cnt1 = (a+c + add-m)/p;
  44. //cout<<t1<<" "<<add<<endl;
  45. long long t2 = (b+c-)%p;
  46. long long sub = (t2 - m + p)%p;
  47. long long cnt2 = (b+c--sub-m)/p;
  48. //cout<<t2<<" "<<sub<<endl;
  49. ans += (cnt2 - cnt1 + )*(+add) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/ * p;
  50. //printf("%I64d %I64d %I64d\n",cnt1,cnt2,ans);
  51. t1 = (b+c)%p;
  52. add = (m - t1 + p)%p;
  53. cnt1 = (b+c+add-m)/p;
  54. t2 = (a+d)%p;
  55. sub = (t2 - m + p)%p;
  56. cnt2 = (a+d-sub-m)/p;
  57. ans += (cnt2 - cnt1 + )*(b-a+);
  58. t1 = (a+d+)%p;
  59. add = (m - t1 + p)%p;
  60. cnt1 = (a+d++add-m)/p;
  61. t2 = (b+d)%p;
  62. sub = (t2 - m + p)%p;
  63. cnt2 = (b+d-sub-m)/p;
  64. ans += (cnt2 - cnt1 + )*(+sub) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/*p;
  65. }
  66. else
  67. {
  68. long long t1 = (a+c)%p;
  69. long long add = (m - t1 + p)%p;
  70. long long cnt1 = (a+c + add-m)/p;
  71. long long t2 = (a+d-)%p;
  72. long long sub = (t2 - m + p)%p;
  73. long long cnt2 = (a+d--sub-m)/p;
  74. ans += (cnt2 - cnt1 + )*(+add) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/ * p;
  75. t1 = (a+d)%p;
  76. add = (m - t1 + p)%p;
  77. cnt1 = (a+d+add-m)/p;
  78. t2 = (b+ c)%p;
  79. sub = (t2 - m + p)%p;
  80. cnt2 = (b+c-sub-m)/p;
  81. ans += (cnt2 - cnt1 + )*(d-c+);
  82. t1 = (b+c+)%p;
  83. add = (m - t1 + p)%p;
  84. cnt1 = (b+c++add-m)/p;
  85. t2 = (b+d)%p;
  86. sub = (t2 - m + p)%p;
  87. cnt2 = (b+d - sub-m)/p;
  88. ans += (cnt2 - cnt1 + )*(+sub) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/*p;
  89. }
  90. long long tot = (b-a+)*(d-c+);
  91. long long GCD = gcd(ans,tot);
  92. ans /= GCD;
  93. tot /= GCD;
  94. printf("Case #%d: %I64d/%I64d\n",iCase,ans,tot);
  95. }
  96. return ;
  97. }

HDU 4790 Just Random (2013成都J题)的更多相关文章

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

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

  2. HDU 4786 Fibonacci Tree (2013成都1006题)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 4733 G(x) (2013成都网络赛,递推)

    G(x) Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契

    题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...

  5. 2014 HDU多校弟六场J题 【模拟斗地主】

    这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...

  6. 2014 HDU多校弟五场J题 【矩阵乘积】

    题意很简单,就是两个大矩阵相乘,然后求乘积. 用 Strassen算法 的话,当N的规模达到100左右就会StackOverFlow了 况且输入的数据范围可达到800,如果变量还不用全局变量的话连内存 ...

  7. HDU 4790 Just Random 数学

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

  8. hdu 4790 Just Random (思路+分类计算+数学)

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

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

随机推荐

  1. argunlar 1.0.1 【数据绑定】

    <!DOCTYPE html><html lang="en" ng-app><head>    <meta charset="U ...

  2. shell脚本常用参数

    shell 脚本 常用参数 #!/bin/sh # 在脚本第一行脚本头 # sh为当前系统默认shell,可指定为bash等shell sh -x # 执行过程 sh -n # 检查语法 (a=bbk ...

  3. Caffe 碎碎念

    Window Data Layer window data layer 的数据是存在硬盘上的图片, 需要在一个txt里指定用于训练或测试的图片以及bounding box, bounding box ...

  4. [转]python ctypes 探究 ---- python 与 c 的交互

    近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ctypes 模块.同时,虽然网上有这方面的内容,但是感觉还是没说清楚.这里记录 ...

  5. 微信小程序调用接口返回数据或提交数据

    由于小程序发起网络请求需要通过 wx.request 文档地址 https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html 习 ...

  6. C/C++杂记:深入理解数据成员指针、函数成员指针

    1. 数据成员指针 对于普通指针变量来说,其值是它所指向的地址,0表示空指针. 而对于数据成员指针变量来说,其值是数据成员所在地址相对于对象起始地址的偏移值,空指针用-1表示.例: 代码示例: str ...

  7. python里面的引用

    1 对象及其引用 python中,引用是用命名空间来实现的,命名空间维护了变量和对象之间的引用关系. myInt = 27 yourInt = myInt #change the value of y ...

  8. kali linux2.0安装vega

    1.到官网下载安装包(选择版本:我这里以Linux64位为例) vega下载地址:https://subgraph.com/vega/download/ 2.解压到指定目录: unzip VegaBu ...

  9. idea导入maven项目,包没有自动下载

    解决办法: 先把这里面的全部删掉 然后在pom.xml里面右键 这样就会重新下载包

  10. CAS单点登录流程

    CAS的官方站点: https://apereo.github.io/cas/5.2.x/index.html 概念解读: The TGT (Ticket Granting Ticket), stor ...