1012. K-based Numbers. Version 2
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-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 Ndigits.
You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 180.
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 |
Ural1009的高精度版本
转移方程为f[i]=(k-1)*(f[i-1]+f[i-2])
import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner (System.in);
BigInteger [][]dp = new BigInteger[20][2000];
int i,n,k;
for (i= 2; i <= 10; i ++)
{
dp[i][1] = BigInteger.valueOf(i - 1);
dp[i][2] = BigInteger.valueOf(i * (i - 1));
}
for (i = 2; i <= 10; i ++)
for (k = 3; k <= 1800; k ++)
dp[i][k] = (dp[i][k - 1].add(dp[i][k - 2])).multiply(BigInteger.valueOf(i - 1));
while(cin.hasNext())
{
n = cin.nextInt();
k = cin.nextInt();
System.out.println(dp[k][n]);
}
}
}
9136

被折叠的 条评论
为什么被折叠?



