1.

/**
 * Angewandte Mathematik, SS11


 * Problem: 107 The Cat in the Hat
 * Link:
http://uva.onlinejudge.org/external/3/374.pdf
 *
 * @author Westerfield, Christopher
 * @author Gent Selmanaj


 * @author Marco Schuster
 * @version 1.0, 04/19/2011
 *
 * Method : Ad-Hoc
 * Status : Accepted
 * Runtime: 0.140
 */

import java.util.Scanner;
public class Main
{
    static final double EPSILON = 0.000001;


    static int tallest, workers, n;
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args)
    {   
        while (testcase());
    }
    public static boolean testcase()


    {
        tallest = sc.nextInt();
        workers = sc.nextInt();
        if (tallest == 0 && workers == 0)
            return false;
        calcN();
        int lazy = 0;
        int stack = tallest;


        int h = tallest;
        int c = 1;
        while (h > 1)
        {
            lazy += c;
            c *= n;
            h = (int) (h / (1.0+n) + EPSILON);
            stack += h * c;   


        }
        System.out.printf("%d %d%n", lazy, stack);
        return true;
    }
    public static void calcN()
    {
        double m1 = -1;
        double m2 = -1;
        for (int k = 2; k <= 31; k++)


        {
            m1 = Math.pow(tallest, 1.0 / k);
            if (Math.abs(m1 - Math.round(m1)) < EPSILON)
            {
                m2 = Math.pow(workers, 1.0 / k);
                if (Math.abs(m1 - m2 - 1) < EPSILON)


                {
                    n = (int) (m2 + EPSILON);
                    return;
                }
            }
        }       
        n = tallest-1;
    }
}

2.

/**
* Angewandte Mathematik, SS11
* Problem: 107 The Cat in the Hat
* Link: http://acm.uva.es/p/v1/107.html
*
* @author Fabian Trampusch
* @author Robert Schwarz
* @version 1.0, 08.05.2011
*
* Method : Logarithm.
* Status : Accepted
* Runtime: 1.612
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
static double EPSILON = 0.000001;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
long startMillis = System.currentTimeMillis();

while (true) {
st = new StringTokenizer(br.readLine());

int heightInitialCat = Integer.parseInt(st.nextToken());
int countWorkerCats = Integer.parseInt(st.nextToken());

// Special case "0 0"
if (heightInitialCat == 0 && countWorkerCats == 0) {
//System.out.println(System.currentTimeMillis() - startMillis);
return;
}

// Special Case "1 1"
if (heightInitialCat == 1 && countWorkerCats == 1) {
System.out.println("0 1");
continue;
}

// Special case "n+1 n"
if (heightInitialCat == countWorkerCats + 1) {
System.out.println("1 " + (heightInitialCat + countWorkerCats));
continue;
}

// Special case "n 1"
if (heightInitialCat != 1 && countWorkerCats == 1) {
int divCount = 0;
int newHeight = heightInitialCat;
int catHeightSum = heightInitialCat;

while (newHeight > 1) {
newHeight /= 2;
catHeightSum += newHeight;
divCount++;
}

System.out.println(divCount + " " + catHeightSum);
continue;
}

int rows = 0;
int catsPerHat = 2;
boolean solved = false;

while (!solved) {
rows = (int) (Math.log(countWorkerCats)
/ (Math.log(catsPerHat)) + EPSILON);
long tempCountWorkerCats = (long) Math.pow(catsPerHat, rows);

if (Math.pow(catsPerHat + 1, rows) == heightInitialCat
&& tempCountWorkerCats == countWorkerCats) {
solved = true;
} else {
catsPerHat++;
}
}

long countNonWorkingCats = (long) endlicheGeometrischeReihe(
catsPerHat, rows - 1);

long heightSum = heightInitialCat;
for (int i = 1; i < rows + 1; i++) {
heightSum += (long) (Math.pow(catsPerHat, i) * (heightInitialCat / Math
.pow(catsPerHat + 1, i)));
}

System.out.println(countNonWorkingCats + " " + heightSum);
}
}

public static double endlicheGeometrischeReihe(double summand, int potenz) {
if (summand != 1) {
double numerator = 1 - Math.pow(summand, potenz + 1);
return (numerator / (1 - summand));
} else {
return potenz + 1;
}
}

}

3.

/**
* Angewandte Mathematik, SS11
* Problem: 107 - The Cat in the Hat
* Link: http://uva.onlinejudge.org/external/1/107.pdf
*
* @author Benedikt Z�nnchen
* @author Erik Wenzel
* @version 1.0, 28/04/2011
*
* Method : Brute Force
* Status : Accepted
* Runtime: 1.984
*
* Ich habe zun�chst versucht das Problem in Java zu l�sen indem ich eine
* Primfaktorzerlegung vornehme. Die Ergenisse waren korrekt aber die Laufzeit
* zu langsam. Die Methode hier ist eher unsch�n aber Sie funktioniert (nur in C++).
*
* Aufgabe: L�se n^p = b und (n+1)^p = a wobei n,p gesucht und a,b gegeben ist.
*
* L�sung: Zun�chst beachtet man den Sonderfall wenn b = 1 ist. Ist b nicht =1 so berechnet man durch den log
* den ersten m�glichen exponenten �berpr�ft ihn f�r n+1 bzw. a. Wenn er passt ist man fertig
* wenn nicht holt man sich den n�chsten.
*
*/

