Cod sursa(job #1830759)

Utilizator xRoALexBirtoiu Alexandru xRoALex Data 17 decembrie 2016 07:59:53
Problema GFact Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.47 kb
#include <cstdio>

using namespace std;

FILE *f=fopen("gfact.in","r");
FILE *g=fopen("gfact.out","w");

int baza,putere;

int k,v[]= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
struct element
{
    int put,exp;
};
element fv[500];

int cifre(int x,int b)
 {
   int s=0;
   while(x)
   {
       x=x/b;
       s+=x;
   }
   return s;
 }

void divizor(int x)
{
    int e,i=0,j;
    while(v[i]*v[i]<=x)
    {
        if(x%v[i]==0)
        {
            e=0;
            while(x%v[i]==0)
                e++,x/=v[i];
            if(k==0)
            {
                k++;
                fv[k].put=v[i];
                fv[k].exp+=e;
            }else
            {
                int ok=0;
            for(j=k;j>=1;j--)
            {
                if(fv[j].put==v[i])
                    fv[j].exp+=e,ok=1;
            }
            if(ok==0)
            {k++;
                fv[k].put=v[i];
                fv[k].exp+=e;}
            }
        }
        i++;
    }
    if(x!=1&&(x%v[i]==0))
    {
        e=0;
        while(x%v[i]==0)
            e++,x/=v[i];
        if(k==0)
            {
                k++;
                fv[k].put=v[i];
                fv[k].exp+=e;
            }
            else
            {
                int ok=0;
            for(j=k;j>=1;j--)
            {
                if(fv[j].put==v[i])
                    fv[j].exp+=e,ok=1;
            }
            if(ok==0)
            {k++;
                fv[k].put=v[i];
                fv[k].exp+=e;}
            }
        i++;
    }
    if(x!=1)
         if(k==0)
            {
                k++;
                fv[k].put=x;
                fv[k].exp+=1;
            }
            else
            {
                int ok=0;
            for(j=k;j>=1;j--)
            {
                if(fv[j].put==x)
                    fv[j].exp+=1,ok=1;
            }
            if(ok==0)
            {k++;
                fv[k].put=x;
                fv[k].exp+=1;}
            }
        i++;
}

int rez1()
{
    int pas=1<<30,r=0,i;
    while(pas!=0)
    {
        for(i=1;i<=k;i++)
        if(cifre(r+pas,fv[i].put)<fv[i].exp)
            {r+=pas;break;}
         pas/=2;
    }
    return r;
}

int main()
{
    int i;
    fscanf(f,"%d%d",&baza,&putere);
    divizor(baza);
    if(putere!=1)
    for(i=1;i<=k;i++)
        fv[i].exp*=putere;
    fprintf(g,"%d",rez1()+1);
    return 0;
}