您的位置:首页 > 其它

ural 1013. K-based Numbers. Version 3(动态规划)

2013-09-05 20:44 399 查看
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

inputoutput
2
10

90

题意:

思路:和1009、1012一样;这三个题只是更改了数据范围;1012和本题用到了高精度运算;

import java.util.Scanner;
import java.util.regex.Pattern;
import java.lang.Math;
import java.math.BigInteger;

public class t1 {
public static void main(String[] args) {
Scanner ks=new Scanner(System.in);
BigInteger a,b,c,d,e;
a=new BigInteger("0");
b=new BigInteger("0");
c=new BigInteger("1");
d=new BigInteger("0");
e=new BigInteger("0");
int n,k;
n=ks.nextInt();
k=ks.nextInt();
int i;
for(i=1;i<k;i++)
{
d=d.add(c);//调出进制数减1;
}
b=b.add(d);
for(i=2;i<=n;i++)
{
e=b.multiply(c);//保存变量b的值;
b=b.add(a);//更新b的值;
b=b.multiply(d);//更新b的值
a=e.multiply(c);//跟新a的值;
}
b=a.add(b);
System.out.println(b.toString());
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: