1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS09
* Problem: 10722 - Super Lucky Numbers
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1663
*
* @author Kratzer Kevin
* @version 1.0, 11/19/2009
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 1.280
*/

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;

public class MainSuperLucky {
public static BigInteger[][] lbn = new BigInteger[129][102];
public static void main(String... strings) {
Scanner s = new Scanner(System.in);
while(true) {
int givenB = s.nextInt();
int n = s.nextInt();


if(n == 0 && givenB == 0) {
return;
}
BigInteger b = BigInteger.valueOf(givenB);

BigInteger[] ln = lbn[givenB];
if(ln == null) {
ln = new BigInteger[102];
lbn[givenB] = ln;
}
for(int i = 1; i <= n; i++) {
if(ln[i] == null) {
ln[i] = calcLN(ln,i,b);
}
}
if(ln[n] == null) {
System.out.println(0);
} else {
System.out.println(ln[n]);
}
}
}
private static BigInteger calcLN(BigInteger[] ln, int i, BigInteger b) {
if(i > 2) {
return (b.multiply(ln[i-1])).subtract(ln[i-2]);
} else if(i == 1) {
return b.subtract(BigInteger.valueOf(1));
} else if(i == 2) {
return ((b.multiply(b)).subtract(b)).subtract(BigInteger.valueOf(1));
}
return BigInteger.valueOf(-1);
}

}

2.

/* Angewandte Mathematik, SS09, IFB 2C
* ACM Problem #10722 (Super Lucky Numbers)
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1663
*
* @author Dennis Wilfert
* @version 1.0, 05/29/2009
*
* Status : Accepted
* Runtime: 1.248
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.TreeMap;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

// StringBuilder für die Ausgabe
StringBuilder output = new StringBuilder();

StringTokenizer token;
// Basis
int b;
// Anzahl der Stellen
int n;

Main lucky = new Main();

// Cache zum speichern bereits berechneter Werte
lucky.cache = new TreeMap<Integer, BigInteger>();

while(true){

// Die beiden Werte auslesen
token = new StringTokenizer(reader.readLine());
b = Integer.parseInt(token.nextToken());
n = Integer.parseInt(token.nextToken());

// Abberechen wenn beide Werte 0 sind
if(n==0 && b==0)break;

// Cache leeren
lucky.cache.clear();
// Anzahl an die Ausgabe anhängen
output.append(lucky.luckyNumbers(b, n));
output.append("\n");

}
System.out.print(output);
}

// Cache für bereits berechnete Werte
TreeMap<Integer, BigInteger> cache;

/* Anzahl der SuperLuckyNumbers rekursiv berechnen.
*
*/
BigInteger luckyNumbers(int b, int n){

if(n==1){
return BigInteger.valueOf(b-1);
}

if(n==2){
return BigInteger.valueOf(b*b-b-1);
}

// Prüfen ob der Wert bereits berechnet wurde
if(cache.containsKey(n))return cache.get(n);

// Berechnen und im cache speichern
BigInteger result = BigInteger.valueOf(b).multiply(luckyNumbers(b, n-1)).subtract(luckyNumbers(b, n-2));

cache.put(n, result);

return result;
}
}

3.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS09
* Problem: 10722 Super Lucky Numbers
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1663
*
* @author Christoph Hausmann
* @version 0.1, 11/18/2009
*
* Method : DP
* Status : Accepted
* Runtime: 1.264
*/

import java.io.IOException;
import java.math.BigInteger;
import java.util.Scanner;


public class SuperLuckyNumbers_10722 {
public static void main(String... args) throws IOException {

final BigInteger[][] bi = new BigInteger[101][129];

for(int b = 4; b <= 128; b++) {
bi[1][b] = BigInteger.valueOf(b-1);
bi[2][b] = BigInteger.valueOf(b*b-b-1);

for(int n = 3; n <= 100; n++) {
bi[n][b] = bi[n-1][b].multiply(BigInteger.valueOf(b)).subtract(bi[n-2][b]);
}
}

final Scanner scanner = new Scanner(System.in);

while(scanner.hasNextInt()) {
final int base = scanner.nextInt();
final int digits = scanner.nextInt();


if(digits == 0 && base == 0)
break;


final BigInteger res = bi[digits][base];

if(res == null)
System.out.println("0");
else
System.out.println(res);

}
}
}