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 << 61;
const double eps=1e-5;
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[110][110];
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=1;i<k;i++)dp[1][i]=1;dp[1][0]=0;int ans=0;for(int i=2;i<=n;i++){for(int j=0;j<k;j++){if(j==0)for(int t=1;t<k;t++)dp[i][j]+=dp[i-1][t];else for(int t=0;t<k;t++)dp[i][j]+=dp[i-1][t];//cout<<dp[2][j]<<endl;
            }}for(int i=0;i<k;i++)ans+=dp[n][i];cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/pk28/p/5845857.html

Timus 1009. K-based Numbers相关推荐

  1. ural 1009 K-based Numbers

     1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, ...

  2. ural 1012K-based Numbers. Version 2 1013. K-based Numbers. Version 3

    1013. K-based Numbers. Version 3 Time Limit: 2.0 second Memory Limit: 64 MB Let's consider K-based n ...

  3. URAL1009 K-based Numbers

    Description Let's consider K-based numbers, containing exactly N digits. We define a number to be va ...

  4. ural 1009. K-based Numbers - dp

    1009. K-based Numbers Let's consider K-based numbers, containing exactlyN digits. We define a number ...

  5. 老BOJ 13 K-based Numbers

    K-based Numbers Accept:141     Submit:314 Time Limit:1000MS     Memory Limit:65536KB Description Let ...

  6. 【Java】 剑指offer(40) 最小的k个数

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入n个整数,找出其中最小的k个数.例如输入4.5.1.6.2.7 ...

  7. 从n返回k个元素的所有组合的算法

    我想编写一个函数,该函数采用字母数组作为参数,并选择多个字母. 假设您提供8个字母的数组,并希望从中选择3个字母. 然后您将获得: 8! / ((8 - 3)! * 3!) = 56 返回由3个字母组 ...

  8. 程序员面试题精选100题(05)-查找最小的k个元素[算法]

    题目:输入n个整数,输出其中最小的k个. 例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4. 分析:这道题最简单的思路莫过于把输入的n个整数排序,这样排在最前面的k个 ...

  9. java 返回第k小的数_java – 给定n和k,返回第k个置换序列

    所以如果我正确地阅读这个问题,你想要找到第k个排列,最好不要使用BigInteger,只要k不够大,不需要一个BigInteger. 如果我们看序列 1 2 3 1 3 2 2 1 3 2 3 1 3 ...

最新文章

  1. Spring Cloud云架构 - SSO单点登录之OAuth2.0登录流程(2)
  2. 模拟一下goldengate中断后,重新同步操作
  3. 构建本地缓存java_Java8简单的本地缓存实现
  4. java sessionstate_在Java Web开发中自定义Session
  5. matlab this指针,C++ this指针(直戳本质)
  6. jmeter压力测试(Linux与Windows)
  7. python 遗传算法_Python实现遗传算法的代码
  8. WebView复制粘贴文本
  9. 两台计算机怎么ping通,怎么ping网络(2个网段电脑怎么ping通)
  10. rpm 打包及其命令
  11. AntD 的 Descriptions 组件实现“ 右对齐效果 ”
  12. php实现邀请好友,新版php获取MSN好友列表,邀请MSN好友
  13. Unity实战之类爬塔功能
  14. uni-app 加入logo.png
  15. 企业级无线渗透之PEAP
  16. 如何一键删除PPT的动画效果?
  17. esrally 如何进行简单的自定义性能测试?
  18. 轮回混音 佛教童音dj (1.0)
  19. stm32中spi可以随便接吗_STM32的SPI模式读写FLASH芯片全面讲解
  20. 《当程序员的那些狗日日子》(五十九)凤凰涅磐

热门文章

  1. 也谈网易“有道难题”在线解谜游戏
  2. 北京大兴计算机学校排名2015,北京大兴区小学排名2016
  3. 2021年保育员(中级)考试题库及保育员(中级)考试资料
  4. 计算机软件著作权在哪登记
  5. 17、控件使用之位变量图标显示
  6. 如何成为一门领域的专家——提高技术
  7. android 打开蓝牙语音拨号功能
  8. 免费资源库_第二篇 文档(1)_数据分析
  9. 部署MySQL数据库到服务器
  10. Caffe SSD编译、训练及测试