Computer Programming Contest Preparation

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



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