Cod sursa(job #2663507)

Utilizator Senth30Denis-Florin Cringanu Senth30 Data 26 octombrie 2020 17:02:26
Problema GFact Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>
#define MAX 131072
#define MOD 1000000007

using namespace std;
const int NMAX = 1005;

int P, Q, K, ans;
int fact[NMAX], p[NMAX];

int main(){

    freopen("gfact.in", "r", stdin);
    freopen("gfact.out", "w", stdout);

    scanf("%d%d", &P, &Q);
    if(P % 2 == 0)
        fact[++K] = 2;
    while(P % 2 == 0){
        p[K]++;
        P /= 2;
    }
    for(int i = 3; i * i <= P; i += 2){
        if(P % i != 0) continue;
        fact[++K] = i;
        while(P % i == 0){
            ++p[K];
            P /= i;
        }
    }
    if(P > 2){
        fact[++K] = P;
        p[K] = 1;
    }
    for(int i = 1; i <= K; ++i)
        p[i] = p[i] * Q;
    for(int i = 1; i <= K; ++i){
        int x = 0;
        while(p[i] > 0){
            x += fact[i];
            int y = x;
            while(y % fact[i] == 0){
                --p[i];
                y /= fact[i];
            }
        }
        ans = max(ans, x);
    }
    printf("%d", ans);

    return 0;
}