Cod sursa(job #58482)

Utilizator DastasIonescu Vlad Dastas Data 5 mai 2007 23:09:54
Problema Pascal Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <cstdio>
#define max 5000001

FILE *in = fopen("pascal.in","r"), *out = fopen("pascal.out","w");

int r, d;
int a[max];

int putere(int n, int p)
{
    int cnt = 0;

    while ( n % p == 0 )
    {
        cnt++;
        n /= p;
    }

    return cnt;
}

// Ac = Ac-1 + puterea lui p in (r-c+1) - puterea lui p in c

int main()
{
    fscanf(in, "%d %d", &r, &d);

    int cnt = 0;
    a[0] = 0;

    for ( int i = 1; i <= r; ++i )
    {
        int t = 0, p = 0;
        t = putere((r-i+1), d);
        p = putere(i, d);

        a[i] = (a[i-1] + t - p);
        if ( a[i] > 0 )
            ++cnt;
    }

//    for ( int i = 0; i <= r; ++i )
//        printf("%d ", a[i]);
//    printf("\n");
//
//    printf("\n%d\n", putere(3, 2));

    fprintf(out, "%d\n", cnt);

    return 0;
}