lightoj1027_数学求期望

题目链接:http://lightoj.com/volume_showproblem.php?problem=1027

题意 : 在n个门前选择一扇门出去, 然后如果第i扇门的 Xi值是正的话,你会花费Xi时间后出去 , 如果Xi是负数的话你会花费-Xi时间后回到老地方,并且忘记了刚才的选择, 选择一扇门的概率是等概的。求出去的期望。

思路 :定义一次选择选择到Xi是整数的概率为P1,选择到负数的概率是P2,然后选择了正数后平均在T1时间后出去, 选择了负数后平均在T2时间后回到原地。接着设出去的期望是Y,那么可以写出一个式子 :Y = P1 * T1 + P2 * (T2 + Y), 这样的话问题就得到了解决, 最后整理下式子就是 : Y = 正数个数的倒数 * ∑abs(Xi) ;

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15
16 int main()
17 {
18     int t, n, a;
19     scanf("%d", &t);
20     for(int ca = 1; ca <= t; ca++)
21     {
22         scanf("%d", &n);
23         int x = 0, sum  = 0;
24         for(int i = 0; i < n; i++)
25         {
26             scanf("%d", &a);
27             if(a > 0)
28                 sum += a;
29             else
30             {
31                 sum -= a;
32                 x++;
33             }
34         }
35         x = n - x;
36         if(x == 0)
37             printf("Case %d: inf\n", ca);
38         else
39         {
40             int y = __gcd(sum, x);
41             printf("Case %d: %d/%d\n", ca, sum/y, x/y);
42         }
43     }
44     return 0;
45 }
时间: 2024-12-25 02:36:11

lightoj1027_数学求期望的相关文章

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

hdu 4405 Aeroplane chess(概率DP 求期望__附求期望讲解方法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal p

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

杭电ACM 二 数学求模

Font Size: ← → Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Ea

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU 4035:Maze 概率DP求期望(有环)

Maze 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4035 题意: 有N(2 ≤ N ≤ 10000)个房间和一堆双向边(不存在环),每个房间有ki和ei两个值,分别代表回到房间1和游戏结束的概率,求游戏结束时通过的边数的期望 题解: 一道很好很经典的求期望的题 设E[i]为以i为起点,直到游戏结束所通过边数的期望,则E[1]即所求答案 设fa代表父亲节点(由于不存在环,则图为一棵树,设1为根节点),∑ch代表所有孩子节点,size代表与这

HDU 4405:Aeroplane chess 概率DP求期望

One Person Game 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 有个人在玩飞行棋,规则是:掷一个骰子,就能从当前点x飞到(x+点数)处,在棋盘上有一些飞行通道,如果点x和点y间存在飞行通道,那么当你走到点x,就会飞到点y处,起点在0,求从起点飞到终点n所需要投掷骰子次数的期望. 题解: 一道简单的求期望的题,不会求期望的可以看下这里 当点i是飞行通道的起点的时候,由于不需要投掷骰子,就能飞到飞行通道的终点处,所以此