Cod sursa(job #1715788)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 11 iunie 2016 14:57:32
Problema GFact Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>

#define BUF_SIZE 16384
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi;
inline char nextch(){
    if(pbuf==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fi);
        pbuf=0;
    }
    return buf[pbuf++];
}
inline int nextnum(){
    int a=0;
    char c=nextch();
    while(c<'0' || c>'9')
        c=nextch();
    while('0'<=c && c<='9'){
        a=a*10+c-'0';
        c=nextch();
    }
    return a;
}

int pow[100][2], last;

inline int ok(long long a){
    for(int i=0;i<last;i++){
        long long ca=a;
        long long p=pow[i][0];
        long long sum=0;
        while(p<=ca){
            sum+=ca/p;
            p*=pow[i][0];
        }
        if(sum<pow[i][1])
            return 0;
    }
    return 1;
}

int main(){
    int p, q;
    FILE*fo;
    fi=fopen("gfact.in","r");
    fo=fopen("gfact.out","w");
    p=nextnum();
    q=nextnum();
    int d=2;
    while(d*d<=p){
        if(p%d==0){
            pow[last][0]=d;
            while(p%d==0){
                pow[last][1]++;
                p/=d;
            }
            pow[last][1]*=q;
            last++;
        }
        d++;
    }
    if(p>1){
        pow[last][0]=p;
        pow[last][1]=q;
        last++;
    }
    long long st=1, dr=1LL*p*q;
    while(dr-st>1){
        long long mij=(st+dr)/2;
        if(ok(mij))
            dr=mij;
        else
            st=mij+1;
    }
    if(ok(st))
        fprintf(fo,"%d", st);
    else
        fprintf(fo,"%d", dr);
    fclose(fi);
    fclose(fo);
    return 0;
}