Cod sursa(job #585675)

Utilizator AndreyPAndrei Poenaru AndreyP Data 30 aprilie 2011 10:56:32
Problema NumMst Scor 0
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Clasele 10-12 Marime 1.05 kb
#include <cstdio>
#include <cmath>
#define ll long long

int n,d;
double n1,d1;

inline double get(int k) {
    double k1 = (double)k;
    return (k1*log(d1) + (k1-1.000000)*log((double)(n/k)) + log((double)(n%k)));
}

inline int cmmdc(int a,int b) {
    int r;
    while(b) {
        r = a%b;
        a = b;
        b = r;
    }
    return a;
}

inline ll cmmmc(int x,int y) {
    if(y==0)
        return ((ll)x);
    return (((ll)(x/cmmdc(x,y)))*((ll)y));
}

int main() {
    freopen("nummst.in","r",stdin);
    freopen("nummst.out","w",stdout);

    scanf("%d",&n);
    for(d=n-1; d>0 && n%d!=0; --d)
        ;
    printf("%d ",d);
    n /= d;
    n1 = (double)n;
    d1 = (double)d;

    int p=2,u=n,m;

    while(p+16<u) {
        m = (p+u)>>1;

        if(get(m-1)<=get(m))
            p = m;
        else
            u = m-1;
    }

    double aux=get(p),aux1;
    int k=p;
    for(int i=p+1; i<=u; ++i) {
        aux1 = get(i);
        if(aux1>aux) {
            aux = aux1;
            k = i;
        }
    }

    printf("%lld\n",((ll)d)*cmmmc(n/k,n%k));
    return 0;
}