Cod sursa(job #2769196)

Utilizator ps2001Silviu Popescu ps2001 Data 14 august 2021 08:30:54
Problema Suma divizorilor Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <bits/stdc++.h>

using namespace std;

long long exp(int a, int b) {
    if (b == 0) return 1 % 9901;
    if (b % 2 == 0) return exp((a*a) % 9901, b / 2) % 9901;
    return a*exp((a*a) % 9901, b/2) % 9901;
}

int gcd(int a, int b, int &x, int &y) {
    if (b==0) {
        x = 1;
        y = 0;
        return a;
    }

    int x0, y0, d;
    d = gcd(b, a%b, x0, y0);

    x = y0;
    y = x0 - (a/b)*y0;

    return d;
}

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

    int a, b;
    fin >> a >> b;

    vector<int> prime;
    vector<bool> prim(100001, true);

    for (int i = 2; i <= 100000; i++) {
        if (prim[i] == true) {
            prime.push_back(i);
            for (int j = i * 2; j <= 100000; j += i)
                prim[j] = false;
        }
    }

    long long number = 1, sum = 1, d = 0;

    while (a > 1 && prime[d] * prime[d] <= a) {
        long long p = 1, count = 0;

        while (a % prime[d] == 0) {
            //p = (p * prime[d]) % 9901;
            a /= prime[d];
            count++;
        }

        //cout << count << '\n';

        int copy = b;
        if (count != 0) {
            p = exp(prime[d], copy * count);

            int tmp = (prime[d] - 1) % 9901;
            int x, y;
            int D = gcd(tmp, 9901, x, y);


            while (x < 0)
                x+=9901;

            int inv = x / D;
            sum = (sum * ((((p * prime[d]) % 9901) + 9900) % 9901 * inv)) % 9901;
            //cout << p << ' ' << inv << ' ' << sum << '\n';
        }

        d++;
    }

    if (a != 1) {
        //cout << "aici\n";
        int copy = b;
        long long p = 1;

        p = exp(a % 9901, copy);
        int tmp = (a - 1) % 9901;
        int x, y;
        int D = gcd(tmp, 9901, x, y);

        while (x < 0)
            x+=9901;

        int inv = x / D;
        sum = (sum * ((((p * a) % 9901) + 9900) % 9901 * inv)) % 9901;
    }

    fout << sum << '\n';

    return 0;
}