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. JSBinding+Bridge.NET:Inspector拖变量支持

    之前的文档说了,JSB的设计是不允许gameObject上挂逻辑脚本的.原因很简单,在Js工程中根本就不存在C#形式的逻辑脚本,如果在Cs工程中挂上了,到了Js工程这边,直接Missing. 实际在使 ...

  2. es6笔记(1) 概要

    什么是ES6 ECMAScript 6.0 (简称ES6) 是继ECMAScript 5.1以后的javascript 语言的下一代标准,在2015年6月份发布. 他的目标是使javascript语言 ...

  3. iOS 自己封装的网络请求,json解析的类

    基本上所有的APP都会涉及网络这块,不管是用AFNetWorking还是自己写的http请求,整个网络框架的搭建很重要. 楼主封装的网络请求类,包括自己写的http请求和AFNetWorking的请求 ...

  4. Loadrunner里面的深入理解Resource 的 0和1

    最近在倒腾loadrunner,发现一些非常有意思的配置项,也许同学们平时去玩的时候,没有注意这些点.我也查阅了网上的帖子,说的都不够详细~操作起来的话,同学们也只是看到文字的描述,并不能发现区别.今 ...

  5. 转载 为什么print在Python 3中变成了函数?

    转载自编程派http://codingpy.com/article/why-print-became-a-function-in-python-3/ 原作者:Brett Cannon 原文链接:htt ...

  6. Java基础打包以及批处理命令运行

    1.前期准备

  7. python中的*号

    from:https://www.douban.com/note/231603832/ 传递实参和定义形参(所谓实参就是调用函数时传入的参数,形参则是定义函数是定义的参数)的时候,你还可以使用两个特殊 ...

  8. 【技术知识】恶意PDF文件分析-PDFdump的问题

    1.提醒 百度分析恶意PDF文件,很多都是推荐PDFdump.在某次沙箱产品分析出疑似高级威胁的PDF样本后,我使用PDFdump查看ShellCode的加密数据,分析后并没有找到相关的ShellCo ...

  9. Linux下USB suspend/resume源码分析【转】

    转自:http://blog.csdn.net/aaronychen/article/details/3928479 Linux下USB suspend/resume源码分析 Author:aaron ...

  10. android休眠唤醒驱动流程分析【转】

    转自:http://blog.csdn.net/hanmengaidudu/article/details/11777501 标准linux休眠过程: l        power managemen ...