URAL1009——DP——K-based Numbers

Description

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

input output
2
10
90

大意:和flag那道一样,如果前面是1的话,当前状态是由dp[n-1]转移过来,若果前面是0的话说明前面的前面是1当前的状态是由dp[n-2]转移过来的(0是肯定要的),对于每一种情况都有(k-1)种方法,一开始考虑0的状态想错了。。WA了几发

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    long long dp[200];
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        dp[1] = k - 1;
        dp[2] = k*(k-1);
        for(int i = 3; i <= n ; i ++)
            dp[i] = (k-1)*(dp[i-1] + dp[i-2]);
        printf("%lld\n",dp[n]);
    }
    return 0;
}

  

时间: 2024-08-10 01:44:45

URAL1009——DP——K-based Numbers的相关文章

61. 从1到n,共有n个数字,每个数字只出现一次。从中随机拿走一个数字x,请给出最快的方法,找到这个数字。如果随机拿走k(k&gt;=2)个数字呢?[find k missing numbers from 1 to n]

[本文链接] http://www.cnblogs.com/hellogiser/p/find-k-missing-numbers-from-1-to-n.html  [题目] 从1到n,共有n个数字(无序排列),每个数字只出现一次.现在随机拿走一个数字x,请给出最快的方法,找到这个数字.要求时间复杂度为O(n),空间复杂度为O(1).如果随机拿走k(k>=2)个数字呢? [分析] 题目给出的条件很强,数字是从1~n的数字,限制了数字的范围:每个数字只出现一次,限制了数字出现的次数:随即拿走了一

K Closest Numbers In Sorted Array

Given a target number, a non-negative integer k and an integer array A sorted in ascending order, find the k closest numbers to target in A, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending ord

Top k Largest Numbers

Given an integer array, find the top k largest numbers in it. Example Given [3,10,1000,-99,4,100] and k = 3.Return [1000, 100, 10]. 思路:由于需要按从大到小的顺序,因此直接用PriorityQueue即可,用Partition的方法的话还需要排序.直接用PriorityQueue 写的代码量少. 1 class Solution { 2 /* 3 * @param

1296. Divide Array in Sets of K Consecutive Numbers

Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbersReturn True if its possible otherwise return False. Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output:

DP:Humble Numbers,丑数

描述A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.  Write a program to find and print the nth elemen

[暑假集训--数位dp]LightOj1205 Palindromic Numbers

A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j

[暑假集训--数位dp]cf55D Beautiful numbers

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子树上的所有节点的权值的乘积x,及x的因数个数. 2.SEED:将节点u的权值乘以x. 思路: 比赛时少看了因数不大于13这句话,然后本题难度增加数倍,肝了两个小时都没肝出来,对不起队友啊,今天的组队训练赛实力背锅…… 这题一眼线段树,由于是对一棵子树进行处理,因此我们采用常规套路,借助dfs序将子树

Timus Online Judge1009---K-based Numbers(简单递推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