Computer Programming Contest Preparation

ToolBox - Source for: 3/374/c.c



/home/toolbox/public_html/solutions/3/374/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 #define MAX_LINE 257
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2021-11-11
   21  * Purpose: fun
   22  * Problem: 374 - Big Mod
   23  */
   24 
   25 /*
   26  * This template reads lines of data at a time until end of file.
   27  */
   28 
   29 unsigned int b;
   30 unsigned int p;
   31 unsigned int m;
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int getInput()
   44 {
   45     /* FUNCTION getInput */
   46     int dataReadFlag;
   47 
   48     dataReadFlag = (1 == scanf(" %u ", &b));
   49     if (dataReadFlag)
   50         {
   51             /* more to read ? */
   52             scanf(" %u %u ", &p, &m);
   53         } /* more to read ? */
   54     return (dataReadFlag);
   55 } /* FUNCTION getInput */
   56 
   57 void process()
   58 {
   59     /* FUNCTION process */
   60     unsigned int i;
   61     unsigned int square;
   62     unsigned int goal;
   63 
   64     if (0 == p)
   65         {
   66             /* x^0 == 1 */
   67             goal = 0;
   68         } /* x^0 == 1 */
   69     else
   70         {
   71             /* compute it */
   72             b = b % m;
   73             square = b;
   74             goal = 1;
   75             i = p;
   76             while (0 < i)
   77                 {
   78                     /* while */
   79                     if (1 == (i & 1))
   80                         {
   81                             /* odd power */
   82                             goal = (goal * square) % m;
   83                         } /* odd power */
   84                     square = (square * square) % m;
   85                     i = i / 2;
   86                 } /* while */
   87         } /* compute it */
   88     printf("%u\n", goal);
   89 } /* FUNCTION process */
   90 
   91 int main()
   92 {
   93     /* main */
   94     int moreToDo;
   95 
   96     init();
   97     moreToDo = getInput();
   98     while (moreToDo)
   99         {
  100             /* while */
  101             process();
  102             moreToDo = getInput();
  103         } /* while */
  104 
  105     return EXIT_SUCCESS;
  106 } /* main */
  107