Cod sursa(job #905548)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 5 martie 2013 21:54:23
Problema Frac Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <math.h>
using namespace std;
ifstream f("frac.in");
ofstream g("frac.out");
long long p, u, m, v[100], aux, c, h, s, n, care, i, j, t, d;

long long afla(long long a){
    long long i, j, k, nr, x[100], u, p, s, aux;
    u=n;
    s=0;
    j=1;
    for(j=0; j<=t; j++)
        x[j]=0;
    while(x[0]!=1)
    {
        j=t;
        while(x[j]==1)
        {
            x[j]=0;
            j--;
        }
        x[j]=1;
        if(x[0]==1)
            break;
        p=1;
        nr=0;
        for(j=1; j<=t; j++)
            if(x[j]==1)
            {
                nr++;
                p*=v[j];
            }
        if(nr%2==0)
            s-=a/p;
        else
            s+=a/p;
    }
    return a-s;
}

int main(){
	f>>n>>care;
	f.close();
	c=n;
	d=2;
	int rn=(int)sqrt(n*1.0);
	while(c!=1 && d<=rn)
	{
		if(c%d==0)
		{
			v[++t]=d;
			while(c%d==0)
				c/=d;
		}
		d++;
	}
	if(c!=1)
		v[++t]=c;
	p=1;
	u=(1LL<<62);
	while(p<=u)
	{
		m=(p+u)/2;
		h=afla(m);
        if(h>=care)
        {
            u=m-1;
            s=m;
        }
        else
            p=m+1;

	}
	g<<s<<"\n";
	g.close();
	return 0;
}