Computer Programming Contest Preparation

ToolBox - Source for: 127/12703/a.c



/home/toolbox/public_html/solutions/127/12703/a.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 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author:
   19  *    Date:
   20  * Purpose:
   21  * Problem:
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_PRIMES 168
   29 
   30 int numberOfTimes;
   31 int primes[MAX_PRIMES] =
   32 {
   33     2,     3,     5,     7,    11,    13,    17,    19,    23,    29,
   34     31,    37,    41,    43,    47,    53,    59,    61,    67,    71,
   35     73,    79,    83,    89,    97,   101,   103,   107,   109,   113,
   36     127,   131,   137,   139,   149,   151,   157,   163,   167,   173,
   37     179,   181,   191,   193,   197,   199,   211,   223,   227,   229,
   38     233,   239,   241,   251,   257,   263,   269,   271,   277,   281,
   39     283,   293,   307,   311,   313,   317,   331,   337,   347,   349,
   40     353,   359,   367,   373,   379,   383,   389,   397,   401,   409,
   41     419,   421,   431,   433,   439,   443,   449,   457,   461,   463,
   42     467,   479,   487,   491,   499,   503,   509,   521,   523,   541,
   43     547,   557,   563,   569,   571,   577,   587,   593,   599,   601,
   44     607,   613,   617,   619,   631,   641,   643,   647,   653,   659,
   45     661,   673,   677,   683,   691,   701,   709,   719,   727,   733,
   46     739,   743,   751,   757,   761,   769,   773,   787,   797,   809,
   47     811,   821,   823,   827,   829,   839,   853,   857,   859,   863,
   48     877,   881,   883,   887,   907,   911,   919,   929,   937,   941,
   49     947,   953,   967,   971,   977,   983,   991,   997
   50 };
   51 int A[MAX_PRIMES];
   52 int B[MAX_PRIMES];
   53 int N;
   54 
   55 void init()
   56 {
   57     /* FUNCTION init */
   58     scanf("%d ", &numberOfTimes);
   59 } /* FUNCTION init */
   60 
   61 void dump()
   62 {
   63     /* FUNCTION dump */
   64 } /* FUNCTION dump */
   65 
   66 void factor(int num, int f[])
   67 {
   68     /* FUNCTION factor */
   69     int i;
   70     int tmp;
   71 
   72     tmp = num;
   73     for (i=0; i<MAX_PRIMES; i++)
   74         {
   75             f[i] = 0;
   76         }
   77     DEBUG printf("factor: array cleared   num = %d\n", tmp);
   78     for (i=0; 1<tmp; i++)
   79         {
   80             /* factor num */
   81             DEBUG printf("factor: testing %d\n", primes[i]);
   82             while (0 == (tmp % primes[i]))
   83                 {
   84                     /* divide by this prime as many times as possible */
   85                     f[i] = f[i] + 1;
   86                     tmp = tmp / primes[i];
   87                 } /* divide by this prime as many times as possible */
   88             DEBUG printf("factor: %d divides %d %d times (remainder: %d)\n", primes[i], num, f[i], tmp);
   89         } /* factor num */
   90 } /* FUNCTION factor */
   91 
   92 void iterate(int x[], int y[])
   93 {
   94     /* FUNCTION iterate */
   95     int i;
   96 
   97     for (i=0; i<MAX_PRIMES; i++)
   98         {
   99             /* for each possibel prime number */
  100             x[i] = x[i] + y[i];
  101         } /* for each possibel prime number */
  102 } /* FUNCTION iterate */
  103 
  104 void getInput()
  105 {
  106     /* FUNCTION getInput */
  107     int tmp;
  108 
  109     scanf(" %d ", &N);
  110     DEBUG printf("N = %d\n", N);
  111     scanf(" %d ", &tmp);
  112     DEBUG printf("a = %d\n", tmp);
  113     factor(tmp, A);
  114     scanf(" %d ", &tmp);
  115     DEBUG printf("b = %d\n", tmp);
  116     factor(tmp, B);
  117 } /* FUNCTION getInput */
  118 
  119 void process()
  120 {
  121     /* FUNCTION process */
  122     int i;
  123     int *p;  /* previous */
  124     int *c;  /* current */
  125     int *t;  /* temporary */
  126 
  127     p = A;
  128     c = B;
  129     for (i=1; i<N; i++)
  130         {
  131             /* make each iteration */
  132             iterate(p, c);
  133             /* swap previous and curent */
  134             t = p;
  135             p = c;
  136             c = t;
  137         } /* make each iteration */
  138     for (i=0; i<MAX_PRIMES; i++)
  139         {
  140             /* for each prime factor */
  141             if (0 < c[i])
  142                 {
  143                     /* need to print it */
  144                     printf("%d %d\n", primes[i], c[i]);
  145                 } /* need to print it */
  146         } /* for each prime factor */
  147 } /* FUNCTION process */
  148 
  149 int main()
  150 {
  151     /* main */
  152     int i;
  153 
  154     init();
  155     for (i=0; i<numberOfTimes; i++)
  156         {
  157             /* while */
  158             getInput();
  159             process();
  160             printf("\n");
  161         } /* while */
  162 
  163     return EXIT_SUCCESS;
  164 } /* main */
  165 
  166