Cod sursa(job #584298)

Utilizator preda_alexandruPreda Alexandru preda_alexandru Data 24 aprilie 2011 21:36:41
Problema Pascal Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.71 kb
#include<stdio.h>

int main()
{
    int R, D, i, c = 0, pow = 0, pow2 = 0, q;
    FILE *f, *g;
    f = fopen("pascal.in", "rt");
    g = fopen("pascal.out", "wt");
    fscanf(f, "%i%i", &R, &D);
    switch (D)
    {
    case 4:
        for(i=1; i<=R/2; i++)
        {
            q = R-i+1;
            while(!(q%2))
            {
                pow++;
                q/=2;
            }
            q = i;
            while(!(q%2))
            {
                pow--;
                q/=2;
            }
            if(pow > 1)
                c++;
        }
        break;
    case 6:
        for(i=1; i<=R/2; i++)
        {
            q = R-i+1;
            while(!(q%2))
            {
                pow++;
                q/=2;
            }
            while(!(q%3))
            {
                pow2++;
                q/=3;
            }
            q = i;
            while(!(q%2))
            {
                pow--;
                q/=2;
            }
            while(!(q%3))
            {
                pow2--;
                q/=3;
            }
            if(pow > 0 && pow2 > 0)
                c++;
        }
        break;
    default:
        for(i=1; i<=R/2; i++)
        {
            q = R-i+1;
            while(!(q%D))
            {
                pow++;
                q/=D;
            }
            q = i;
            while(!(q%D))
            {
                pow--;
                q/=D;
            }
            if(pow > 0)
                c++;
        }
    }

    if(c)
        if(R%2)
            c = 2*c;
        else
            c = 2*c - 1;

    fprintf(g, "%i", c);
    fclose(f);
    fclose(g);
    return 0;
}