Cod sursa(job #1013179)

Utilizator fustosroberttFustos Robert fustosrobertt Data 20 octombrie 2013 15:09:09
Problema Suma divizorilor Scor 100
Compilator cpp Status done
Runda micuti1 Marime 1.87 kb
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

const int mod = 9901;

ifstream fin ("sumdiv.in");
ofstream fout ("sumdiv.out");

bool phi[(int)(1e5+5)];
vector <int> prim;
vector <pair <int, int> > D;
int a, b;

void PHI() {
    phi[1] = 1;
    prim.push_back (2);
    int i = 3;
    for (; i < (1e5+5) / 2; i += 2)
        if (!phi[i]) {
            prim.push_back (i);
            for (int j = i * 2; j < 1e5+5; j += i)
                phi[j] = 1;
        }
    for (;i < 1e5 + 5; i += 2)
        if (!phi[i])
            prim.push_back (i);
}

int POW (int a, int b) {
    long long tmp = a % mod, sol = 1;
    for (int i = 1; i <= b; i <<= 1) {
        if (i & b)
            sol = (sol * tmp) % mod;
        tmp = (tmp * tmp) % mod;
    }
    return sol;
}

int main() {
    PHI();
    fin >> a >> b;
    fin.close();
    int aux = a;
    for (vector <int> :: iterator it = prim.begin(); a != 1 && *it <= a && it != prim.end(); ++it)
        if (!(a % *it)) {
            D.push_back (make_pair (*it, 0));
            while (!(a % *it)) {
                a /= *it;
                D.back().second += b;
            }
            if (a < 1e5 + 5 && !phi[a])
                break;
        }
    if (D.empty())
        D.push_back (make_pair (aux, b));
    else
        if (a > 1)
            D.push_back (make_pair (a, b));
    int res = 1;
    if (aux != 1)
        for (vector <pair <int, int> > :: iterator it = D.begin(); it != D.end(); ++it) {
            if ((*it).first % mod == 1) {
                res = (res * (b + 1)) % mod;
                continue;
            }
            res = (res * (POW((*it).first, (*it).second + 1) - 1)) % mod;
            res = (res * (POW((*it).first -1, mod - 2))) % mod;
        }
    while (res < 0)
        res += mod;
    fout << res;
    fout.close();
}