lightoj1027(期望dp)

有一个迷宫,有n个门,走每个的概率都是相同的

每个门有一个数字,如果是正数ai,那么表示走ai天就能走出迷宫,如果是负数,那么走-ai天会回到原地,然后会忘记之前的事情,继续选择门去走

所以,如果都是负数,那么是不可能走出迷宫的

设d为走出迷宫的期望天数

那么第三个样例就是 d = 1/3*3+1/3*(6+d)+1/3*(9+d)

那么可以算出d=18/1

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <string>
12 #include <math.h>
13 using namespace std;
14 #pragma warning(disable:4996)
15 #pragma comment(linker, "/STACK:1024000000,1024000000")
16 typedef __int64 LL;                   
17 const int INF = 1<<30;
18 /*
19 
20 */
21 
22 int gcd(int a, int b)
23 {
24     if (b == 0)return a;
25     return gcd(b, a%b);
26 }
27 int main()
28 {
29     int t, n, x;
30     scanf("%d", &t);
31     int cnt1, cnt2;
32     for (int k = 1; k <= t; ++k)
33     {
34         scanf("%d", &n);
35         cnt1 = cnt2 = 0;;
36         for (int i = 1; i <= n; ++i)
37         {
38             scanf("%d", &x);
39             if (x < 0)
40             {
41                 cnt1 += -x;
42                 cnt2++;
43             }
44             else
45                 cnt1 += x;
46         }
47         if (cnt2 == n)
48             printf("Case %d: inf
", k);
49         else
50         {
51             int a = cnt1;
52             int b = n - cnt2;
53             int g = gcd(a, b);
54             a /= g;
55             b /= g;
56             printf("Case %d: %d/%d
",k, a, b);
57         }
58     }
59     return 0;
60 }
View Code
原文地址:https://www.cnblogs.com/justPassBy/p/4744277.html