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. ruby http爬虫中的 :body 用法问题

    require 'http' url = 'http://localhost/b.php' data = 'whoami=whoami' html = HTTP.via('127.0.0.1',808 ...

  2. Linux内核启动流程分析(一)【转】

    转自:http://blog.chinaunix.net/uid-25909619-id-3380535.html 很久以前分析的,一直在电脑的一个角落,今天发现贴出来和大家分享下.由于是word直接 ...

  3. 在SharePoint 2013里配置Excel Services

    配置步骤,请参看下面两篇文章 http://www.cnblogs.com/jianyus/p/3326304.html https://technet.microsoft.com/zh-cn/lib ...

  4. ASP.NET MVC环境下实现一个网站多个网站模板的方法

    asp.net mvc下实现多个网站模板的方法,让ASP.NET一个网站有多套网站模板,不用的场景下使用不用的mvc 模版.  比如有默认,红,蓝,绿几种网站模板,客户可以根据自己喜好选择自己喜欢的网 ...

  5. cat集成项目所遇到的一些坑

    第一个问题:(jar包依赖冲突) 启动报错,直接贴log zhengxin-third-shanghai-cis [2017-08-21 14:17:49] 56231 WARN [main] - A ...

  6. jdk678910新特性地址

    jdk678910新特性地址 https://blog.csdn.net/f641385712/article/details/81289401 每篇一句:每个人受到的尊重从来都不是应得的,而是赢得的 ...

  7. navicate连接Linux下mysql慢,卡,以及mysql相关查询,授权

    方法,网上的办法是在my.ini的“[mysqld]”下面加入一行“skip-name-resolve”,就像这样: 然后保存并重启mysql服务即可. service mysqld restart ...

  8. 基于nopCommerce的开发框架(附源码)

    .NET的开发人员应该都知道这个大名鼎鼎的高质量b2c开源项目-nopCommerce,基于EntityFramework和MVC开发,拥有透明且结构良好的解决方案,同时结合了开源和商业软件的最佳特性 ...

  9. java轻松实现无锁队列

    1.什么是无锁(Lock-Free)编程 当谈及 Lock-Free 编程时,我们常将其概念与 Mutex(互斥) 或 Lock(锁) 联系在一起,描述要在编程中尽量少使用这些锁结构,降低线程间互相阻 ...

  10. 003.NFS配置实例

    一 NFS常见服务管理 1.1 启动NFS [root@imxhy ~]# systemctl start nfs #CentOS7.x系列启动 [root@imxhy ~]# service nfs ...