HDU 4790 Just Random

78 篇文章 0 订阅

Just Random

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


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 <= 10 9, 0 <=c <= d <= 10 9, 0 <= m < p <= 10 9).
 

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
 

寒假训练的一道题,在比赛中,对这种数学题格外情有独钟,两个小时一直在想这个题,当有思路时已经没时间了,比赛结束又是各种WA,主要原因在于 边界没有处理好!

题目大意:

给你两个闭区间:[a,b],[c,d],让你在这两个区间内各选一个数,x,y,问取到的数满足(x+y)mod p = m的概率!

分析:
这个题大体思路很好想:那就是找到符合条件的,除以总情况, 总情况很好找:就是(b-a+1) * (d-c+1);
关键是符合条件的:
可以给(x+y)mod p = m变一下型,变成 n * p +m = ( x + y );这里n是整数。这里n相当于整体解!就是余数相同时,有几个结果!
在变换一下: y = -x + (n*p + m)   很明显 这是一次函数,只有p是变量,因此他是一个上下平移的直线!
很明显  总情况就是 y∈[ c , d ]  ,x ∈ [ a , b ]的由点构成的矩形,就是求直线与矩形的交点的个数。这就是所有符合条件的解!
一开始我的做法是直接循环扫描 就是一条直线一条直线的扫,结果超时!因此,这个题目必须把边界球出来,直接用数学算出来  不能循环!
边界分析:



很明显 四条直线把整个矩形分成三个区域,而求交点,显然A1是递减的等差数列,A2是常数列(全都是b-a+1个交点),A3是递增的等差数列!
只需要求出n1 n2 n3 n4,边界在求数列和即可!
需要注意的是这个图是适合c-d > b-a的情况,  对于b-a > d-c的情况可以把bd  ca交换,相当于y ∈[a,b]不管怎样变换  只是形式的变换 结果不可能变得 直接swap即可!
这里我错了很多次,是因为边界没处理好,就是有重复计算!
可以把没一个区域具体化!就是A1有 n11 和 n12 构成  A2 由 n21 n22 构成  A3 由 n31 n32 构成 !
不管怎样总有一个适合自己的方法!
最后还要除以最大公约数写个gcd()即可!

代码如下:
 
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)return a;
    return gcd(b,a%b);
}
ll a,b,c,d,p,m;
ll f(ll x,ll n){
    return -x+(n*p+m);
}
int main()
{
    ll n;
    scanf("%lld",&n);
    for (ll cont = 1;cont <= n; ++cont){
        scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&p,&m);
        ll n11,n12,n21,n22,n31,n32;
        if (b-a > d-c){
            swap(b,d);
            swap(c,a);
        }
        n11 = ceil((double)(a+c-m)/p);
        n12 = (b+c-m )% p == 0 ? (b+c-m) / p - 1 : floor((double)(b+c-m)/p);// == n1;
        n21 = (b+c-m )% p == 0 ? (b+c-m) / p : ceil((double)(b+c-m)/p);
        n22 = (a+d-m)%p == 0 ? (a+d-m) / p : floor((double)(a+d-m)/p);
        n31 = (a+d-m)%p == 0 ? (a+d-m) / p + 1: ceil((double)(a+d-m)/p);
        n32 = (b + d - m )% p == 0 ? (b+d-m)/p : floor((double)(b+d-m)/p);
        ll sum=0;
        sum += (n22 - n21 + 1) * (b - a + 1);
        ll b2 = n11 * p + m - c;
        ll st1 =  b2 - a + 1;
        ll num1 = n12 - n11 + 1;
        sum +=  num1*st1 + (num1-1)*num1*p / 2;
        ll a2 = n32 * p + m - d;
        ll st2 = b - a2 + 1;
        ll num2 = n32-n31+ 1;
        sum+=num2*st2+num2*(num2-1)*p/2;
        ll len=(d-c+1)*(b-a+1);
        ll gd=gcd(len,sum);
        printf("Case #%lld: %lld/%lld\n",cont,sum/gd,len/gd);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值