Just Random HDU - 4790 思维题(打表找规律)分段求解

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 的概率这题需要找规律 然后根据规律求解 每一个数出现的次数为 上升的等差数列 相同的值 下降的等差数列 以下看规律
1 0 5 0 5 3 0
2 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 7 7 7 7 8 8 8 9 9 10
3 3 7 2 5 4 6
4 5 6 6 7 7 7 8 8 8 8 9 9 9 9 10 10 10 11 11 12
5 1 3 2 6 2 5
6 3 4 4 5 5 5 6 6 6 7 7 7 8 8 9

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-6
14 #define  fi first
15 #define  se second
16 #define  lson l,m,rt<<1
17 #define  rson m+1,r,rt<<1|1
18 #define  rtl   rt<<1
19 #define  rtr   rt<<1|1
20 #define  bug         printf("******\n")
21 #define  mem(a,b)    memset(a,b,sizeof(a))
22 #define  name2str(x) #x
23 #define  fuck(x)     cout<<#x" = "<<x<<endl
24 #define  f(a)        a*a
25 #define  sf(n)       scanf("%d", &n)
26 #define  sff(a,b)    scanf("%d %d", &a, &b)
27 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
28 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
29 #define  pf          printf
30 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
31 #define  FREE(i,a,b) for(i = a; i >= b; i--)
32 #define  FRL(i,a,b)  for(i = a; i < b; i++)
33 #define  FRLL(i,a,b) for(i = a; i > b; i--)
34 #define  FIN         freopen("in.txt","r",stdin)
35 #define  gcd(a,b)    __gcd(a,b)
36 #define  lowbit(x)   x&-x
37 using namespace std;
38 typedef long long  LL;
39 typedef unsigned long long ULL;
40 const int mod = 1e9 + 7;
41 const int maxn = 1e5 + 10;
42 const int INF = 0x3f3f3f3f;
43 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
44 int _;
45 LL  a, b, c, d, m, p;
46 LL solveL ( LL L, LL R ) {
47     LL cnt = L % p;
48     if ( cnt > m )  cnt = L + p - cnt + m;
49     else cnt = L + m - cnt ;
50     if ( cnt > R ) return 0;
51     else {
52         int temp = cnt - ( a + c ) + 1;
53         int num = ( R - cnt ) / p + 1;
54         LL sum = 1LL * temp * num + 1LL * num * ( num - 1 ) * p / 2;
55         return sum;
56     }
57 }
58 LL solveM ( LL L, LL R ) {
59     LL cnt = L % p;
60     if ( cnt > m )  cnt = L + p - cnt + m;
61     else cnt = L + m - cnt ;
62     if ( cnt > R ) return 0 ;
63     else {
64         LL temp = L - ( a + c ) + 1;
65         LL num = ( R - cnt ) / p + 1;
66         LL sum = 1LL * temp * num;
67         return sum;
68     }
69 }
70 LL solveR ( LL L, LL R ) {
71     LL cnt = L % p;
72     if ( cnt > m )  cnt = L + p - cnt + m;
73     else cnt = L + m - cnt ;
74     if ( cnt > R ) return 0 ;
75     else {
76         LL temp =  ( b + d ) - cnt  + 1;
77         LL num = ( R - cnt ) / p + 1;
78         LL sum = 1LL * temp * num - 1LL * num * ( num - 1 ) *  p / 2;
79         return sum;
80     }
81 }
82 int main() {
83     sf ( _ );
84     int cas = 1;
85     while ( _-- ) {
86         scanf ( "%lld%lld%lld%lld%lld%lld", &a, &b, &c, &d, &p, &m );
87         LL L = a + c, R = b + d, l = min ( b + c, a + d ), r = max ( b + c, a + d ), ans = 0;
88         ans += solveL ( L, l - 1 );
89         ans += solveM ( l, r );
90         ans += solveR ( r + 1, R );
91         LL sum = ( b - a + 1 ) * ( d - c + 1 );
92         LL g = gcd ( ans, sum );
93         printf ( "Case #%d: %lld/%lld\n", cas++, ans / g, sum / g );
94     }
95     return 0;
96 }

原文地址:https://www.cnblogs.com/qldabiaoge/p/9703858.html

时间: 2024-08-26 00:41:02

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

HDU 3032 (SG打表找规律)

题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围比较大,所以最好通过SG打表的结果找出规律在解. sg(4k+1)=4k+1;sg(4k+2)=4k+2;sg(4k+3)=4k+4; sg(4k)=4k-1; 1 2 4 3 5 6 8 7 Sample Input232 2 323 3 Sample OutputAliceBob SG打表找规律

HDU 4731 Minimum palindrome 打表找规律

题目链接 虽然想到了可能有规律,但是比赛的时候没有去仔细推敲. 暴力打表找出可以得到对应的长度n和对应字符集m所对应的答案 10 1 1 1 a 2 2 aa 3 3 aaa 4 4 aaaa 5 5 aaaaa 6 6 aaaaaa 7 7 aaaaaaa 8 8 aaaaaaaa 9 9 aaaaaaaaa 10 10 aaaaaaaaaa 20 2 1 1 a 2 1 ab 3 2 aab 4 2 aabb 5 3 aaaba 6 3 aaabab 7 3 aaababb 8 3 aaab

hdu 4883 思维题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 566    Accepted Submission(s): 267 Problem Description TIANKENG manages a

HDU 4588 Count The Carries 数位DP || 打表找规律

2013年南京邀请赛的铜牌题...做的很是伤心,另外有两个不太好想到的地方....a 可以等于零,另外a到b的累加和比较大,大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数,然后统一进位. 设最低位为1,次低位为2,依次类推,ans[]表示这一位上有多少个1,那么有 sum += ans[i]/2,ans[i+1] += ans[i]/2; sum即为答案. 好了,现在问题转化成怎么求ans[]了. 打表查规律比较神奇,上图不说话. 打表的代码 #include <algo

hdu 2147 kiki&#39;s game(DP(SG)打表找规律)

题意: n*m的棋盘,一枚硬币右上角,每人每次可将硬币移向三个方向之一(一格单位):左边,下边,左下边. 无法移动硬币的人负. 给出n和m,问,先手胜还是后手胜. 数据范围: n, m (0<n,m<=2000) 思路: dp[i][j]=0,说明从(i,j)这个点到时左下角先手败.dp[i][j]=1则先手胜. 然后记忆搜.但是记忆搜会超时. 搜完把整张表打出来,发现规律了,,,,然后,,,代码剩几行了. 代码: ///打表观察 /* int f[2005][2005]; int go(in

HDU 4919 打表找规律 java大数 map 递归

== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.ValueOf(2)).equal( 1 ) T^T100块没了... import java.math.*; import java.util.*; import static java.lang.System.out; import java.io.*; public class Main { s

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

【NOIP 模拟赛】中值滤波 打表找规律

对于这样看起来不像什么算法也没什么知识点的题,一脸懵逼的话不是手推规律就是打表找规律......... 当然还有一些超出你能力之外的数学题...... #include <cstdio> const int N=500010; int n,ans,A[N]; inline int Max(int x,int y){ return x>y?x:y; } int main(){ scanf("%d",&n); int last,now,P=0; for(int i