ural 1013. K-based Numbers. Version 3(动态规划)
1013. K-based Numbers. Version 3
Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn’t contain two successive zeros. For example:
- 1010230 is a valid 7-digit number;
- 1000198 is not a valid number;
- 0001235 is not a 7-digit number, it is a 4-digit number.
Given two numbers N and K, you are to calculate an amount of valid K based numbers, containing N digits.
You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 1800.
Input
The numbers N and K in decimal notation separated by the line break.
Output
The result in decimal notation.
Sample
input | output |
---|---|
2 10 |
90 |
题意:
思路:和1009、1012一样;这三个题只是更改了数据范围;1012和本题用到了高精度运算;
1 import java.util.Scanner; 2 import java.util.regex.Pattern; 3 import java.lang.Math; 4 import java.math.BigInteger; 5 6 7 8 public class t1 { 9 public static void main(String[] args) { 10 Scanner ks=new Scanner(System.in); 11 BigInteger a,b,c,d,e; 12 a=new BigInteger("0"); 13 b=new BigInteger("0"); 14 c=new BigInteger("1"); 15 d=new BigInteger("0"); 16 e=new BigInteger("0"); 17 int n,k; 18 n=ks.nextInt(); 19 k=ks.nextInt(); 20 int i; 21 for(i=1;i<k;i++) 22 { 23 d=d.add(c);//调出进制数减1; 24 } 25 b=b.add(d); 26 for(i=2;i<=n;i++) 27 { 28 e=b.multiply(c);//保存变量b的值; 29 b=b.add(a);//更新b的值; 30 b=b.multiply(d);//更新b的值 31 a=e.multiply(c);//跟新a的值; 32 } 33 b=a.add(b); 34 System.out.println(b.toString()); 35 } 36 }