#include <iostream>
#include <math.h>
using namespace std;

double power;
double n;

double getGeoSum(double a, double q, double n);
double getGeoSum2(double a, double n, double power);

int main() {
int a; // H�he der ersten Katzt
int b; // Anzahl der Katzten die arbeiten

while (true) {
cin>>a;
cin>>b;
// Abbruchbedingung
if (a == 0 && b == 0)
{
break;
}

double distance = 1;
n = 1; // basis
power = 0; // exponent

// sonderfall b=1
if (b == 1)
{
n = 1;
power = log(a) / log(2);
}
else // hier ist die eigentliche Logik!
{
// passt die basis so ist die distance sehr klein! (rundungsfehler)
while (distance > 0.00001)
{
// neue basis
n++;
// neuer exponent zur basis
power = log(b)/log(n);
// passt der exponent auch f�r basis+1 und a ?
distance = pow((n + 1), power) - a;
}
}

// sonderfall b=1
if (n == 1)
{
cout << (int)power << " " << (int)getGeoSum(a, (n / (n + 1)), power)<<endl;
}
else
{
// nun werden nur noch die formeln f�r die geometrische Reihe angewendet
cout << (int)(getGeoSum(1, n, power - 1) + 0.1) << " " << (int)(getGeoSum2(a, (n / (n + 1)), power))<<endl;
}

}
return 0;
}

/**
* Berechnet die Summe eienr geometrischen Reihe
* dabei wird der rundungsfehler wieder korrigiert!
*/
double getGeoSum2(double a, double n, double power)
{
double total = a;
// +0.1 ausgleich des m�glichen rundungsfehlers
for(int i = 1; i <= power+0.1; i++)
{
a *= n;
total += a;
}
return total;
}

/**
* Berechnet die Summe eienr geometrischen Reihe
*/
double getGeoSum(double a, double q, double n)
{
return a * (pow(q, (n + 1)) - 1.0) / (q - 1.0);
}


4.

/**
* Angewandte Mathematik, SS11
* Problem: 107 The Cat in the Hat
* Link: http://uva.onlinejudge.org/external/1/107.pdf
*
* @author Ritter Marius
* @author Wende Tom
* @author Zehentbauer Stefan
* @version 1.0, 05/09/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.816
*/

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

private double ggT(double n, double m) {
double r = m % n;
while (r > 0) {
m = n;
n = r;
r = m % n;
}
return n;
}

private double prim(double example, double example2) {
double count = 0;
ArrayList<Double> test = new ArrayList<Double>();
for (int i = 2; i <= example; i++) {
while (example % i == 0) {
example /= i;
count++;
}
if (count != 0) {
test.add(count);
}
if (example % i != 0) {
count = 0;
}
}
for (int i = 2; i <= example2; i++) {
while (example2 % i == 0) {
example2 /= i;
count++;
}
if (count != 0) {
test.add(count);
}
if (example % i != 0) {
count = 0;
}
}
double gg;
if (test.size() == 1) {
gg = test.get(0);
} else {
gg = new Main().ggT(test.get(0), test.get(1));
for (int i = 2; i < test.size(); i++) {
gg = new Main().ggT(gg, test.get(i));
}
}
return gg;
}

