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 ≤ 18.
题意问的是在不出现连续0的条件下 k进制的N位数共有多少个
对于第N个数来说 如果之前的数即N-1位为0 则 dp[N] 加上 dp[N-2] 的k-1倍
如果N-1不为0 则加上dp[N-1]的 k-1倍
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll dp[50];
int main()
{
int n , k ;
while(scanf("%d%d",&n,&k)!=EOF)
{
dp[1] = k - 1;
dp[2] = k * dp[1];
for(int i = 3; i <= n; i++)
{
dp[i] = (k-1)*(dp[i-1]+dp[i-2]);
}
printf("%I64d\n",dp[n]);
}
return 0;
}
本文介绍了如何计算在给定的进制和位数条件下,形成的有效数字总数,有效数字定义为不包含连续两个零的数字。通过动态规划的方法,逐步构建解,最终得出所需答案。

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



