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 的位置,求和就可以搞定了。

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-16 13:20:40
File Name :E:\2013ACM\专题强化训练\区域赛\2013成都\1010.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; long long gcd(long long a,long long b)
{
if(b == )return a;
return gcd(b,a%b);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long a,b,c,d,p,m;
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
long long ans = ;
if(b+c <= a+d)
{
long long t1 = (a+c)%p;
long long add = (m - t1 + p)%p;
long long cnt1 = (a+c + add-m)/p;
//cout<<t1<<" "<<add<<endl;
long long t2 = (b+c-)%p;
long long sub = (t2 - m + p)%p;
long long cnt2 = (b+c--sub-m)/p;
//cout<<t2<<" "<<sub<<endl;
ans += (cnt2 - cnt1 + )*(+add) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/ * p;
//printf("%I64d %I64d %I64d\n",cnt1,cnt2,ans);
t1 = (b+c)%p;
add = (m - t1 + p)%p;
cnt1 = (b+c+add-m)/p;
t2 = (a+d)%p;
sub = (t2 - m + p)%p;
cnt2 = (a+d-sub-m)/p;
ans += (cnt2 - cnt1 + )*(b-a+);
t1 = (a+d+)%p;
add = (m - t1 + p)%p;
cnt1 = (a+d++add-m)/p;
t2 = (b+d)%p;
sub = (t2 - m + p)%p;
cnt2 = (b+d-sub-m)/p;
ans += (cnt2 - cnt1 + )*(+sub) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/*p;
}
else
{
long long t1 = (a+c)%p;
long long add = (m - t1 + p)%p;
long long cnt1 = (a+c + add-m)/p;
long long t2 = (a+d-)%p;
long long sub = (t2 - m + p)%p;
long long cnt2 = (a+d--sub-m)/p;
ans += (cnt2 - cnt1 + )*(+add) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/ * p;
t1 = (a+d)%p;
add = (m - t1 + p)%p;
cnt1 = (a+d+add-m)/p;
t2 = (b+ c)%p;
sub = (t2 - m + p)%p;
cnt2 = (b+c-sub-m)/p;
ans += (cnt2 - cnt1 + )*(d-c+);
t1 = (b+c+)%p;
add = (m - t1 + p)%p;
cnt1 = (b+c++add-m)/p;
t2 = (b+d)%p;
sub = (t2 - m + p)%p;
cnt2 = (b+d - sub-m)/p;
ans += (cnt2 - cnt1 + )*(+sub) + (cnt2 - cnt1 + )*(cnt2 - cnt1)/*p;
}
long long tot = (b-a+)*(d-c+);
long long GCD = gcd(ans,tot);
ans /= GCD;
tot /= GCD;
printf("Case #%d: %I64d/%I64d\n",iCase,ans,tot);
}
return ;
}

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. JavaScript编写风格指南 (二)

    七:注释 // 频繁的使用注释有助于他人理解你的代码// 1.代码晦涩难懂// 2.可能被误认为是错误的代码// 3.必要但不明显的针对特定浏览器的代码// 4.对于对象,方法或者属性,生成文档是有必 ...

  2. Javascript Jquery 中的数组定义与操作

    1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多维数组,但是因为数组里面可以包含对象(数组也是一个对象),所以数组可以通过相互嵌套实现类似多维数 ...

  3. 【洛谷 P2726】 [SHOI2005]树的双中心(树的重心)

    先考虑一个\(O(N^2)\)做法. 设选的两个点为\(x,y\),则一定可以将树分成两个集合\(A,B\),使得\(A\)集合所有点都去\(x\),\(B\)集合所有点都去\(y\),而这两个集合的 ...

  4. Servlet笔记7--HttpServletRequest介绍

    通过HttpServletRequest获取表单提交的数据: 前端页面: <html> <head> <title>register</title> & ...

  5. Linux的软中断处理实现 【转】

    转自:http://blog.chinaunix.net/uid-25909619-id-3070190.html 一.概念   首先我们要知道为什么中断需要下半部 .我们可以想象一下,如果没有下半部 ...

  6. 【Android开发】之Fragment重要函数讲解

    上一节我们讲到了Fragment的生命周期(都是基于android.support.v4.app包下的Fragment),学习之后相信大家对它的生命周期有了很深入的了解了,如果还有不懂得,可以再看一下 ...

  7. python enumrate使用

    新接触了一个函数 enumrate ,很多情况下我们想获得可迭代的容器(例如dict.list.tuple等)元素的时候,想同时获得一个序号用以他用. 代码常常写成这个样子 list_a = [&qu ...

  8. vs2017 Remote Debugger远程调试目录

    默认目录:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Remote Debugger

  9. poj 1797 一条路径中的最小边 再找出最大的

    Sample Input 1 // T3 3// n m1 2 3//u v w1 3 42 3 5Sample Output Scenario #1:4 # include <iostream ...

  10. django为url写测试用例

    这个和为orm写测试用例类似. 但为了区分文件,还是建议在app目录下,用tests_orm.py,tests_url.py这类单独文件加以区分. urls.py如果如这样. from django. ...