public static void main(String[] args) {
Scanner bsr = new Scanner(System.in);
double eingabe1 = bsr.nextDouble();
double eingabe2 = bsr.nextDouble();
while (eingabe1 != 0 && eingabe2 != 0) {
double exponent = 0;
double basis = 0;
double anzahlNixTuer = 0;
double groesse = 0;
double anzahlCats = 0;
if (eingabe1 == 1) {
groesse = 1;
anzahlNixTuer = 0;
} else {
if (eingabe2 == 1) {
exponent = new Main().prim(eingabe1, eingabe2);
basis = 2;
anzahlCats = 1;
double nexponent = exponent;
for (int i = 0; i < exponent; i++) {
double temp = Math.pow(anzahlCats, i);
anzahlNixTuer = anzahlNixTuer + temp;
groesse = groesse + temp * Math.pow(basis, nexponent);
nexponent--;
}
groesse = groesse + eingabe2;
} else {
exponent = new Main().prim(eingabe2, eingabe1);
anzahlCats = Math.round(Math.pow(eingabe2, 1 / exponent));
basis = Math.round(Math.pow(eingabe1, 1 / exponent));
double nexponent = exponent;
for (int i = 0; i < exponent; i++) {
double temp = Math.pow(anzahlCats, i);
anzahlNixTuer = anzahlNixTuer + temp;
groesse = groesse + temp * Math.pow(basis, nexponent);
nexponent--;
}
groesse = groesse + eingabe2;
}
}

long ausgabe1 = (long) groesse;
long ausgabe2 = (long) anzahlNixTuer;
System.out.print(ausgabe2 + " ");
System.out.println(ausgabe1);
//System.out.println(exponent);
//System.out.println(anzahlCats);
//System.out.println(basis);
eingabe1 = bsr.nextDouble();
eingabe2 = bsr.nextDouble();
if(eingabe1 == 0.0 && eingabe2 == 0.0){
break;
}
}
}

}


5.

package sem2.am.thecatinthehat;

/**
* Angewandte Mathematik, SS11
* Problem: 107 - the cat in the hat
* Link: http://uva.onlinejudge.org/external/1/107.pdf
*
* @author Florian Stein
* @author Franz Sommer
* @version 1.0, 05/15/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.100
*/

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// schleife faelle
while (sc.hasNext()) {

int initialHeight = sc.nextInt();
int workingCats = sc.nextInt();

// abbruch bei negativen zahlen
if (initialHeight == 0 && workingCats == 0) {
break;
}

int rowsBetween = 0;
int catsPerHat = 0;
int nonWorkingCats = 0;
int totalHeight = 0;

// N finden - brute forcen
for (int i = 2; i <= workingCats; ++i) {
rowsBetween = (int) Math.round((Math.log(workingCats) / Math
.log(i)));

if (Math.round(pow(i + 1, rowsBetween)) == initialHeight
&& Math.round(pow(i, rowsBetween)) == workingCats) {
catsPerHat = i;
break;
}
}

// sonderfaelle abfangen
if (workingCats == 0) {
System.out.println("1 1");
} else if (workingCats == 1) {
rowsBetween = (int) (Math.round(Math.log(initialHeight)
/ Math.log(2)));
totalHeight = (int) Math.round(pow(2, rowsBetween + 1)) - 1;
System.out.println(rowsBetween + " " + totalHeight);
} else if (initialHeight == 1) {
System.out.println("0 " + rowsBetween);
} else {

// berechnung der nicht arbeitenden katzen
nonWorkingCats = (int) ((pow(catsPerHat, rowsBetween) - 1) / (catsPerHat - 1));

// berechnung der hoehe aller katzen
totalHeight = (int) Math
.round((initialHeight * ((pow(catsPerHat
/ (double) (catsPerHat + 1), rowsBetween + 1) - 1) / (catsPerHat
/ (double) (catsPerHat + 1) - 1))));

// ausgabe
System.out.println(nonWorkingCats + " " + totalHeight);
}
}

}

// schnelleres pow
private static double pow(double x, double y) {

double ret = 1;

for (int i = 0; i < y; i++) {
ret = ret * x;
}

return ret;
}
}

6.

/**
* Angewandte Mathematik, SS11
* Problem: 107 Cat in the hat
* Link: http://uva.onlinejudge.org/external/1/107.pdf
* @author Brielbeck, Daniel
* @author Weber, Georg
* @version 1.0, 05/02/2011
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.536
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
public static void main(String[] args) throws Exception {
int x, k;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = in.readLine()) != null) {
x = Integer.parseInt(input.split(" ")[0]);
k = Integer.parseInt(input.split(" ")[1]);

if (x == 0 || k == 0) break;

if (x == 1 && k == 1) System.out.printf("0 1\n");
else if (x == 1) System.out.printf("1 %d\n", k);

function(x, k);
}
}

static void function(int x, int k) {
for (int n = 2; n <= x; ++n) {
int xx = x;
int cat = 1;
int allCat = 0, notWorkCat = 0;

while ((xx % n) == 0) {
allCat += xx * cat;
notWorkCat += cat;
xx = xx / n;
cat = cat * (n - 1);
}

if (cat == k) {
allCat += cat;
System.out.printf("%d %d\n", notWorkCat, allCat);
break;
}
}
}
}

7.
/**
* FWP, Angewandte Mathematik, SS11
* Problem: 107 - The Cat in the Hat
* Link: http://uva.onlinejudge.org/external/1/107.pdf
*
* @author Sebastian Stumpf
* @author Benjamin Vogt
* @version 1.0, 2011-05-09 19:05:57
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.196
*/

import java.io.*;
import java.util.*;

public class Main
{
public static void main(String args[]) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(reader.readLine());
// the height of the "mothercat"
int h0 = Integer.valueOf(token.nextToken());
// the working cats
int w = Integer.valueOf(token.nextToken());
while(h0 != 0 || w != 0)
{
int nonWorkingCats;
int totalHeight;
// the depth of the cat Tree
int p;
// catching values that doesnt fit with the for loop
if(w == 0)
{
System.out.println("1 1");
}
else if(w == 1)
{
p = (int) (Math.round(Math.log(h0) / Math.log(2)));
nonWorkingCats = p;
totalHeight = (int) Math.round(Math.pow(2, p + 1)) - 1;
System.out.println(nonWorkingCats + " " +totalHeight);
}
else if(h0 == 1)
{
System.out.println("0 "+w);
}
else
{
// computing the nonWorkingCats and the totalHeight of Cats
// trying different N smaller than w and generating p for that N by solving the equation
// w = N^p for p. Then check if the p and the N fit in the equations w = N^p and
// h0 = (N+1)^p. If they do, compute nonWorkingCats and totalHeight with the formula
// for geometric series.
for(int N = 2; N <= w; ++N)
{
p = (int) Math.round((Math.log(w) / Math.log(N)));
if(Math.round(Math.pow(N+1, p)) == h0 && Math.round(Math.pow(N, p)) == w)
{
nonWorkingCats = (int) ((Math.pow(N, p) - 1) / (N - 1));
totalHeight = (int) Math.round((h0 * ((Math.pow(N/(double)(N+1), p+1) - 1) / (N/(double)(N+1) - 1))));
System.out.println(nonWorkingCats + " " +totalHeight);
break;
}
}
}
token = new StringTokenizer(reader.readLine());
h0 = Integer.valueOf(token.nextToken());
w = Integer.valueOf(token.nextToken());
}
}
}


8.

/**
* FWP, Angewandte Mathematik, SS11
* Problem: 107 - The Cat in the Hat
* Link: http://uva.onlinejudge.org/external/1/107.pdf
*
* @author Sebastian Stumpf
* @author Benjamin Vogt
* @version 1.0, 2011-05-09 19:05:57
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.196
*/

import java.io.*;
import java.util.*;

public class Main
{
public static void main(String args[]) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(reader.readLine());
// the height of the "mothercat"
int h0 = Integer.valueOf(token.nextToken());
// the working cats
int w = Integer.valueOf(token.nextToken());
while(h0 != 0 || w != 0)
{
int nonWorkingCats;
int totalHeight;
// the depth of the cat Tree
int p;
// catching values that doesnt fit with the for loop
if(w == 0)
{
System.out.println("1 1");
}
else if(w == 1)
{
p = (int) (Math.round(Math.log(h0) / Math.log(2)));
nonWorkingCats = p;
totalHeight = (int) Math.round(Math.pow(2, p + 1)) - 1;
System.out.println(nonWorkingCats + " " +totalHeight);
}
else if(h0 == 1)
{
System.out.println("0 "+w);
}
else
{
// computing the nonWorkingCats and the totalHeight of Cats
// trying different N smaller than w and generating p for that N by solving the equation
// w = N^p for p. Then check if the p and the N fit in the equations w = N^p and
// h0 = (N+1)^p. If they do, compute nonWorkingCats and totalHeight with the formula
// for geometric series.
for(int N = 2; N <= w; ++N)
{
p = (int) Math.round((Math.log(w) / Math.log(N)));
if(Math.round(Math.pow(N+1, p)) == h0 && Math.round(Math.pow(N, p)) == w)
{
nonWorkingCats = (int) ((Math.pow(N, p) - 1) / (N - 1));
totalHeight = (int) Math.round((h0 * ((Math.pow(N/(double)(N+1), p+1) - 1) / (N/(double)(N+1) - 1))));
System.out.println(nonWorkingCats + " " +totalHeight);
break;
}
}
}
token = new StringTokenizer(reader.readLine());
h0 = Integer.valueOf(token.nextToken());
w = Integer.valueOf(token.nextToken());
}
}
}


-----------------------------------------------------------


1.

package acm_107_the_cat_in_the_hat;

/**
* FWP, Ausgew¦hlte Probleme aus dem ACM Programming Contest, SS10
* Problem: acm_107_the_cat_in_the_hat
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43
*
* @author Martin Lambeck
* @version 1.0, 10.08.2010
*
* Method : math
* Status : Accepted
* Runtime: 2.380
*/

import java.util.Scanner;

public class Main
{
static final double EPSILON = 0.000001;
static int tallest, workers, n;
static Scanner sc = new Scanner(System.in);

public static void main(String[] args)
{
while (testcase());
}

public static boolean testcase()
{
tallest = sc.nextInt();
workers = sc.nextInt();

if (tallest == 0 && workers == 0)
return false;

calcN();

int lazy = 0;
int stack = tallest;
int h = tallest;
int c = 1;

while (h > 1)
{
lazy += c;

c *= n;
h = (int) (h / (1.0+n) + EPSILON);
stack += h * c;
}

System.out.printf("%d %d%n", lazy, stack);

return true;
}

// find k, n such that:
// tallest = (n+1)^k
// &&
// workers = n^k
//
// k = number of "levels" of cats - 1 = number of cats of different size - 1
// n = number of cats in each hat
public static void calcN()
{
double m1 = -1;
double m2 = -1;

for (int k = 2; k <= 31; k++)
{
m1 = Math.pow(tallest, 1.0 / k); //m1 = k-th root of tallest
//m2 = Math.pow(workers, 1.0 / k);

if (Math.abs(m1 - Math.round(m1)) < EPSILON) //if m1 integral number
{
m2 = Math.pow(workers, 1.0 / k); //m2 = k-th root of workers

if (Math.abs(m1 - m2 - 1) < EPSILON) //both conditions met
{
n = (int) (m2 + EPSILON);
return;
}
}
}

n = tallest-1;
}

}


2.



/**

 * ACM Training, SS09

 * ACM Problem #107 The
Cat in the Hat


 * Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43


 *

 * @author Doina Logofatu

 * @version 1.0, 25/09/2009

 *

 * Methode: Math

 * Status : Accepted

 * Runtime: 0.200

 */



#include <iostream>

#include <vector>



using namespace std;



long int gCD(long int a, long
int b){


  long int r;

  while(b!=0){

    r=a%b;

    a=b;

    b=r;

  }

  return a;

}



long int pow(long int b, long
int e){


  long int res = 1;

  while(e){

    if(e%2) res
*= b;


    e /= 2;

    b *= b;

  }

  return res;

}



long int giveP(long int n, long
int &t){


  long int p, i = 2,
k=0; 


  std::vector<int>
a, b;


  while(n-1 &&
i<=n){


    p=0;

    if(n%i==0){

     
b.push_back(i);


     
while(n%i==0 && n-1){p++; n/=i;}


     
a.push_back(p);


    }

    i++;

  }

  long int g = a[0];

  t = 1;

  for(i=1;
i<(int)a.size(); i++) g = gCD(g, a[i]);


  for(i=0;
i<(int)a.size(); i++) t*=pow(b[i], a[i]/g);   


  return g;

}



int main(){

  long int H, W, p1, p2,
k1, k2, nr, p;


 
while(cin>>H>>W && (H||W)){


    if(1==W)
{   


      
p1 = 0;


   
    nr = H;


   
    while(H>1){p1++; H/=2; nr +=H;}


   
    cout << p1 << " " << nr <<
std::endl;


      


    }

    else

     
if(1==H-W)       


       
cout << "1 " << H+W << std::endl;


     
else{       


       
p1 = giveP(H, k1);


       
p2 = giveP(W, k2);


       
if(p1 && p2 && p1-p2)
{                 


         
p = gCD(p1, p2);


         
if(p1>p){


           
k1=pow(k1, p1/p);


           
p1=p;


         
}


         
if(p2>p){


           
k2=pow(k2, p2/p);


           
p2=p;


         
}       


       
}


       
if(p1-p2 || k1-k2-1 != 0)


         
cout << "Es gibt keine Loesung.\n";


       
else{              


         
nr = (pow(k2, p1)-1)/(k2-1);


         
cout << nr << " ";


         
nr = H*k1-H/pow(k1, p1)*pow(k2, p1+1);   
     


         
cout << nr << std::endl;


       
}


      }

    }

  return 0;

}





3.



/**

 * ACM Training, SS09

 * ACM Problem #107 The
Cat in the Hat


 * Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43


 *

 * @author Doina Logofatu

 * @version 1.0, 25/09/2009

 *

 * Methode: Math

 * Status : Accepted

 * Runtime: 0.184

 */



#include <iostream>

#include <vector>

#include <cmath>



using namespace std;



long int gCD(long int a, long
int b){


  long int r;

  while(b!=0){

    r=a%b;

    a=b;

    b=r;

  }

  return a;

}



long int giveP(long int n, long
int &t){


  long int p, i = 2,
k=0; 


  std::vector<int>
a, b;


  while(n-1 &&
i<=n){


    p=0;

    if(n%i==0){

     
b.push_back(i);


     
while(n%i==0 && n-1){p++; n/=i;}


     
a.push_back(p);


    }

    i++;

  }

  long int g = a[0];

  t = 1;

  for(i=1;
i<(int)a.size(); i++) g = gCD(g, a[i]);


  for(i=0;
i<(int)a.size(); i++) t*=(long int)pow((double)b[i],
(double)(a[i]/g));   


  return g;

}



int main(){

  long int H, W, p1, p2,
k1, k2, nr, p;


 
while(cin>>H>>W && (H||W)){


    if(1==W)
{   


      
p1 = 0;


   
    nr = H;


   
    while(H>1){p1++; H/=2; nr +=H;}


   
    cout << p1 << " " << nr <<
std::endl;


      


    }

    else

     
if(1==H-W)       


       
cout << "1 " << H+W << std::endl;


     
else{       


       
p1 = giveP(H, k1);


       
p2 = giveP(W, k2);


       
if(p1 && p2 && p1-p2)
{                 


         
p = gCD(p1, p2);


         
if(p1>p){


           
k1=(long int)pow((double)k1, (double)(p1/p));


           
p1=p;


         
}


         
if(p2>p){


           
k2=(long int)pow((double)k2, (double)(p2/p));


           
p2=p;


         
}       


       
}


       
if(p1-p2 || k1-k2-1 != 0)


         
cout << "Es gibt keine Loesung.\n";


       
else{              


         
nr = ((long int)pow((double)k2, (double)p1)-1)/(k2-1);


         
cout << nr << " ";


         
nr = H*k1-H/((long int)pow((double)k1, (double)p1))*pow((double)k2,
(double)p1+1);         


         
cout << nr << std::endl;


       
}


      }

    }

  return 0;

}



4.



/**

 * ACM Training, SS09

 * ACM Problem #107 The
Cat in the Hat


 * Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43


 *

 * @author Doina Logofatu

 * @version 1.0, 25/09/2009

 *

 * Methode: Math

 * Status : Accepted

 * Runtime: 0.864

 */



