题意:求长度为n的k进制数的个数,并且其中不能有存在相邻位置都为0的数
链接:http://acm.timus.ru/problem.aspx?space=1&num=1009
思路:递推/数位dp。转移前一位是否为0的状态的个数
注意点:无
以下为AC代码:
ID | Date | Author | Problem | Language | Judgement result | Test # | Execution time | Memory used |
---|---|---|---|---|---|---|---|---|
6247221 |
15:49:52
14 May 2015 | luminous11 | 1009. K-based Numbers | G++ 4.9 C++11 | Accepted | 0.015 | 238 KB |
/*
***********************************************
*# @Author : Luminous11 (573728051@qq.com)
*# @Date : 2015-05-14 18:27:10
*# @Link : http://blog.csdn.net/luminous11
***********************************************
*/
#include <bits/stdc++.h>
#define clr(a, v) memset( a , v , sizeof(a) )
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1.0);
int main()
{
ios::sync_with_stdio ( false );
long long dp[20][2];
int n, k;
while ( cin >> n >> k ){
dp[1][0] = 1;
dp[1][1] = k - 1;
for ( int i = 2; i < 20; i ++ ){
dp[i][1] = ( k - 1 ) * ( dp[i-1][0] + dp[i-1][1] );
dp[i][0] = dp[i-1][1];
}
cout << dp[n][1] << endl;
}
return 0;
}