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. springboot 整合 mongodb实现 批量更新数据

    现需求:需要批量将1000个数据先查询在更新到mongodb(如果查询不到数据,则添加数据) 1:工具类BathUpdateOptions import org.springframework.dat ...

  2. Dream_Spark版本定制第一课

    从今天起,我们踏上了新的Spark学习旅途.我们的目标是要像Spark官方机构那样有能力去定制Spark. 一.  我们最开始将从Spark Streaming入手. 为何从Spark Streami ...

  3. perl6 HTTP::UserAgent (2)

    http://www.cnblogs.com/perl6/p/6911166.html 之前这里有个小小例子, 这里只要是总结一下. HTTP::UserAgent包含了以下模块: --------- ...

  4. kali的中文输入法-安装后无中文选项的解决办法

    我在我实体电脑上安装了kali 本想安装个中文输入法但是有时候输入发中根本没有选择输入法的这一项.所以导致很别扭,之前在虚机上安装过没有问题但是挪到实体机上就是没有用的也是fcitx,这一度让我这个小 ...

  5. linux串口驱动分析【转】

    转自:http://blog.csdn.net/hanmengaidudu/article/details/11946591 硬件资源及描述 s3c2440A 通用异步接收器和发送器(UART)提供了 ...

  6. linux动态追踪神器——Strace实例介绍【转】

    Strace是Linux下一款通用的进程动态跟踪工具,用来追踪程序执行时的系统调用和所接收的信号.其应用方法如下图(部分). 首先,简单说说它的使用参数,Strace的参数包括输出参数.过滤参数.统计 ...

  7. cefSharp获取百度搜索结果页面的源码

    using CefSharp; using CefSharp.WinForms; using System; using System.Collections.Generic; using Syste ...

  8. java虚拟机规范(se8)——java虚拟机结构(二)

    2.5 运行时数据区域 java虚拟机定义了多个用于程序执行期间的运行时数据区域.这些数据区域中一些随着java虚拟机的启动而创建,随着虚拟机的退出而销毁.其他的数据区域时和线程相关的.线程相关数据区 ...

  9. 关于x509、crt、cer、key、csr、pem、der、ssl、tls 、openssl等

    关于x509.crt.cer.key.csr.pem.der.ssl.tls .openssl等 TLS:传输层安全协议 Transport Layer Security的缩写 TLS是传输层安全协议 ...

  10. python 包详解

    包 包是一种管理 Python 模块命名空间的形式,采用"点模块名称". 比如一个模块的名称是 A.B, 那么他表示一个包 A中的子模块 B . 就好像使用模块的时候,你不用担心不 ...