Cod sursa(job #2052679)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 30 octombrie 2017 21:31:08
Problema Suma divizorilor Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#define LL long long

using namespace std;

ifstream f("sumdiv.in");
ofstream g("sumdiv.out");

const int mod = 9901;
bool prim[23500];
int P[5000], np, n, k, i, x;
int nr[5000], exp[5000],t;

void ciur(int n) {
    int i, j;
    P[np=1] = 2;
    for (i = 3; i <= n; i += 2)
        if (prim[i] == 0) {
            P[++np] = i;
            for (j = 3*i; j <= n; j += 2*i)
                prim[j] = 1;
        }
}

void euclid(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1, y = 0;
        return;
    }
    int xx, yy;
    euclid(b, a%b, xx, yy);
    x = yy;
    y = xx-(a/b)*yy;
}

int inv_mod(int n) {
    int x, y;
    euclid(n, mod, x, y);
    if (x < 0) x += mod;
    return x;
}

int pow(LL n, LL k) {
    int sol = 1, a = n;
    while (k) {
        if (k&1) sol = (1LL*sol*a)%mod;
        a = (1LL*a*a)%mod;
        k >>= 1;
    }
    return sol;
}

int main() {
    ciur(23000);
    f >> n >> k;
    int n1 = n;
    for (i = 1; n1 > 1; i++) {
        if (P[i]*P[i] >= n1 && n1 > 1)
            exp[++t] = k, nr[t] = n1, n1 = 1;
        if (n1%P[i] == 0) {
            nr[++t] = P[i];
            while (n1%P[i] == 0)
                exp[t]+=k, n1 /= P[i];
        }
    }
    int sol = 1;
    for (i = 1; i <= t; i++) {
        nr[i] %= mod;
        if (nr[i] == 0)
            continue;
        else if (nr[i] == 1) {
            sol = (sol*(exp[i]+1));
            continue;
        }
        x = pow(nr[i], 1LL*exp[i]+1)-1;
        if (x < 0) x += mod;
        x  = (x*inv_mod(nr[i]-1))%mod;
        sol = (sol*x)%mod;
    }
    g << sol%mod;
}