Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 1,988 Bytes
7acee6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Hacker Cup 2017
// Qualification Round
// Fighting The Zombie
// Wesley May

import java.util.*;

public class FightingTheZombie 
{
	static int H;

	public static void main(String args[])
	{
		double EPS = 0.000001;
		Scanner scan = new Scanner(System.in);
		int T = scan.nextInt();

		for(int ca=1;ca <= T;ca++)
		{
			H = scan.nextInt();
			int S = scan.nextInt();
			Spell[] a = new Spell[S];

			for(int i=0;i < S;i++)
			{
				String str = scan.next();
				int X, Y, Z;

				int didx = str.indexOf("d");
				int plusidx = str.indexOf("+");
				int minusidx = str.indexOf("-");

				X = Integer.parseInt(str.substring(0, didx));

				if(plusidx >= 0)
				{
					Y = Integer.parseInt(str.substring(didx+1, plusidx));
					Z = Integer.parseInt(str.substring(plusidx));
				} 
				else if(minusidx >= 0)
				{
					Y = Integer.parseInt(str.substring(didx+1,minusidx));
					Z = Integer.parseInt(str.substring(minusidx));
				}
				else
				{
					Y = Integer.parseInt(str.substring(didx+1));
					Z = 0;
				}

				a[i] = new Spell(X, Y, Z);
			}

			Arrays.sort(a);

			System.out.printf("Case #%d: %.6f\n", ca, a[0].p);
		}
	}

	static class Spell implements Comparable<Spell>
	{
		int x, y, z;
		public double p;
		double[][] dp;

		public Spell(int ix, int iy, int iz)
		{
			x = ix;
			y = iy;
			z = iz;
			int newH = H - z;

			if(newH <= 0)
			{
				p = 1;
				return;
			}

			dp = new double[newH+1][x+1];
			for(int i=0;i < dp.length;i++)
				Arrays.fill(dp[i], -1);

			p = f(newH, x);
		}

		private double f(int h, int d)
		{
			if(h <= 0) return 1;
			if(d == 0) return 0;
			if(dp[h][d] >= 0) return dp[h][d];

			double ans = 0;
			for(int i=1;i <= y;i++)
			{
				ans += (1.0 / y) * f(h-i, d-1);
			}

			return dp[h][d] = ans;
		}

		public int compareTo(Spell that) {
			if(this.p > that.p) return -1;
			if(this.p < that.p) return 1;
			return 0;
		}
	}
}