[原创]hdu 4790 Just Random 2013 Asia Chengdu Regional Contest [数学]【思维】
[原创]hdu 4790 Just Random 2013 Asia Chengdu Regional Contest [数学]【思维】
2016-10-05 19:28:29 Tabris_ 阅读数:282
博客爬取于2020-06-14 22:43:05
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52740134
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4790
------------------------------.
Just Random
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2782 Accepted Submission(s): 850
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
2013 Asia Chengdu Regional Contest
-----------------------------.
题目大意:
给你两个区间[a,b][c,d]
求分别从两个区间中取出x,y. 使得(x+y)mod p==m的概率
解题思路 :
概率就是
可行的组数/所有的组数
所有的组数就是(b-a+1)*(d-c+1)
问题就是怎么求解可行的组数
如果暴力的话 最坏是O(10^18) 一定会超时
如果把区间对p取模 存在数组里然后在选取 时空复杂度均为O(10^9) 还是不可行
于是 就改变一下思路
他要求的是(x+y)mod p==m 那么x+y的区间就是
[a+c,b+d]
这样的话 会发现 [a+c,b+d]中每个数出现的次数虽然不相等但是有非常严谨的规律 如下图
明确这些就很好算了
只有分别对这三个区间进行统计就行了
但是为了方便计算这里讲[c,d]区间变成了[c+p-m,d+p-m]
这样的话选出来的数就相当于(x+y)mod p==0
统计的时候就方便多了
统计的时候
每一个区间只要找到最大和最小的满足mod p==0的数的位置
然后用高斯公式就能直接计算了 ::(首项+末项)*项数/2
为了避免重复统计 鄙渣做了2点改动
1.判断的区间为[A1,A2] (A2,A3) [A3,A4]
2.特判了一下(a==b&&c==d)的情况 否则按照我的思路就会多统计一遍
1 | if(a==b&&c==d) |
附本题代码
---------------------------.
1 | # include <bits/stdc++.h> |