Cod sursa(job #1465827)

Utilizator om6gaLungu Adrian om6ga Data 28 iulie 2015 02:13:35
Problema Pascal Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 2.72 kb
#include <stdio.h>

int r, d, total[6];
int count = 0, i, aux;

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)
        {
            aux = r;
            while(aux)
            {
                aux >>= 1;
                total[2] += aux;
            }
            aux = i;
            while(aux)
            {
                aux >>= 1;
                total[2] -= aux;
            }
            aux = r-i;
            while(aux)
            {
                aux >>= 1;
                total[2] -= aux;
            }
        }
        if (d == 3 || d == 6)
        {
            aux = r;
            while(aux)
            {
                aux = (int)(((long long)0xAAAAAAABULL * aux) >> 33);
                //aux /= 3;
                total[3] += aux;
            }
            aux = i;
            while(aux)
            {
                aux = (int)(((long long)0xAAAAAAABULL * aux) >> 33);
                //aux /= 3;
                total[3] -= aux;
            }
            aux = r-i;
            while(aux)
            {
                aux = (int)(((long long)0xAAAAAAABULL * aux) >> 33);
                //aux /= 3;
                total[3] -= aux;
            }
        }
        if (d == 5)
        {
            aux = r;
            while(aux)
            {
                aux /= 5;
                total[5] += aux;
            }
            aux = i;
            while(aux)
            {
                aux /= 5;
                total[5] -= aux;
            }
            aux = r-i;
            while(aux)
            {
                aux /= 5;
                total[5] -= aux;
            }
        }
        
        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;   
}