Computer Programming Contest Preparation

ToolBox - Source for: 100/10014/a.c



/home/toolbox/public_html/solutions/100/10014/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: Isaac Traxler
   19  *    Date: 2020-02-05
   20  * Purpose: fun
   21  * Problem: 10014
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 int numberOfTimes;
   29 int n;
   30 double a0;
   31 double ax;
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     scanf("%d ", &numberOfTimes);
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42 } /* FUNCTION dump */
   43 
   44 void getInput()
   45 {
   46     /* FUNCTION getInput */
   47     scanf(" %d %lf %lf ", &n, &a0, &ax);
   48 } /* FUNCTION getInput */
   49 
   50 /*********************************************
   51  * a[i] = (a[i-1] + a[i+1]) / 2 - c[i]
   52  * 2*a[i] = a[i-1] + a[i+1] - 2*c[i]
   53  * n = 1
   54  * a[1] = (a[0] + a[2] - 2*c[1]) / 2
   55  * n = 2
   56  * a[2] = (a[1] + a[3] - 2*c[2]) / 2
   57  * a[1] = ((a[0] + (a[1] + a[3] - 2*c[2]) / 2) - 2*c[1]) / 2 (substitute for a[1] using its answer above)
   58  * 2*a[1] = (a[0] + (a[1] + a[3] - 2*c[2]) / 2) - 2*c[1]
   59  * 4*a[1] = a[0] + a[1] + a[3] - 2*c[2] - 4*c[1]
   60  * 3*a[1] = a[0] + a[3] - 2*c[2] - 4*c[2]
   61  * n = 3
   62  * a[3] = (a[2] + a[4]) / 2 - 2*c[3]
   63  * a[3] = (((a[1] + a[3] - 2*c[2]) / 2) + a[4]) / 2 - c[3]  (substitute for a[2])
   64  * 2*a[3] = ((a[1] + a[3] - 2*c[2]) / 2) + a[4] - 2*c[3]    (multiply by 2)
   65  * 4*a[3] = a[1] + a[3] - 2*c[2]) + 2*a[4] - 4*c[3]         (subtract a[3] from both sides)
   66  * 3*a[3] = a[1] + 2*a[4] - 2*c[2] - 4*c[3]
   67  * a[3] = (a[1] + 2*a[4] - 2*c[2] - 4*c[3]) / 3
   68  * a[2] = (a[1] + a[3]) /2 - c[2]
   69  * 2*a[2] = a[1] + a[3] - 2*c[2]
   70  * 2*a[2] = a[1] + ((a[1] + 2*a[4] - 2*c[2] - 4*c[3]) / 3) - 2*c[2]  (substitute for a[3])
   71  * 6*a[2] = 3*a[1] + (a[1] + 2*a[4] - 2*c[2] - 4*c[3]) - 6*c[2]      (multiply by 3)
   72  * 6*a[2] = 4*a[1] + 2*a[4] - 8*c[2] - 4*c[3]
   73  * a[2] = (4*a[1] + 2*a[4] - 8*c[2] - 4*c[3]) / 6
   74  * a[1] = (a[0] + a[2] - 2*c[1]) / 2
   75  * 2*a[1]  = a[0] + a[2] - 2*c[1]                                      (multiply by 2)
   76  * 2*a[1]  = a[0] + ((4*a[1] + 2*a[4] - 8*c[2] - 4*c[3]) / 6) - 2*c[1] (substitute for a[2])
   77  * 12*a[1] = 6*a[0] + (4*a[1] + 2*a[4] - 8*c[2] - 4*c[3]) - 12*c[1]    (multiply by 6)
   78  * 12*a[1] = 6*a[0] + 4*a[1] + 2*a[4] - 8*c[2] - 4*c[3] - 12*c[1]      (simplify)
   79  * 8*a[1] = 6*a[0] + 2*a[4] - 12*c[1] - 8*c[2] - 4*c[3]                (subtract 4*a[1] from each side)
   80  * 4*a[1] = 3*a[0] + a[4] - 6*c[1] - 4*c[2] - 2*c[3]                     (divide by 2)
   81  * a[1] = (3*a[0] + a[4] - 6*c[1] - 4*c[2] -2*c[3]) / 4
   82  *
   83  * Generalizes to
   84  * a[1] = (n*a[0] + a[n+1] - n*2*c[1] - (n-1)*2*c[2] - (n-2)*2*c[3] ... - (1)*2*c[n])/(n+1)
   85  *
   86  **********************************************/
   87 
   88 void process()
   89 {
   90     /* FUNCTION process */
   91     double tot;
   92     int i;
   93     double c;
   94 
   95     tot = n * a0 + ax;
   96     for (i=0; n>i; i++)
   97         {
   98             /* for each c */
   99             scanf(" %lf ", &c);
  100             tot = tot - (n - i) * 2 * c;
  101         } /* for each c */
  102     tot = tot / (n + 1);
  103     printf("%.2lf\n", tot);
  104 } /* FUNCTION process */
  105 
  106 int main()
  107 {
  108     /* main */
  109     int i;
  110 
  111     init();
  112     for (i=0; i<numberOfTimes; i++)
  113         {
  114             /* while */
  115             if (0 < i)
  116                 {
  117                     printf("\n");
  118                 }
  119             getInput();
  120             process();
  121         } /* while */
  122 
  123     return EXIT_SUCCESS;
  124 } /* main */
  125 
  126