Cod sursa(job #2772262)

Utilizator SergiuS3003Sergiu Stancu Nicolae SergiuS3003 Data 31 august 2021 15:03:05
Problema Pascal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>

using namespace std;

ifstream f ( "pascal.in" );
ofstream g ( "pascal.out" );

int expp ( int n, int p )
{
    int e = 0;

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

    return e;
}

int cLucas ( int n, int p ) ///Ioan Tomescu
{
    int rez = n + 1, prod = 1;

    while ( n > 0 )
    {
        prod *= ( n % p + 1 );
        n /= p;
    }

    rez -= prod;
    return rez;
}

int main()
{
    int R, D, nrd = 0;
    f >> R >> D;

    if ( R >= 2 )
    {
        if ( D == 2 || D == 3 || D == 5 )
            nrd = cLucas ( R, D );
        else
        {
            int r2 = ( R + 1 ) / 2,
                k, ex2, ex3;

            if ( D == 4 )
            {
                ex2 = 2;

                for ( k = 1; k < r2; k++ )
                {
                    ex2 -= expp ( R - k + 1, 2 ) - expp ( k, 2 );
                    nrd += ex2 <= 0;
                }

                nrd *= 2;

                if ( R % 2 == 0 )
                {
                    ex2 -= expp ( r2 + 1, 2 ) - expp ( r2, 2 );
                    nrd += ex2 <= 0;
                }
            }
            else  //if(D == 6)
            {
                ex2 = ex3 = 1;

                for ( k = 1; k < r2; k++ )
                {
                    ex2 -= expp ( R - k + 1, 2 ) - expp ( k, 2 );
                    ex3 -= expp ( R - k + 1, 3 ) - expp ( k, 3 );
                    nrd += ex2 <= 0 && ex3 <= 0;
                }

                nrd *= 2;

                if ( R % 2 == 0 )
                {
                    ex2 -= expp ( r2 + 1, 2 ) - expp ( r2, 2 );
                    ex3 -= expp ( r2 + 1, 3 ) - expp ( r2, 3 );
                    nrd += ex2 <= 0 && ex3 <= 0;
                }
            }
        }
    }

    g << nrd;
    f.close();
    g.close();
    return 0;
}