您的位置:首页 > 其它

Timus Online Judge1009---K-based Numbers(简单递推dp)

2015-05-14 19:12 375 查看
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

dp[i][j]i位数,第i位j时的合法数目

/*************************************************************************
    > File Name: TOJ1009.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月14日 星期四 18时55分28秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

LL dp[20][20];

int main() {
    int k, n;
    while (~scanf("%d%d", &n, &k)) {
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < k; ++i) {
            dp[1][i] = 1;
        }
        for (int i = 2; i <= n; ++i) {
            for (int j = 1; j < k; ++j) {
                dp[i][0] += dp[i - 1][j];
            }
            for (int j = 1; j < k; ++j) {
                for (int l = 0; l < k; ++l) {
                    dp[i][j] += dp[i - 1][l];
                }
            }
        }
        LL ans = 0;
        for (int i = 1; i < k; ++i) {
            ans += dp
[i];
        }
        printf("%lld\n", ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: