B - K-based Numbers
Crawling in process...
Crawling failed
Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Let’s consider K-based numbers, containing exactlyN 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
N digits.
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.
输出
2
10 90
一、题目要看懂。。。考验英语的时候到了。
题意:输入n和k,表示一个n位的k进制数,这个数字要符合两个条件,没有前导0(否则怎么算是n位数呢?),不能有两个或以上连续的0,问你一共有多少个这样的n位的k进制数
二、解题思路
主要是明确 第一个位置 有k-1个选择 即dp【1】=k-1 第二个位置 有(k-1)*k个选择 即dp【2】=(k-1)*k;
到第n个位置时,_ _ n ,当n为0时,n-1!=0,n-2之前的位置为不为0无所谓不用管,所以有(k-1)*dp【n-2】种选择
当n不为0时,第n个位置有k-1种选择,n-1之前的位置无所谓,不用管,共有dp【n-1】*(k-1)种选择
综上共有 (k-1)*(dp【n-1】+dp【n-2】)种选择 所以用个递归就搞定了
ac
#include<iostream>
#include<stdio.h>
using namespace std;
long long dp[21];
int n,k,i;
int main()
{
while(~scanf("%d%d",&n,&k))
{
dp[1]=k-1; dp[2]=(k-1)*k;
for (i=3;i<=n;i++)
dp[i]=(k-1)*(dp[i-1]+dp[i-2]);
printf("%lld\n",dp[n]);
}
return 0;
}
#include<stdio.h>
using namespace std;
long long dp[21];
int n,k,i;
int main()
{
while(~scanf("%d%d",&n,&k))
{
dp[1]=k-1; dp[2]=(k-1)*k;
for (i=3;i<=n;i++)
dp[i]=(k-1)*(dp[i-1]+dp[i-2]);
printf("%lld\n",dp[n]);
}
return 0;
}
参考资料