Cod sursa(job #1466068)

Utilizator om6gaLungu Adrian om6ga Data 28 iulie 2015 16:29:10
Problema Pascal Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <stdio.h>
 
int r, d, total[6];
int count = 0, i, aux;


int pwr(int n, int div)
{
    int c = 0;
    aux = n;
    while(aux)
    {
        if (div == 2)
            aux >>= 1;
        else
            aux /= div;
        c += aux;
    }
    return c;
}

 
int main()
{
    FILE *in, *out;
    in = fopen("pascal.in", "r");
    out = fopen("pascal.out", "w");
    fscanf(in, "%d %d", &r, &d);
     
    for (i = 1; i <= r/2; i++)
    {
        total[2] = total[3] = total[5] = 0;
        if (d == 2 || d == 4 || d == 6)
        {
            total[2] += pwr(r, 2);
            total[2] -= pwr(i, 2);
            total[2] -= pwr(r-i, 2);
        }
        if (d == 3 || d == 6)
        {
            total[3] += pwr(r, 3);
            total[3] -= pwr(i, 3);
            total[3] -= pwr(r-i, 3);
        }
        if (d == 5)
        {
            total[5] += pwr(r, 5);
            total[5] -= pwr(i, 5);
            total[5] -= pwr(r-i, 5);
        }
         
        if (d == 2)
            count += (total[2] ? 1 : 0);
        else if (d == 3)
            count += (total[3] ? 1 : 0);
        else if (d == 4)
            count += (total[2] >= 2 ? 1 : 0);
        else if (d == 5)
            count += (total[5] ? 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] ? 1 : 0);
        else if (d == 3)
            count -= (total[3] ? 1 : 0);
        else if (d == 4)
            count -= (total[2] >= 2 ? 1 : 0);
        else if (d == 5)
            count -= (total[5] ? 1 : 0);
        else if (d == 6)
            count -= (total[2] && total[3] ? 1 : 0);
    } 
 
    fprintf(out, "%d\n", count);
    fclose(in);
    fclose(out);
    return 0;  
}