Cod sursa(job #1465792)

Utilizator om6gaLungu Adrian om6ga Data 27 iulie 2015 23:39:26
Problema Pascal Scor 10
Compilator c Status done
Runda Arhiva de probleme Marime 2.58 kb
#include <stdio.h>

#define RMAX 5000005
#define TWO 0
#define THREE 1
#define FIVE 2

int R, D;
int pwr[RMAX][3];


void precalc(int r)
{
    int i, aux;
    
    for (i = 2; i <= r; i++)
    {
        aux = i;
        while (aux)
        {
              pwr[i][TWO] += aux/2;
              aux /= 2;
        }
        aux = i;
        while (aux)
        {
              pwr[i][THREE] += aux/3;
              aux /= 3;
        }
        aux = i;
        while (aux)
        {
              pwr[i][FIVE] += aux/5;
              aux /= 5;
        }
    }
}

int nr(int r, int d)
{
    int count = 0, i, aux[3];
    
    for (i = 1; i <= r/2; i++)
    {
        aux[TWO] = pwr[r][TWO] - pwr[i][TWO] - pwr[r-i][TWO];
        aux[THREE] = pwr[r][THREE] - pwr[i][THREE] - pwr[r-i][THREE];
        aux[FIVE] = pwr[r][FIVE] - pwr[i][FIVE] - pwr[r-i][FIVE];
        switch(d)
        {
                 case 2:
                      count += (aux[TWO] ? 1 : 0);
                      break;
                 case 3:
                      count += (aux[THREE] ? 1 : 0);
                      break;
                 case 4:
                      count += (aux[TWO] >= 2 ? 1 : 0);
                      break;
                 case 5:
                      count += (aux[FIVE] ? 1 : 0);
                      break;
                 case 6:
                      count += ((aux[TWO] > 0 && aux[THREE]) > 0 ? 1 : 0);
                      break;
        }
    }
    count *= 2;
    if ((r/2)*2 == r)
    {
        aux[TWO] = pwr[r][TWO] - 2*pwr[r/2][TWO];
        aux[THREE] = pwr[r][THREE] - 2*pwr[r/2][THREE];
        aux[FIVE] = pwr[r][FIVE] - 2*pwr[r/2][FIVE];
        switch(d)
        {
                 case 2:
                      count -= (aux[TWO] ? 1 : 0);
                      break;
                 case 3:
                      count -= (aux[THREE] ? 1 : 0);
                      break;
                 case 4:
                      count -= (aux[TWO] >= 2 ? 1 : 0);
                      break;
                 case 5:
                      count -= (aux[FIVE] ? 1 : 0);
                      break;
                 case 6:
                      count -= ((aux[TWO] > 0 && aux[THREE] > 0) ? 1 : 0);
                      break;
        }    
    }  
    
    return count;   
}

int main()
{
    FILE *in, *out;
    in = fopen("pascal.in", "r");
    out = fopen("pascal.out", "w");
    fscanf(in, "%d %d", &R, &D);
    
    precalc(R);
    //fprintf(out, "%d\n", nr(R, D));
    fclose(in);
    fclose(out);
    return 0;   
}