Computer Programming Contest Preparation

ToolBox - Source for: 5/530/b.c



/home/toolbox/public_html/solutions/5/530/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 <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2015-03-24
   18  * Purpose:
   19  * Problem: 530
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int n;
   27 int k;
   28 
   29 void init()
   30 {
   31     /* FUNCTION init */
   32 } /* FUNCTION init */
   33 
   34 void dump()
   35 {
   36     /* FUNCTION dump */
   37 } /* FUNCTION dump */
   38 
   39 int getInput()
   40 {
   41     /* FUNCTION getInput */
   42     int dataReadFlag;
   43 
   44     scanf(" %d %d ", &n, &k);
   45     dataReadFlag = (n != 0);
   46     return (dataReadFlag);
   47 } /* FUNCTION getInput */
   48 
   49 void process()
   50 {
   51     /* FUNCTION process */
   52     int i;
   53     int j;
   54     unsigned long long tot;
   55     unsigned long long num;
   56     unsigned long long den;
   57 
   58     if (1 == k)
   59         {
   60             printf("%d\n", n);
   61         }
   62     else
   63         {
   64             /* work to do */
   65             num = n;
   66             den = 2;
   67             i = n - 1;   /* will go from k-1 to n */
   68             j = 2;     /* will go form 2 to k */
   69             while (1 < den)
   70                 {
   71                     /* while */
   72                     printf("i=%d  j=%d  num=%d  den=%d\n", i, j, num, den);
   73                     if (0 == (num % den))
   74                         {
   75                             /* time to cancel */
   76                             num = num / den;
   77                             if (j >= k)
   78                                 {
   79                                     j = 1;
   80                                 }
   81                             else
   82                                 {
   83                                     j++;
   84                                 }
   85                             den = j;
   86                         } /* time to cancel */
   87                     else if (1 < den)
   88                         {
   89                             /* need more numerator */
   90                             num = num * i;
   91                             if (i > k)
   92                                 {
   93                                     i--;
   94                                 }
   95                             else
   96                                 {
   97                                     i = 1;
   98                                 }
   99                         } /* need more numerator */
  100                     else
  101                         {
  102                             /* need more denominator */
  103                             den = den * j;
  104                             if (j >= k)
  105                                 {
  106                                     j = 1;
  107                                 }
  108                             else
  109                                 {
  110                                     j++;
  111                                 }
  112                         } /* need more denominator */
  113                 } /* while */
  114             printf("num=%llu  i=%d  k=%d\n", num, i, k);
  115             while (i > k)
  116                 {
  117                     /* use up rest of i */
  118                     num = num * i;
  119                     printf("num=%llu  i=%d  k=%d\n", num, i, k);
  120                     i--;
  121                 } /* use up rest of i */
  122         } /* work to do */
  123     printf("%llu\n", num);
  124 
  125 } /* FUNCTION process */
  126 
  127 int main()
  128 {
  129     /* main */
  130     int moreToDo;
  131 
  132     init();
  133     moreToDo = getInput();
  134     while (moreToDo)
  135         {
  136             /* while */
  137             process();
  138             moreToDo = getInput();
  139         } /* while */
  140 
  141     return EXIT_SUCCESS;
  142 } /* main */
  143