Timus Online Judge1009 - - K-based Numbers

4667 단어 dp
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: [email protected] > 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[n][i];
        }
        printf("%lld
"
, ans); } return 0; }

좋은 웹페이지 즐겨찾기