ural 1012. K-based Numbers. Version 2(大数dp)

和1009相同,只是n达到了180位,可以模拟大数加和大数乘,这里用的java中的大数。

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner cin = new Scanner(System.in);
 6         while (cin.hasNext()) {
 7             int n = cin.nextInt();
 8             int k = cin.nextInt();
 9             BigInteger[][] dp = new BigInteger[200][2];
10             dp[1][0] = BigInteger.ZERO;
11             dp[1][1] = BigInteger.valueOf(k - 1);
12             for (int i = 2; i <= n; i++) {
13                 dp[i][0] = dp[i - 1][1];
14                 dp[i][1] = dp[i - 1][0].add(dp[i - 1][1]).multiply(
15                         BigInteger.valueOf(k - 1));
16 
17             }
18 
19             BigInteger ans = dp[n][0].add(dp[n][1]);
20             System.out.println(ans);
21         }
22 
23     }
24 }
View Code

 

posted @ 2014-04-16 19:59  N_ll  阅读(127)  评论(0编辑  收藏  举报