Computer Programming Contest Preparation

ToolBox - Source for: 132/13276/d.c



/home/toolbox/public_html/solutions/132/13276/d.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 char digits[267];
   36 
   37 
   38 /* if p*k > e -- stewart will die right off
   39  * if stewart survives first gun
   40  * if (p*k <= r), stewart will never be defeated
   41  */
   42 
   43 void init()
   44 {
   45     /* FUNCTION init */
   46     int i;
   47 
   48     for (i=0; 256>i; i=i+10)
   49         {
   50             /* for each 10 */
   51             digits[i] = '0';
   52             digits[i+1] = '1';
   53             digits[i+2] = '2';
   54             digits[i+3] = '3';
   55             digits[i+4] = '4';
   56             digits[i+5] = '5';
   57             digits[i+6] = '6';
   58             digits[i+7] = '7';
   59             digits[i+8] = '8';
   60             digits[i+9] = '9';
   61         } /* for each 10 */
   62     scanf("%d ", &numberOfTimes);
   63 } /* FUNCTION init */
   64 
   65 void dump()
   66 {
   67     /* FUNCTION dump */
   68 } /* FUNCTION dump */
   69 
   70 void printLong(long x)
   71 {
   72     /* FUNCTION printLong */
   73     char buf[52];
   74     int i = 50;
   75     long tmp;
   76 
   77     buf[51] = 0;
   78     buf[50] = '\n';
   79     tmp = x;
   80     while (0 < tmp)
   81         {
   82             /* while */
   83             i--;
   84             buf[i] = '0' + (tmp % 10);
   85             tmp = tmp / 10;
   86         } /* while */
   87     fputs(&buf[i], stdout);
   88 } /* FUNCTION printLong */
   89 
   90 void getInput()
   91 {
   92     /* FUNCTION getInput */
   93     scanf(" %d %d %d %d ", &e, &p, &k, &r);
   94 } /* FUNCTION getInput */
   95 
   96 void process()
   97 {
   98     /* FUNCTION process */
   99     INT tmp;
  100     INT pk;
  101     INT rndDamage;
  102     INT rnds;
  103 
  104     pk = p * k;
  105 
  106     if (pk >= e)
  107         {
  108             /* stewart will die in first round */
  109             tmp = (e + p - 1) / p;
  110             printLong(tmp);
  111         } /* stewart will die in first round */
  112     else
  113         {
  114             /* stewart makes it to 2nd gun at least */
  115             if (pk <= r)
  116                 {
  117                     /* stewart is immortal */
  118                     fputs("-1\n", stdout);
  119                 } /* stewart is immortal */
  120             else
  121                 {
  122                     /* figure out how many shots it will take to kill stewart */
  123                     /* remove damage done in first round */
  124                     /*
  125                      *  (e + r) / (p * k) is how many more rounds to go
  126                     36 4 3 2 e p k r
  127                     pk = 4 * 3 = 12
  128                     (4) 22 - 16 = 6
  129                     (8) 6 + 13 - 16
  130                     (12) 3 + 13 - 16
  131                      */
  132                     /* INT e;  initial energy */
  133                     /* INT p;  damage a round does */
  134                     /* INT k;  rounds per gun */
  135                     /* INT r;  energy gain per swap */
  136                     /*
  137                     e      p k     r
  138                     100000 1 50000 49999
  139                     e = 100000
  140                     pk = 1 * 50000 = 50000
  141                     e = e - pk = 100000 - 50000 = 50000
  142                     tmp = 50000
  143                     rndDamage = 50000 - 49999 = 1
  144                     rnds = 50000 / 1 = 50000
  145                     e = 50000 - (50000 * 1) = 0
  146                     tmp = 50000 + (50000 * 50000) = 50000 + 2500000000 = 2500050000
  147                     */
  148                     e = e - pk; /* remove first round */
  149                     rndDamage = pk - r; /* round does pk damage with r regen */
  150                     rnds = (e / rndDamage);
  151                     e = e - (rnds * rndDamage);
  152                     tmp = ((1 + rnds) * k);
  153                     if (0 < e)
  154                         {
  155                             /* a partial round */
  156                             tmp = tmp + ((e + r + p - 1) / p);
  157                         } /* a partial round */
  158                     printLong(tmp);
  159                 } /* figure out how many shots it will take to kill stewart */
  160         } /* stewart makes it to 2nd gun at least */
  161 } /* FUNCTION process */
  162 
  163 int main()
  164 {
  165     /* main */
  166     int i;
  167 
  168     init();
  169     for (i=1; i<=numberOfTimes; i++)
  170         {
  171             /* while */
  172             printf("Case %d: ", i);
  173             getInput();
  174             process();
  175         } /* while */
  176 
  177     return EXIT_SUCCESS;
  178 } /* main */
  179 
  180