1009. K-based Numbers
Time limit: 0.5 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
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.
Sample
input | output |
---|---|
2 10 | 90 |
Problem Source: USU Championship 1997
Difficulty: 96
Printable version
Submit solution
Discussion (90)
All submissions (44849) All accepted submissions (18084) Solutions rating (13434)
All submissions (44849) All accepted submissions (18084) Solutions rating (13434)
题目:k进制数,判断是否合法,不能有前导0,并且不能有两个连续的0;
代码:
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int dp[18][2], n, k;
/*
dp[i][1]表示第i个位置为1;dp[i][1] = (dp[i - 1][0] + dp[i - 1][1])*(k - 1);
dp[i][0]表示第i个位置为0;dp[i][0] = dp[i - 1][1];
注意初始化就好了,不能有前导0;
*/
void init()
{
memset(dp, 0, sizeof(dp));
dp[1][0] = 0;
dp[1][1] = (k - 1);
for(int i = 2; i <= n+1; i++)
{
dp[i][1] = (dp[i - 1][0] + dp[i - 1][1])*(k - 1);
dp[i][0] = dp[i - 1][1];
}
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d",&n,&k))
{
init();
printf("%d\n", dp[n][0]+dp[n][1]);
}
return 0;
}