Cod sursa(job #58493)

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

    if ( p == 2 )
    {
        while ( (n & 1) == 0 )
        {
            ++cnt;
            n = (n >> 1);
        }

        return cnt;
    }

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

    return cnt;
}

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;
            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);
            b[i] = (b[i-1] + t1 - p1);
            if ( a[i] > 1 && b[i] > 1 )
                ++cnt;
        }
    }

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

    return 0;
}