Cod sursa(job #2148451)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 1 martie 2018 18:48:35
Problema Zero 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>

using namespace std;

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

long long p;

long long cmmfp(long long x)
{
    p = 0;
    while(!(x & 1)) {
        p++;
        x >>= 1;
    }

    if(x == 1)
        return 2;
    else {
        int d = 3;

        while(d * d <= x) {
            if(x % d) continue;

            p = 0;
            while(!(x % d)) {
                x /= d;
                p++;
            }
            d += 2;
        }

        if(x > 1) {
            p = 1;
            return x;
        }
        else
            return d - 2;
    }
}

int main()
{
    int t = 10;

    while(t--) {
        long long n, b, x, nr = 0;

        fin >> n >> b;

        x = cmmfp(b);

        b = x;
        while(b <= n) {
            int k = n / b;

            nr += (((n + 1 - b) * 2 - (k - 1) * b) * k) / 2;

            b *=  x;
        }

        fout << nr / p << '\n';
    }

    return 0;
}