107

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>

#define ll long long
#define MOD 1000000007
using namespace std;

int _pow(int n,int p)
{
	if(p==0)
		return 1;
	else
		return n*_pow(n,p-1);
}

int isAnyRoot(int a,int b)
{
	if(a%b!=0)
		return -1;
	int cnt=0;
	while(a>=b)
	{
		cnt+=1;
		a=a/b;
		if(a>=b && a%b!=0)
			return -1;
	}
	return cnt;
}
int main()
{
	//cout<<_pow(6,2)<<"\n";
	int H,W,m,n,non_working_cat;
	int c1,c2;
	int total_non_working_cat , height_of_the_cat_stack , height_of_each_internal_cat;
	while(scanf("%d%d",&H,&W),H!=0,W!=0)
	{
		if(H==1)
		{
			printf("%d %d\n",0,H);
			continue;
		}
		//that means there is one working cat ; so there can be two possible scenarios
		//1. if the height of the initial cat is nth power of 2,
		//i.e. just next(inside of which hat working cat is staying) non working cat
		//whose height should be 2 so that 2/N+1==1;
		// In this acse total non working cat will be n (2^n=initial height), and height of the cat stack = 2+4+...2^n
		//2. if the height of the initial cat is not nth power of 2; then 1=(Height)/N+1, so N+1=Height,
		//means total cat= Height of the initial cat
		//only one is non working rest are working cat
		//and total height should be (N*1+initial height) or (N*1+1) if they are considered in a same ground
		if(W==1)
		{
			m = isAnyRoot(H,2);
			if(m!=-1)
			{
				printf("%d %d\n",m,_pow(2,m+1)-1);
			}
			else
			{
				printf("%d %d\n",1,2*H-1);
			}
			continue;
		}
		/*
		m=0;
		while(++m)
		{
			n = pow(W*1.0,1.0/m);
			if(H<=pow(n+1, m))
			break;
		}*/
		n=1;
		while(1)
		{
			n+=1;
			c1 = isAnyRoot(W,n);
			c2 = isAnyRoot(H,n+1);
			if(c1==c2 && c1!=-1)
				break;
		}
		m=c1;
		total_non_working_cat = 0;
		height_of_the_cat_stack = 0;
		for(int i=0;i<m;i++)
		{
			non_working_cat = _pow(n,i);
			total_non_working_cat += non_working_cat;
			height_of_each_internal_cat = (H/_pow(n+1,i));
			height_of_the_cat_stack += (non_working_cat*height_of_each_internal_cat);
		}
		height_of_the_cat_stack += W;//as working cats are of height 1.
		printf("%d %d\n",total_non_working_cat,height_of_the_cat_stack);
	}
	return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *