Cod sursa(job #448900)

Utilizator ursu-valiJerdea Florin ursu-vali Data 4 mai 2010 22:26:05
Problema Numere 2 Scor 25
Compilator c Status done
Runda Arhiva de probleme Marime 0.9 kb
#include<stdio.h>
#define infile "numere2.in"
#define outfile "numere2.out"
long long p,a,b;
long long bmax;

void read()
{
	scanf("%lld\n",&p);
}

long long pows(long long a, long long b)
{
	if(!b) return 1;
	if(b==1) return a;
	if(b&1) return a*pows(a,b-1);
	long long x=pows(a,b>>1);
	return x*x;
}

void search_bmax()
{
	long long st=1,dr=31,mij;
	while(st<=dr)
	{
		mij=(st+dr)>>1;
		if(pows(2,mij)>=p) bmax=mij, dr=mij-1;
		else st=mij+1;
	}
}

void search_amin()
{
	long long st,dr,mij,x;
	for(b=bmax;b>=1;b--)
	{
		st=1,dr=p;
		while(st<=dr)
		{
			mij=(st+dr)>>1;
			x=pows(mij,b);
			if(x==p) a=mij, st=dr+1;
			else if(x<p) st=mij+1;
			else dr=mij-1;
		}
		if(a) break;
	}
}

void write()
{
	printf("%lld\n",a);
	printf("%lld\n",b);
}

int main()
{
	freopen(infile,"r",stdin);
	freopen(outfile,"w",stdout);
	
	read();
	search_bmax();
	search_amin();
	write();
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}