Cod sursa(job #1466099)

Utilizator om6gaLungu Adrian om6ga Data 28 iulie 2015 17:24:20
Problema Pascal Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <stdio.h>
 
int r, d, total[6];
int count = 0, i, aux, mult;


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 gain(int a, int b, int div)
{
    int c = 0;
    while(a && a%div == 0)
    {
        c++;
        if (div == 2)
            a >>= 1;
        else
            a /= div;
    }
    while(b && b%div == 0)
    {
        c--;
        if (div == 2)
            b >>= 1;
        else
            b /= div;
    }
    return c;
}

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

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

    if (r > 1)
    {
        total[2] = pwr(r, 2) - 2*pwr(r/2, 2);
        total[3] = pwr(r, 3) - 2*pwr(r/2, 3);
        total[5] = pwr(r, 5) - 2*pwr(r/2, 5);

        mult = (r%2 == 0 ? 1 : 2);
        if (d == 2)
            count += mult*(total[2] ? 1 : 0);
        else if (d == 3)
            count += mult*(total[3] ? 1 : 0);
        else if (d == 4)
            count += mult*(total[2] >= 2 ? 1 : 0);
        else if (d == 5)
            count += mult*(total[5] ? 1 : 0);
        else if (d == 6)
            count += mult*(total[2] && total[3] ? 1 : 0);
    }
 
    fprintf(out, "%d\n", count);
    fclose(in);
    fclose(out);
    return 0;  
}