• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

2013 Asia Chengdu Regional Contest J

Just Random

Time Limit: 2000/1000 MS (Java/Others)   

Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 0    Accepted Submission(s): 0

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
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define ll __int64
 7 ll gcd(ll n, ll m){ return m ? gcd(m, n%m) : n; }
 8 ll n, m, a, b, c, d, p,t1, t2, cnt1, cnt2, add, sub,s;
 9 int main(){
10     int t, cas = 1;
11     scanf("%d", &t);
12     while (t--){
13         s = 0;
14         scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &a, &b, &c, &d, &p, &m);
15         if (b + c <= a + d)swap(a, c), swap(b, d);
16         t1 = (a + c) % p;
17         add = (m - t1 + p) % p;
18         cnt1 = (a+c + add - m) / p;
19         t2 = (b + c-1) % p;
20         sub = (t2 - m + p) % p;
21         cnt2 = (b + c-1 - sub - m) / p;
22         s += (cnt2 - cnt1 + 1)*(add + 1) + (cnt2 - cnt1 + 1)*(cnt2 - cnt1) / 2 * p;
23         t1 = (b + c) % p;
24         add = (m - t1 + p) % p;
25         cnt1 = (b + c + add - m) / p;
26         t2 = (a + d) % p;
27         sub = (t2 - m + p) % p;
28         cnt2 = (a + d - sub - m) / p;
29         s += (cnt2 - cnt1 + 1)*(b - a + 1);
30         t1 = (a + d + 1) % p;
31         add = (m - t1 + p) % p;
32         cnt1 = (a + d + 1+add-m) / p;
33         t2 = (b + d) % p;
34         sub = (t2 - m + p) % p;
35         cnt2 = (b + d - sub - m) / p;
36         s += (cnt2 - cnt1 + 1)*(sub + 1) + (cnt2 - cnt1 + 1)*(cnt2 - cnt1) / 2 * p;
37         ll ss = (b - a + 1)*(d - c + 1);
38         printf("Case #%d: ", cas++);
39         printf("%I64d/%I64d\n", s / gcd(s, ss), ss / gcd(s, ss));
40     }
41     return 0;
42 }
View Code

 

posted @ 2013-11-16 22:14  HaibaraAi  阅读(116)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3