Cod sursa(job #58487)

Utilizator DastasIonescu Vlad Dastas Data 5 mai 2007 23:21:53
Problema Pascal Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 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;

    int prime = 1;
    if ( d == 4 || d == 6 )
        prime = 0;


    if ( prime )
    {
        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;
        }
    }
    else if ( d == 4 )
    {
        for ( int i = 1; i <= r; ++i )
        {
            int t = 0, p = 0;
            t = putere((r-i+1), 2);
            p = putere(i, 2);

            a[i] = (a[i-1] + t - p);
            if ( a[i] > 1 )
                ++cnt;
        }
    }
    else
    {
        int *b = new int[max];
        b[0] = 0;
        for ( int i = 1; i <= r; ++i )
        {
            int t = 0, t1 = 0, p = 0, p1 = 0, ok = 1;
            t = putere((r-i+1), 2);
            p = putere(i, 2);

            t1 = putere((r-i+1), 3);
            p1 = putere(i, 3);


            a[i] = (a[i-1] + t - p);
            if ( a[i] < 1 )
                ok = 0;
            b[i] = (b[i-1] + t1 - p1);
            if ( b[i] < 1 )
                ok = 0;

            if ( ok )
                ++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;
}