Computer Programming Contest Preparation

ToolBox - Source for: 108/10825/a.c



/home/toolbox/public_html/solutions/108/10825/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 <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 (TRUE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2012-08-07:
   18  * Purpose: fun
   19  * Problem: 10825 - Anagram and Multiplication
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int digits[400];
   27 int M;
   28 int N;
   29 int start[7] = {0, 0, 0, 100, 1000, 10000, 100000};
   30 
   31 unsigned int addDigits(int num)
   32 {
   33     /* FUNCTION addDigits */
   34     int i;
   35     int j;
   36     unsigned int sum = 0;
   37 
   38     for (i=0; N>i; i++)
   39         {
   40             /* for */
   41             digits[i] = 0;
   42         } /* for */
   43 
   44     for (i=num; 0<i; i = i / N)
   45         {
   46             /* for */
   47             digits[i % N]++;
   48         } /* for */
   49 
   50     for (i=(N-1); 0<=i; i--)
   51         {
   52             /* for i */
   53             for (j=digits[i]; 0<j; j--)
   54                 {
   55                     /* for j */
   56                     sum = sum * N + i;
   57                 } /* for j */
   58         } /* for i */
   59     return sum;
   60 } /* FUNCTION addDigits */
   61 
   62 
   63 
   64 void init()
   65 {
   66     /* FUNCTION init */
   67     int i;
   68     unsigned int t1;
   69     unsigned int t2;
   70     unsigned int t3;
   71     unsigned int t4;
   72     unsigned int t5;
   73     unsigned int t6;
   74 
   75     t6=4096 * 100000;
   76     printf("%u\n", t6);
   77 
   78     for (i=0; 10>i; i++)
   79         {
   80             digits[i] = 0;
   81         }
   82 
   83     for (i=100000; i<166667; i++) ;
   84     for (i=10000; i<20000; i++) ;
   85     for (i=1000; i<2500; i++)
   86         {
   87             /* for */
   88             printf("%d %d %d\n", i, i*2, i*3);
   89             t1 = addDigits(i);
   90             t2 = addDigits(i*2);
   91             t3 = addDigits(i*3);
   92             t4 = addDigits(i*4);
   93             /*
   94             t5 = addDigits(i*5);
   95             t6 = addDigits(i*6);
   96             */
   97             DEBUG printf("%u %u %u %u %u %u\n", t1, t2, t3, t4, t5, t6);
   98             if (!((t1 != t2)
   99                     || (t3 != t4)
  100                     /*
  101                         || (t5 != t6)
  102                         || (t1 != t5)
  103                     */
  104                     || (t1 != t3)
  105                  ))
  106                 {
  107                     /* solution found */
  108                     printf("solution: %d\n", i);
  109                 } /* solution found */
  110 
  111         } /* for */
  112 } /* FUNCTION init */
  113 
  114 void dump()
  115 {
  116     /* FUNCTION dump */
  117 } /* FUNCTION dump */
  118 
  119 int getInput()
  120 {
  121     /* FUNCTION getInput */
  122     int dataReadFlag;
  123 
  124     scanf(" %d %d ", &M, &N);
  125 
  126     dataReadFlag = (0 != M);
  127     return (dataReadFlag);
  128 } /* FUNCTION getInput */
  129 
  130 void process()
  131 {
  132     /* FUNCTION process */
  133     int num;
  134     int upperLimit;
  135     unsigned int hash1;
  136     unsigned int hash2;
  137     int hashMatch;
  138     int solutionNotFound = TRUE;
  139     int i;
  140 
  141     num = start[M] - 1;
  142     upperLimit = start[M] * N / M + 1;
  143     while ((num < upperLimit) && (solutionNotFound))
  144         {
  145             /* while */
  146             num++;
  147             hash1 = addDigits(num);
  148             hashMatch = TRUE;
  149             for (i=2; (i<=M) && (hashMatch); i++)
  150                 {
  151                     /* for - check each multiple */
  152                     hash2 = addDigits(i * num);
  153                     DEBUG printf("number: %d => %u    %d*%d => %u  \n", num, hash1, i, num *i, hash2);
  154                     hashMatch = (hash1 == hash2);
  155                 } /* for - check each multiple */
  156             solutionNotFound = ! hashMatch;
  157         } /* while */
  158     if (solutionNotFound)
  159         {
  160             /* no solution */
  161             printf("Not found.\n");
  162         } /* no solution */
  163     else
  164         {
  165             /* solution found */
  166             printf("Solution: %d\n", num);
  167         } /* solution found */
  168 } /* FUNCTION process */
  169 
  170 int main ()
  171 {
  172     /* main */
  173     int moreToDo;
  174 
  175     /* init(); */
  176     moreToDo = getInput();
  177     while (moreToDo)
  178         {
  179             /* while */
  180             process();
  181             moreToDo = getInput();
  182         } /* while */
  183 
  184     return EXIT_SUCCESS;
  185 } /* main */
  186