Computer Programming Contest Preparation

ToolBox - Source for: 103/10365/a.c



/home/toolbox/public_html/solutions/103/10365/a.c
    1 #include <stdio.h>
    2 #include <strings.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 
    8 #define TRUE  (1 == 1)
    9 #define FALSE (1 != 1)
   10 
   11 #define DEBUG if (FALSE)
   12 
   13 #define MAXPRIMES 1230
   14 #define UPPERLIMIT 10000
   15 #define MAXFACTORS 15
   16 
   17 /* fprintf(stderr, "functionName: message", varslist); */
   18 
   19 /*
   20  *  Author:
   21  *    Date:
   22  * Purpose:
   23  * Problem: 10365
   24  */
   25 
   26 /*
   27  * This template reads data a specified number of times.
   28  */
   29 
   30 int numberOfTimes;
   31 int num;
   32 int primeCnt;
   33 int a[UPPERLIMIT];
   34 int p[MAXPRIMES];
   35 int factors[MAXFACTORS];
   36 int tarray[MAXFACTORS];
   37 int numFactors;
   38 
   39 
   40 int init()
   41 {
   42     /* FUNCTION init */
   43     int i;
   44     int j;
   45 
   46     scanf("%d` ", &numberOfTimes);
   47     /* load p via Sieve of Erasthones */
   48     primeCnt = 0;
   49     for (i=0; i< UPPERLIMIT; i++) a[i] = i;
   50     for (i=2; i<UPPERLIMIT; i++)
   51         {
   52             /* for */
   53             if (0 != a[i])
   54                 {
   55                     /* found a prime */
   56                     p[primeCnt] = i;
   57                     primeCnt++;
   58                     for (j=i*2; j < UPPERLIMIT; j=j+i) a[j] = 0;
   59                 } /* found a prime */
   60         } /* for */
   61 
   62 } /* FUNCTION init */
   63 
   64 void dump()
   65 {
   66     /* FUNCTION dump */
   67 } /* FUNCTION dump */
   68 
   69 void getInput()
   70 {
   71     /* FUNCTION getInput */
   72     scanf("%d ", &num);
   73 } /* FUNCTION getInput */
   74 
   75 void factor(int num)
   76 {
   77     /* FUNCTION factor */
   78     int i;
   79     int tmp;
   80 
   81     numFactors = 0;
   82     tmp=num;
   83     for (i=0; ((i<primeCnt) && (tmp > 1));)
   84         {
   85             /* divide number */
   86             if (0 == (tmp % p[i]))
   87                 {
   88                     /* divides evenly */
   89                     factors[numFactors] = p[i];
   90                     numFactors++;
   91                     tmp = tmp % p[i];
   92                 } /* divides evenly */
   93             else
   94                 {
   95                     /* not a divisor -- go to next */
   96                     i++;
   97                 } /* not a divisor -- go to next */
   98         } /* divide number */
   99 } /* FUNCTION factor */
  100 
  101 int try(nt ary, int root)
  102     {
  103         /* FUNCTION try */
  104     } /* FUNCTION try */
  105 
  106 int findFactor(int root)
  107 {
  108     /* FUNCTION findFactor */
  109     int toReturn;
  110     int res;
  111     int i;
  112     int k;
  113 
  114     if (root <= factors[numFactors-1])
  115         {
  116             /* one prime bigger than root */
  117             numFactors--;
  118             toReturn = factors[numFactors];
  119         } /* one prime bigger than root */
  120     else
  121         {
  122             /* no single factor bigger */
  123             k=0;
  124             for (i=0; i<numFactors; i++)
  125                 if (k != i) tarray[i] = factors[i];
  126             res=try(tarray, ((root+k-1)/k));
  127         } /* no single factor bigger */
  128     return toReturn;
  129 } /* FUNCTION findFactor */
  130 
  131 void process()
  132 {
  133     /* FUNCTION process */
  134     int cubeRoot;
  135     factor(num);
  136     cubeRoot = ceil(cbrt(num*1.0));
  137     factor1 = findFactor(cubeRoot);
  138 } /* FUNCTION process */
  139 
  140 int main ()
  141 {
  142     /* main */
  143     int i;
  144 
  145     init();
  146     DEBUG printf("number of primes: %d\n", primeCnt);
  147     DEBUG for (i=0; i<primeCnt; i++) printf("\t%d:%d\n", i, p[i]);
  148     for (i=0; i<numberOfTimes; i++)
  149         {
  150             /* while */
  151             getInput();
  152             process();
  153         } /* while */
  154 
  155     return EXIT_SUCCESS;
  156 } /* main */
  157