1009. K-based Numbers

Time limit: 0.5 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 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 Ndigits.
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

dp[i][j]表示 第i位为j的合法数量

那么dp[i][j]=sum(dp[i-1][t])

/* ***********************************************
Author :guanjun
Created Time :2016/9/6 14:29:49
File Name :timus1009.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int n,k;
int dp[][];
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(~scanf("%i %i",&n,&k)){ for(int i=;i<k;i++)dp[][i]=;
dp[][]=;
int ans=; for(int i=;i<=n;i++){
for(int j=;j<k;j++){
if(j==)for(int t=;t<k;t++)dp[i][j]+=dp[i-][t];
else for(int t=;t<k;t++)dp[i][j]+=dp[i-][t];
//cout<<dp[2][j]<<endl;
}
}
for(int i=;i<k;i++)ans+=dp[n][i];
cout<<ans<<endl;
}
return ;
}

Timus 1009. K-based Numbers的更多相关文章

  1. 九章面试题:Find first K frequency numbers 解题报告

    Find first K frequency numbers /* * Input: int[] A = {1, 1, 2, 3, 4, 5, 2}; k = 3 * return the highe ...

  2. K Closest Numbers In Sorted Array

    Given a target number, a non-negative integer k and an integer array A sorted in ascending order, fi ...

  3. 544. Top k Largest Numbers【medium】

    Given an integer array, find the top k largest numbers in it.   Example Given [3,10,1000,-99,4,100] ...

  4. Top k Largest Numbers

    Given an integer array, find the top k largest numbers in it. Example Given [3,10,1000,-99,4,100] an ...

  5. 【leetcode】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 ...

  6. 61. 从1到n,共有n个数字,每个数字只出现一次。从中随机拿走一个数字x,请给出最快的方法,找到这个数字。如果随机拿走k(k>=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个数字 ...

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

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  8. 2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest K - Random Numbers (dfs序 线段树+数论)

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  9. Bubble Cup 13 - Finals [Online Mirror, unrated, Div. 1] K. Lonely Numbers (数学)

    题意:定义两个数\(a,b\)是朋友,如果:\(gcd(a,b)\),\(\frac{a}{gcd(a,b)}\),\(\frac{b}{gcd(a,b)}\)能构成三角形,现在给你一个正整数\(n\ ...

随机推荐

  1. docloud后台管理项目(开篇)

    最近朋友做app需要web做后台管理,所以花了一周时间做了这个项目. 废话不多说,开发环境是nginx+php5.3,使用thinkphp框架.是一个医疗器械数据统计的后台,业务功能很简单就是查看用户 ...

  2. swift VS NSObject

    Any class that does not inherit from another class is known as a base class. Swift classes do not in ...

  3. 安卓app测试之内存监控

    一.通过Dumpsys 来取值 1.adb shell dumpsys meminfo 获取的所有进程的内存信息,以及总内存,剩余内存,使用的内存等信息. 2.想获得某一进程内存的详细信息,在后面加上 ...

  4. 05Servlet example

    dgdfgdfggggggg Servlet 表单数据 在客户端,GET通过URL提交数据,数据在URL中可见:POST把数据放在form的数据体内提交.GET提交的数据最多只有1024字节:POST ...

  5. S-HR之代码创建临时表并插入数据

    ... private String tempTab1 = null; //临时表EcirrWithPPTempTable public String getTempTable() { String ...

  6. 14mysql事务、数据库连接池、Tomcat

    14mysql事务.数据库连接池.Tomcat-2018/07/26 1.mysql事务 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. MySQL手动控制事务 开启事 ...

  7. 牛客练习赛25 C 再编号

    解题思路 我们先来观察一下题目中给出的公式 $$a'_i=(\sum_{j=1}^na_j)-a_i$$ 通过这个公式推一下经过再编号后的序列的总和,因为我们推出这个和之后可以进行下一次计算. $$\ ...

  8. PyQt5Icon图标(Icon)无法显示问题

    PyQt5中设置图标无法显示 以下源码来源PyQt5教程http://zetcode.com/gui/pyqt5/firstprograms/ import sys from PyQt5.QtWidg ...

  9. Shock wave

    ** shock wave thickness of shock wave is order of 1e-7 m why governed by Euler Equation? P334 shock ...

  10. 日期工具类 DateTools

    为了跟其他日期工具类进行区分起名字DateTools public class DateTools { /** The DAT e_ forma t1. */ public static String ...