Cod sursa(job #1350774)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 20 februarie 2015 22:30:41
Problema Suma si numarul divizorilor Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;

#define max 1000000
#define ULL unsigned long long
#define MOD 9973

void sieveOfEratosthenes(bool a[]);
ULL modPow(ULL base, int exp);

int main()
{
    int t, i;
    long long n;
    bool sieve[max + 1];
    sieveOfEratosthenes(sieve);

    ifstream f("ssnd.in");
    f >> t;
    int card, p, j, s;
    ofstream g("ssnd.out");
    for (i = 1; i <= t; i++)
    {
        f >> n;
        card = s = 1;
        for (j = 2; n != 1 && j <= max; j++)
        {
            if (sieve[j])
            {
                p = 0;
                while (n % j == 0)
                {
                    p++;
                    n /= j;
                }
                if (p)
                {
                    card *= (p + 1);
                    s = ((s % MOD) * (((modPow(j, p + 1) - 1) / (j - 1)) % MOD)) % MOD;
                }
            }
        }

        g << card << " " << s << "\n";
    }
    f.close();
    g.close();

    return 0;
}

void sieveOfEratosthenes(bool a[])
{
    int i, j;
    for (i = 2; i <= max; i++)
        a[i] = true;

    for (i = 4; i <= max; i += 2)
        a[i] = false;
    for (i = 3; i <= sqrt(max); i++)
        for (j = i * i; j <= max; j += i + i)
            a[j] = false;
}

ULL modPow(ULL base, int exp)
{
    ULL result = 1;
    while (exp)
    {
        if (exp & 1)
            result *= base;
        base *= base;
        exp >>= 1;
    }

    return result;
}