Cod sursa(job #1465814)

Utilizator om6gaLungu Adrian om6ga Data 28 iulie 2015 00:38:26
Problema Pascal Scor 60
Compilator c Status done
Runda Arhiva de probleme Marime 2.6 kb
#include <stdio.h>

int R, D, total[6];

int nr(int r, int d)
{
    int count = 0, i, aux;
    
    for (i = 1; i <= r/2; i++)
    {
        total[2] = total[3] = total[5] = 0;
        if (d == 2 || d == 4 || d == 6)
        {
            aux = r;
            while(aux)
            {
                total[2] += aux/2;
                aux /= 2;
            }
            aux = i;
            while(aux)
            {
                total[2] -= aux/2;
                aux /= 2;
            }
            aux = r-i;
            while(aux)
            {
                total[2] -= aux/2;
                aux /= 2;
            }
        }
        if (d == 3 || d == 6)
        {
            aux = r;
            while(aux)
            {
                total[3] += aux/3;
                aux /= 3;
            }
            aux = i;
            while(aux)
            {
                total[3] -= aux/3;
                aux /= 3;
            }
            aux = r-i;
            while(aux)
            {
                total[3] -= aux/3;
                aux /= 3;
            }
        }
        if (d == 5)
        {
            aux = r;
            while(aux)
            {
                total[5] += aux/5;
                aux /= 5;
            }
            aux = i;
            while(aux)
            {
                total[5] -= aux/5;
                aux /= 5;
            }
            aux = r-i;
            while(aux)
            {
                total[5] -= aux/5;
                aux /= 5;
            }
        }
        if (d == 2)
            count += (total[2] > 0 ? 1 : 0);
        else if (d == 3)
            count += (total[3] > 0 ? 1 : 0);
        else if (d == 4)
            count += (total[2] >= 2 ? 1 : 0);
        else if (d == 5)
            count += (total[5] > 0 ? 1 : 0);
        else if (d == 6)
            count += (total[2] && total[3] ? 1 : 0);
    }
    count *= 2;
    if ((r/2)*2 == r)
    {
        if (d == 2)
            count -= (total[2] > 0 ? 1 : 0);
        else if (d == 3)
            count -= (total[3] > 0 ? 1 : 0);
        else if (d == 4)
            count -= (total[2] >= 2 ? 1 : 0);
        else if (d == 5)
            count -= (total[5] > 0 ? 1 : 0);
        else if (d == 6)
            count -= (total[2] && total[3] ? 1 : 0);
    }  
    
    return count;   
}

int main()
{
    FILE *in, *out;
    in = fopen("pascal.in", "r");
    out = fopen("pascal.out", "w");
    fscanf(in, "%d %d", &R, &D);

    fprintf(out, "%d\n", nr(R, D));
    fclose(in);
    fclose(out);
    return 0;   
}