UVa 10254 - The Priest Mathematician

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

Java 大數~
找遞迴式建表

作法參考:
http://blog.csdn.net/shuangde800/article/details/9705447
http://mobcs.blogspot.tw/2015/05/uva-10254-priest-mathematician.html

Code

UVa 10254
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner;
import java.math.BigInteger;

public class Main {

public static void main(String[] args) {

Scanner cin = new Scanner(System.in);

BigInteger[] ans = new BigInteger[10001];
BigInteger exp = new BigInteger("1");

ans[0] = BigInteger.ZERO;
int count = 1;
for (int i = 1; i <= 10000;) {

for (int j = count; i <= 10000 && j > 0; i++, j--)
ans[i] = ans[i - 1].add(exp);

count++;
exp = exp.shiftLeft(1);
}

while (cin.hasNext()) {
System.out.println(ans[cin.nextInt()].toString());
}
}

}