Computer Programming Contest Preparation

ToolBox - Source for: 114/11417/c.c



/home/toolbox/public_html/solutions/114/11417/c.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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2022-10-04
   19  * Purpose: fun
   20  * Problem: 11417
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define TOP 501
   28 
   29 int ans[TOP+1];
   30 int n;
   31 
   32 int gcd(int a, int b)
   33 {
   34     /* FUNCTION gcd */
   35     /* this uses Euclid's method */
   36     int toReturn;
   37 
   38     if (0 == b)
   39         toReturn = a;
   40     else
   41         toReturn = gcd(b, (a % b));
   42     return toReturn;
   43 } /* FUNCTION gcd */
   44 
   45 void init()
   46 {
   47     /* FUNCTION init */
   48     int i;
   49     int j;
   50     int k;
   51     int tmp;
   52 
   53     ans[0] = 0;
   54     ans[1] = 0;
   55     ans[2] = 1;
   56     for (k=3; TOP>k; k++)
   57         {
   58             /* calculate all 500 of them */
   59             tmp = 0;
   60             for (i=1; k>i; i++)
   61                 {
   62                     /* for i */
   63                     for (j=i+1; k>=j; j++)
   64                         {
   65                             /* for j */
   66                             DEBUG printf("(k %d) (i %d) (j %d) (tmp %d)\n", k, i, j, tmp);
   67                             tmp = tmp + gcd(j, i);
   68                         } /* for j */
   69                 } /* for i */
   70             DEBUG printf("%d: %d\n", k, tmp);
   71             ans[k] = tmp;
   72         } /* calculate all 500 of them */
   73 } /* FUNCTION init */
   74 
   75 void dump()
   76 {
   77     /* FUNCTION dump */
   78 } /* FUNCTION dump */
   79 
   80 int getInput()
   81 {
   82     /* FUNCTION getInput */
   83     int dataReadFlag;
   84 
   85     scanf(" %d ", &n);
   86     dataReadFlag = 0 != n;
   87     return (dataReadFlag);
   88 } /* FUNCTION getInput */
   89 
   90 void process()
   91 {
   92     /* FUNCTION process */
   93     printf("%d\n", ans[n]);
   94 } /* FUNCTION process */
   95 
   96 int main()
   97 {
   98     /* main */
   99     int moreToDo;
  100 
  101     init();
  102     moreToDo = getInput();
  103     while (moreToDo)
  104         {
  105             /* while */
  106             process();
  107             moreToDo = getInput();
  108         } /* while */
  109 
  110     return EXIT_SUCCESS;
  111 } /* main */
  112