1009. K-based Numbers
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 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
Ndigits.
You may assume that 2 ≤
K ≤ 10;
N ≥ 2;
N +
K ≤ 18.
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 |
Problem Source: USU Championship 1997
Tags:
dynamic programming
)
动态转移方程:f[i]=(k-1)*(f[i-1]+f[i-2]);
i表示第i位,如果第i位为0,则i-1位有k-1种选择即(k-1)*f[i-2],如果
第i位不为0则有(k-1)*f[i-2]
#include<iostream>
using namespace std;
int main()
{
int n,k;
int f[20];
cin>>n>>k;
f[0]=1;
f[1]=k-1;
for(int i=2; i<=n; i++)
f[i]=(k-1)*(f[i-1]+f[i-2]);
cout<<f[n]<<endl;
return 0;
}