Cod sursa(job #1000518)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 23 septembrie 2013 00:57:10
Problema Pascal Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
#include <iostream>
#include <fstream>

using namespace std;

int R, D;
int sol;
int nrdiv;
int exp[10];
struct DIV
{
    int d, e;
};
DIV a[3];

inline void Read()
{
    ifstream f ("pascal.in");
    f>>R>>D;
    f.close();
}

inline void CreateDivsList()
{
    switch(D)
    {
        case 2:
            nrdiv = 1;
            a[1].d = 2;
            a[1].e = 1;
            break;
        case 3:
            nrdiv = 1;
            a[1].d = 3;
            a[1].e = 1;
            break;
        case 4:
            nrdiv = 1;
            a[1].d = 2;
            a[1].e = 2;
            break;
        case 5:
            nrdiv = 1;
            a[1].d = 5;
            a[1].e = 1;
            break;
        case 6:
            nrdiv = 2;
            a[1].d = 2;
            a[1].e = 1;
            a[2].d = 3;
            a[2].e = 1;
            break;
    }
}

inline void Solve()
{
    CreateDivsList();
    int i, lim = (R+1) / 2 - 1, j, x;
    bool ok;
    for (i = 1; i<=lim; ++i)
    {
        /// * (R-i) / (i+1)
        x = R-(i-1);
        for (j=1; j<=nrdiv; ++j)
        {
            while (x%a[j].d == 0)
            {
                ++exp[a[j].d];
                x/=a[j].d;
            }
        }
        x = i;
        for (j=1; j<=nrdiv; ++j)
        {
            while (x%a[j].d == 0)
            {
                --exp[a[j].d];
                x/=a[j].d;
            }
        }
        ok = true;
        for (j=1; j<=nrdiv; ++j)
        {
            if (exp[a[j].d] < a[j].e)
                ok = false;
        }
        if (ok)
            sol += 2;
    }
    if (R % 2 == 0)
    {
        x = R-(i-1);
        for (j=1; j<=nrdiv; ++j)
        {
            while (x%a[j].d == 0)
            {
                ++exp[a[j].d];
                x/=a[j].d;
            }
        }
        x = i;
        for (j=1; j<=nrdiv; ++j)
        {
            while (x%a[j].d == 0)
            {
                --exp[a[j].d];
                x/=a[j].d;
            }
        }
        ok = true;
        for (j=1; j<=nrdiv; ++j)
        {
            if (exp[a[j].d] < a[j].e)
                ok = false;
        }
        if (ok)
            ++sol;
    }
}

inline void Write()
{
    ofstream g("pascal.out");
    g<<sol<<"\n";
    g.close();
}

int main()
{
    Read();
    Solve();
    Write();
    return 0;
}