老BOJ 13 K-based Numbers
| Accept:141?? | ??Submit:314 |
| Time 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; }
總結
以上是生活随笔為你收集整理的老BOJ 13 K-based Numbers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 老BOJ 11 Counting
- 下一篇: 老BOJ 16 棋盘分割