/home/toolbox/public_html/solutions/132/13276/b.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14 #define INT long
15
16 /* fprintf(stderr, "functionName: message", varslist); */
17
18 /*
19 * Author: Isaac Traxler
20 * Date: 2018-10-31
21 * Purpose: fun
22 * Problem: 13276
23 */
24
25 /*
26 * This template reads data a specified number of times.
27 */
28
29 int numberOfTimes;
30 INT e; /* initial energy */
31 INT p; /* damage a round does */
32 INT k; /* rounds per gun */
33 INT r; /* energy gain per swap */
34
35 /* if p*k > e -- stewart will die right off
36 * if stewart survives first gun
37 * if (p*k <= r), stewart will never be defeated
38 */
39
40 void init()
41 {
42 /* FUNCTION init */
43 scanf("%d ", &numberOfTimes);
44 } /* FUNCTION init */
45
46 void dump()
47 {
48 /* FUNCTION dump */
49 } /* FUNCTION dump */
50
51 void getInput()
52 {
53 /* FUNCTION getInput */
54 scanf(" %lld %lld %lld %lld ", &e, &p, &k, &r);
55 } /* FUNCTION getInput */
56
57 void process()
58 {
59 /* FUNCTION process */
60 INT tmp;
61 INT pk;
62 INT rndDamage;
63 INT rnds;
64
65 pk = p * k;
66
67 if (pk >= e)
68 {
69 /* stewart will die in first round */
70 tmp = (e + p - 1) / p;
71 } /* stewart will die in first round */
72 else
73 {
74 /* stewart makes it to 2nd gun at least */
75 if (pk <= r)
76 {
77 /* stewart is immortal */
78 tmp = -1;
79 } /* stewart is immortal */
80 else
81 {
82 /* figure out how many shots it will take to kill stewart */
83 /* remove damage done in first round */
84 /*
85 * (e + r) / (p * k) is how many more rounds to go
86 36 4 3 2 e p k r
87 pk = 4 * 3 = 12
88 (4) 22 - 16 = 6
89 (8) 6 + 13 - 16
90 (12) 3 + 13 - 16
91 */
92 /* INT e; initial energy */
93 /* INT p; damage a round does */
94 /* INT k; rounds per gun */
95 /* INT r; energy gain per swap */
96 /*
97 e p k r
98 100000 1 50000 49999
99 e = 100000
100 pk = 1 * 50000 = 50000
101 e = e - pk = 100000 - 50000 = 50000
102 tmp = 50000
103 rndDamage = 50000 - 49999 = 1
104 rnds = 50000 / 1 = 50000
105 e = 50000 - (50000 * 1) = 0
106 tmp = 50000 + (50000 * 50000) = 50000 + 2500000000 = 2500050000
107 */
108 DEBUG printf("(e %lld) (p %lld) (k %lld) (r %lld) (pk = %lld)\n", e, p, k, r, pk);
109 e = e - pk; /* remove first round */
110 tmp = k;
111 rndDamage = pk - r; /* round does pk damage with r regen */
112 rnds = (e / rndDamage);
113 DEBUG printf("(e %lld) (tmp %lld) (rndDamage %lld) (rnds %lld)\n", e, tmp, rndDamage, rnds);
114 e = e - (rnds * rndDamage);
115 tmp = tmp + (rnds * k);
116 DEBUG printf("(e %lld) (tmp %lld)\n", e, tmp);
117 if (0 < e)
118 {
119 /* a partial round */
120 tmp = tmp + ((e + r + p - 1) / p);
121 } /* a partial round */
122 } /* figure out how many shots it will take to kill stewart */
123 } /* stewart makes it to 2nd gun at least */
124
125 printf("%lld\n", tmp);
126 } /* FUNCTION process */
127
128 int main()
129 {
130 /* main */
131 int i;
132
133 init();
134 for (i=1; i<=numberOfTimes; i++)
135 {
136 /* while */
137 printf("Case %lld: ", i);
138 getInput();
139 process();
140 } /* while */
141
142 return EXIT_SUCCESS;
143 } /* main */
144
145