老BOJ 13 K-based Numbers

来源:互联网 发布:mac做ppt能在windows 编辑:程序博客网 时间:2024/05/02 02:09
K-based Numbers
Accept:141    Submit:314Time Limit:1000MS    Memory Limit:65536KB

Description

Let’s considerK-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-digitnumber;

  • 1000198 is not a validnumber;

  • 0001235 is not a 7-digitnumber, it is a 4-digit number.

Given two numbersNandK,you are to calculate an amount of validK based numbers, containingN digits.

You may assume that 2 ≤K≤ 10;N≥ 2;N+K≤ 18.

Input

The numbersN andK in decimal notation separated by the line break.

Output

The result in decimal notation.

SampleInput

2

10

SampleOutput

90

1 N代表数字的位数, K代表是以什么为基的,如10进制,2进制的10和22 判断数值是否合法:开头不能为零, 而不能有两个连续的0设f(n)表示符合题目条件的n位K进制的数的总数。则有:f(n)=(k-1)*(f(n-1)+f(n-2)),且f(1)=k-1,f(2)=k*(k-1)。当n==1时,直接输出k,进行特判!

#include <stdio.h>int main(){    int f[20];    int n,k;    scanf("%d%d",&n,&k);    if(n==1) {        printf("%d\n",k);        return 0;    }    f[1]=k-1;    f[2]=k*(k-1);    for(int i=3; i<=n; i++)        f[i]=(f[i-1]+f[i-2])*(k-1);    printf("%d\n",f[n]);    return 0;}


0 0
原创粉丝点击