2012-03-05 07:43:41Morris

[UVA][JAVA][BigNumber] 10083 - Division

Problem D: Division

Given t, a, b positive integers not bigger than 2147483647, establish whether (t^a - 1)/(t^b -1) is an integer with less than 100 digits. Each line of input contains t, a, b. For each line of input print the formula followed by its value, or followed by "is not an integer with less than 100 digits", whichever is appropriate.

Sample Input

2 9 3
2 3 2
21 42 7
123 911 1

Output For Sample Input

(2^9-1)/(2^3-1) 73
(2^3-1)/(2^2-1) is not an integer with less than 100 digits.
(21^42-1)/(21^7-1) 18952884496956715554550978627384117011154680106
(123^911-1)/(123^1-1) is not an integer with less than 100 digits.



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

public class Main {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int t, a, b;
while(keyboard.hasNextInt()) {
t = keyboard.nextInt();
a = keyboard.nextInt();
b = keyboard.nextInt();
System.out.printf("(%d^%d-1)/(%d^%d-1) ", t, a, t, b);
if(t == 1) {
System.out.println("is not an integer with less than 100 digits.");
continue;
}
if(a == b) {
System.out.println("1");
continue;
}
if(a%b != 0) {
System.out.println("is not an integer with less than 100 digits.");
continue;
}

if((a-b)*Math.log10(t) > 99) {
System.out.println("is not an integer with less than 100 digits.");
continue;
}
BigInteger X, Y, tmp;
X = BigInteger.valueOf(t);
Y = BigInteger.valueOf(t);
X = X.pow(a);
Y = Y.pow(b);
X = X.subtract(BigInteger.valueOf(1));
Y = Y.subtract(BigInteger.valueOf(1));
if(Y.compareTo(BigInteger.valueOf(0)) == 0) {
System.out.println("is not an integer with less than 100 digits.");
continue;
}
tmp = X.mod(Y);
X = X.divide(Y);
System.out.println(X.toString());
}
}
}