Timus Online Judge1009---K-based Numbers(简单递推dp)
2015-05-14 19:12
375 查看
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:
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时的合法数目
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: zchao1995@gmail.com > 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 [i]; } printf("%lld\n", ans); } return 0; }
相关文章推荐
- timus 1225 flags 基础DP 简单递推
- URAL 1009. K-based Numbers(简单递推)
- bzoj 2431 简单dp/递推
- HD 2048 数塔 DP(简单递推)
- ural 1009. K-based Numbers(简单dp)
- 简单dp之递推(1)--CF 429B B.Working out
- [递推简单dp]-hdu 2084 数塔
- [递推简单dp]-hdu 2041 超级楼梯
- 递推关系的运用加简单DP【UVA11137Ingenuous Cubrency】-------2015年1月27日
- [递推简单dp]-hdu 2050 折线分割平面
- dp的简单递推笔记1
- 递推DP URAL 1009 K-based Numbers
- URAL - 1009 - K-based Numbers (简单DP)
- poj 1157 简单递推dp
- URAL 1009 K-based numbers(DP递推)
- 简单dp之递推(2)--ZOJ 3747
- HDU Tickets(简单的dp递推)
- [蓝桥杯] 算法训练 未名湖边的烦恼(dp递推或简单dfs)
- hdu 5965(dp递推,简单题)
- POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)