Cod sursa(job #1939199)

Utilizator MaligMamaliga cu smantana Malig Data 25 martie 2017 15:37:17
Problema Suma si numarul divizorilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <iomanip>

using namespace std;
ifstream in("ssnd.in");
ofstream out("ssnd.out");

const int valMax = 1e6 + 5;
const int mod = 9973;
typedef long long ll;

ll T,N;
int nrPrimes;
int primes[valMax];
bool notPrime[valMax];

void ciur();
ll pw(ll,ll);

int main()
{
    ciur();

    in>>T;
    while (T--) {
        in>>N;

        ll nrDiv = 1,sumTop = 1,sumBot = 1,p;
        for (int i=1;(p = primes[i])!=0 && p*p <= N;++i) {
            if (N % p != 0) {
                continue;
            }

            int nr = 0;
            while (N % p == 0) {
                ++nr;
                N /= p;
            }

            nrDiv *= (nr+1);
            sumTop = (sumTop * (pw(p,nr+1) - 1 + mod)) % mod;
            sumBot = (sumBot * (p-1)) % mod;
        }
        if (N != 1) {
            nrDiv *= 2;
            sumTop = (sumTop * (pw(N,2) -1 + mod)) % mod;
            sumBot = (sumBot * (N-1)) % mod;
        }

        out<<nrDiv<<' '<<(sumTop * pw(sumBot,mod-2)) % mod<<'\n';
    }

    return 0;
}

void ciur() {
    primes[++nrPrimes] = 2;
    for (int i=3;i<=valMax;i+=2) {
        if (notPrime[i]) {
            continue;
        }

        primes[++nrPrimes] = i;
        for (int j=3*i;j<=valMax;j+=2*i) {
            notPrime[j] = true;
        }
    }
}

ll pw(ll b,ll e) {
    ll res = 1;
    while (e>0) {
        if (e & 1 == 1) {
            res = (res * b) % mod;
        }
        b = (b*b) % mod;
        e >>= 1;
    }
    return res;
}