ural 1009 K-based Numbers



1009. K-based Numbers

Time limit: 1.0 second

Memory limit: 64 MB

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

Problem Source: USU Championship 1997

Tags: dynamic programming  (

hide tags for unsolved problems
)

DP题。。。不过不怎么清楚,先留个坑。。

#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
typedef long long ll;
#define PI 3.1415926
int mat[150][150];
int vis[150];
int b[150];
int n,l,flag;
int main()
{
    ll dp[50];
    int n,k;
    while(cin>>n>>k)
    {
        dp[0]=k-1;
        dp[1]=k*(k-1);
        for(int i=2;i<=n;i++)
            dp[i]=(k-1)*(dp[i-1]+dp[i-2]);
        cout<<dp[n-1]<<endl;
    }
}

时间: 2024-12-10 05:24:25

ural 1009 K-based Numbers的相关文章

ural 1009. K-based Numbers dp 高精度

点击打开链接 1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn't contain two successive zeros. For example: 1010230 is a

URAL - 1009 - K-based Numbers (简单DP)

1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn't contain two successive zeros. For example: 1010230 is a valid

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:

URAL 1506. Columns of Numbers(模拟啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1506 Every New Russian has to look through long columns of numbers for analyzing market trends and planning his investments. Psychologists assure that the longer is a column of numbers, the more diffi

ural 1009

我们定义一个合法的K进制数为一个不含连续两个零的K进制数. 例如:1010230 是一个合法的7位数.1000198 不是合法的数.0001234 不是7位数,是一个合法的4位数. 给你N,和K,求出N位的K进制数的总数. 2 <= K <= 10; N >= 2; 4 <= N+K <= 18. /* DP,f[i]代表第i位置 可以表示成的数字的种类, f[i]:=(k-1)*(f[i-1]+f[i-2]) //f[0]:=1,f[1]:=k-1 */ #include&

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序将子树