#include <iostream>

#include <cmath>



using namespace std;



long int pow(long int b, long
int e){


  long int res = 1;

  while(e){

    if(e%2) res
*= b;


    e /= 2;

    b *= b;

  }

  return res;

}



int main(){

  long int H, W, p, nr;

 
while(cin>>H>>W && (H||W)){


    if(1==W)
{   


   
    p = 0;


   
    nr = H;


   
    while(H>1){p++; H/=2; nr +=H;}


   
    cout << p << " " << nr <<
std::endl;      


    }

    else

     
if(1==H-W)       


       
cout << "1 " << H+W << std::endl;


     
else{       


             
bool flag=false;


   
          long int p, np=1;


   
          for(long int N=2;
N*N<=W; N++){


   
             
p=(long int)(log((double)W)/log((double)N)+10e-6);


   
             
if(pow(N, p)==W && pow(N+1, p)==H){


   
       
        cout << (pow(N,
p)-1)/(N-1) << " " << H*(N+1)-H/pow(N+1, p)*pow(N, p+1)
<< std::endl;


   
       
        flag=true;


   
              }


   
          }


   
          if(!flag)
cout<<"Es gibt keine Loesung.\n";


   
    }


    }

  return 0;

}



5.



/**

 * ACM Training, SS09

 * ACM Problem #107 The
Cat in the Hat


 * Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43


 *

 * @author Doina Logofatu

 * @version 1.0, 25/09/2009

 *

 * Methode: Math

 * Status : Accepted

 * Runtime: 1.012

 */



#include <iostream>

#include <cmath>



int main(){

  long int H, W, p, nr;

  while(std::cin>>H>>W && (H||W)){

    if(1==W) {   

        p = 0;

        nr = H;

        while(H>1){p++; H/=2; nr +=H;}

        std::cout << p
<< " " << nr <<
std::endl;      

    }

    else

     
if(1==H-W)       

        std::cout << "1 "
<< H+W << std::endl;

      else{   
   

             
bool flag=false;

             
long int p, np=1;

             
for(long int N=2; N*N<=W; N++){

                 
p=(long int)(log((double)W)/log((double)N)+10e-6);

                 
if((long int)pow((double)N, p)==W && (long int)pow((double)N+1,
p)==H){

                   
std::cout << (long int)((pow((double)N, p)-1)/(N-1))

                        
<< " " << H*(N+1)-(long int)(H/pow((double)N+1, p))*(long
int)pow((double)N, p+1)

                        
<< std::endl;

                   
flag=true;

                 
}

             
}

             
if(!flag) std::cout<<"Es gibt keine Loesung.\n";

        }

    }

  return 0;

}



6.



/**

 * ACM Training, SS09

 * ACM Problem #107 The
Cat in the Hat


 * Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=43


 *

 * @author Doina Logofatu

 * @version 1.0, 25/09/2009

 *

 * Methode: Math

 * Status : Accepted

 * Runtime: 1.000

 */



#include <iostream>

#include <cmath>



int main(){

  long int H, W, p;

  while(std::cin>>H>>W && (H||W)){

    if(1==W) {   

    p = (long int)(log((double)H)/log(2.)+10e-5);
    if((long int)pow(2.0, p)==H) {
      std::cout << p << " " << 2*H-1 <<  std::endl;
    } else {
      std::cout<<"Es gibt keine Loesung.\n";
    }
    }
    else
      if(1==H-W)       
        std::cout << "1 " << H+W << std::endl;
      else{       
              bool flag=false;
              long int p, np=1;
              for(long int N=2; N*N<=W; N++){
                  p=(long int)(log((double)W)/log((double)N)+10e-6);
                  if((long int)pow((double)N, p)==W && (long int)pow((double)N+1, p)==H){
                    std::cout << (long int)((pow((double)N, p)-1)/(N-1))
                         << " " << H*(N+1)-(long int)(H/pow((double)N+1, p))*(long int)pow((double)N, p+1)
                         << std::endl;
                    flag=true;
                  }
              }
              if(!flag) std::cout<<"Es gibt keine Loesung.\n";
        }
    }
  return 0;
}