Cod sursa(job #1746546)

Utilizator antanaAntonia Boca antana Data 23 august 2016 15:37:13
Problema Pascal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <cstdio>
#define MAXN 5000000

inline int nrput(int x, int prim)
{
    int f=prim, ans=0;

    while(f <= x)
    {
        ans+=x/f;
        f*=prim;
    }

    return ans;
}
int main()
{
    freopen("pascal.in", "r", stdin);
    freopen("pascal.out", "w", stdout);

    int r, d, ans=0, i, lim;

    scanf("%d%d", &r, &d);

    lim=r/2;
    for(i=1;i<lim;++i)
    {
        if(d==2 || d==3 || d==5){
            if(nrput(r, d) - nrput(r-i, d) - nrput(i, d))
                ans++;
        }
        else
        {
            if(nrput(r, d) - nrput(r-i, d) - nrput(i, d))
                ans++;
        }
    }

    ans*=2;
    if(!(r%2))
    {
        if(d==2 || d==3 || d==5){
            if(nrput(r, d) - nrput(r-r/2, d) - nrput(r/2, d))
                ans++;
        }
        else
        {
            if(nrput(r, d) - nrput(r-r/2, d) - nrput(r/2, d))
                ans++;
        }
    }

    printf("%d", ans);

    return 0;
}