24h購物| | PChome| 登入
2012-03-05 07:43:41| 人氣2,324| 回應0 | 上一篇 | 下一篇

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

推薦 0 收藏 0 轉貼0 訂閱站台

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());
}
}
}
 

台長: Morris
人氣(2,324) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][JAVA][Biginteger] 619 - Numerically Speaking
此分類上一篇:[UVA] 12385 - Interesting Sequences

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文