Cod sursa(job #731235)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 7 aprilie 2012 19:34:30
Problema Suma divizorilor Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb

#include <fstream>
//#include <iostream>
#include <cmath>

int main (void)
{
    const unsigned short MOD(9901);
    unsigned int a,b;
    std::ifstream input("sumdiv.in");
    input >> a >> b;
    input.close();
    unsigned short divizori [10];
    unsigned char puteri [10] = {0};
    unsigned short *dptr(divizori);
    unsigned char *pptr(puteri);
    {
        bool primi [7071];
        unsigned int s(std::sqrt(a));
        for (unsigned short i(3) ; i <= s ; i += 2)
            if (!primi[i])
                for (unsigned int j(i * 3) ; j <= s ; j += i)
                    primi[j] = true;
        if (!(a % 2))
        {
            *dptr = 2;
            do
            {
                a >>= 1;
                ++*pptr;
            }
            while (!(a % 2));
            ++dptr;
            ++pptr;
        }
        for (bool *ptr(primi + 3), *limit(primi + 7071) ; ptr < limit ; ptr += 2)
            if (*ptr)
            {
                unsigned short n(ptr - limit);
                if (!(a % n))
                {
                    *dptr = n;
                    do
                    {
                        a /= n;
                        ++*pptr;
                    }
                    while (!(a % n));
                    ++dptr;
                    ++pptr;
                }
            }
    }
    unsigned long long result(1),power,ex;
    --dptr;
    --pptr;
    while (dptr >= divizori)
    {
        //std::cout << *dptr << '^' << (short)*pptr << '\n';
        ex = *pptr;
        ex *= b;
        power = *dptr;
        while (ex)
        {
            power *= *dptr;
            if (power > MOD)
                power /= MOD;
            --ex;
        }
        result *= (power - 1);
        result /= (*dptr - 1);
        if (result > MOD)
            result %= MOD;
        --dptr;
        --pptr;
    }
    std::ofstream output("sumdiv.out");
    output << result << '\n';
    output.close();
    return 0;
}