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 给出 a<=x<=b c<=y<=d 求满足 (x+y) % p ==m 的概率
这题需要找规律 然后根据规律求解
每一个数出现的次数为 上升的等差数列 相同的值 下降的等差数列
以下看规律

  然后根据等差数列求和 求解

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("in.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
int _;
LL a, b, c, d, m, p;
LL solveL ( LL L, LL R ) {
LL cnt = L % p;
if ( cnt > m ) cnt = L + p - cnt + m;
else cnt = L + m - cnt ;
if ( cnt > R ) return ;
else {
int temp = cnt - ( a + c ) + ;
int num = ( R - cnt ) / p + ;
LL sum = 1LL * temp * num + 1LL * num * ( num - ) * p / ;
return sum;
}
}
LL solveM ( LL L, LL R ) {
LL cnt = L % p;
if ( cnt > m ) cnt = L + p - cnt + m;
else cnt = L + m - cnt ;
if ( cnt > R ) return ;
else {
LL temp = L - ( a + c ) + ;
LL num = ( R - cnt ) / p + ;
LL sum = 1LL * temp * num;
return sum;
}
}
LL solveR ( LL L, LL R ) {
LL cnt = L % p;
if ( cnt > m ) cnt = L + p - cnt + m;
else cnt = L + m - cnt ;
if ( cnt > R ) return ;
else {
LL temp = ( b + d ) - cnt + ;
LL num = ( R - cnt ) / p + ;
LL sum = 1LL * temp * num - 1LL * num * ( num - ) * p / ;
return sum;
}
}
int main() {
sf ( _ );
int cas = ;
while ( _-- ) {
scanf ( "%lld%lld%lld%lld%lld%lld", &a, &b, &c, &d, &p, &m );
LL L = a + c, R = b + d, l = min ( b + c, a + d ), r = max ( b + c, a + d ), ans = ;
ans += solveL ( L, l - );
ans += solveM ( l, r );
ans += solveR ( r + , R );
LL sum = ( b - a + ) * ( d - c + );
LL g = gcd ( ans, sum );
printf ( "Case #%d: %lld/%lld\n", cas++, ans / g, sum / g );
}
return ;
}

Just Random HDU - 4790 思维题(打表找规律)分段求解的更多相关文章

  1. HDU 4731 Minimum palindrome 打表找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...

  2. 数学--数论--HDU - 6124 Euler theorem (打表找规律)

    HazelFan is given two positive integers a,b, and he wants to calculate amodb. But now he forgets the ...

  3. HDU 3032 (SG打表找规律)

    题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...

  4. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  5. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  6. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  7. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  8. hdu 3032 Nim or not Nim? (sg函数打表找规律)

    题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围  1 ≤ N ...

  9. HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. Java进阶知识点: 枚举值

    Java进阶知识点1:白捡的扩展性 - 枚举值也是对象   一.背景 枚举经常被大家用来储存一组有限个数的候选常量.比如下面定义了一组常见数据库类型: public enum DatabaseType ...

  2. Kali渗透测试-SNMP

    1.snmpwalk -v指定snmpwalk版本 -c指定密码 2.snmp-check 获取系统信息,主机名,操作系统及架构 获取用户账户信息 获取网络信息 获取网络接口信息 IP信息 路由信息 ...

  3. 有关WCSF的几点整理

    本文示例代码 一.CreateNew Attribute实现属性注入 Steps: 1/ aspx创建某个服务的属性. 2/ 为其添加[CreateNew] Attribute. 3/ 页面继承自Mi ...

  4. cookie,localstorge,sessionstorge三者总结

    相同点:都是客户端存储东西的: 不同: 1大小,cookie最小;locastorge最大 2 cookie设置好会在header头里面自动带的:但是ls和ss不会:ls同个浏览下不同网页(非跨域)都 ...

  5. 深搜(DFS)与广搜(BFS)区别

    最近做了不少的搜索题,时而用到DFS时而用到BFS,这里对两种搜索方法做一个总结. 广度优先搜索算法(Breadth-First-Search,缩写为 BFS),是一种利用队列实现的搜索算法.简单来说 ...

  6. java中 i = i++和 j = i++ 的区别

    由于i++和i--的使用会导致值的改变,所以在处理后置的++和--的时候,java的编译器会重新为变量分配一块新的内存空间,用来存放原来的值, 而完成赋值运算之后,这块内存会被释放. (1)对于j = ...

  7. 项目--uml

    [团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...

  8. leetcode 整理

    1.Two Sum 构造Comparator,KSum 这一类的问题最基本的一题, 解法: 先sort,然后双指针,头尾各一个.进行加逼找值. 对于其余的KSum最终是降次到2次. 如3Sum固定一个 ...

  9. java文件操作(普通文件以及配置文件的读写操作)

    转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...

  10. Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"

    (1) 网上下载了一个android应用:死活用不了,查了以下,原来是android studio版本不对,于是把android studio的版本从2.2 升级到3.0,后来发现没法升级,只能下载, ...