题目传送门

题意:n位数,k进制,求个数
分析:dp[i][j] 表示i位数,当前数字为j的个数;若j==0,不加dp[i-1][0];

代码1:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = 22;
const int INF = 0x3f3f3f3f;
long long dp[MAXN][MAXN]; int main(void) //URAL 1009 K-based Numbers
{
//freopen ("B.in", "r", stdin); int n, k;
while (scanf ("%d%d", &n, &k) == 2)
{
memset (dp, 0, sizeof (dp));
for (int i=1; i<k; ++i) dp[1][i] = 1;
for (int i=2; i<=n; ++i)
{
for (int j=0; j<k; ++j)
{
if (!j)
for (int l=1; l<k; ++l) dp[i][j] += dp[i-1][l];
else
for (int l=0; l<k; ++l) dp[i][j] += dp[i-1][l];
}
} long long ans = 0;
for (int i=0; i<k; ++i) ans += dp[n][i];
printf ("%I64d\n", ans);
} return 0;
}

代码2(空间优化):

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = 22;
const int INF = 0x3f3f3f3f;
long long dp[MAXN]; int main(void) //URAL 1009 K-based Numbers
{
//freopen ("B.in", "r", stdin); int n, k;
while (scanf ("%d%d", &n, &k) == 2)
{
memset (dp, 0, sizeof (dp)); dp[0] = 1; dp[1] = k - 1;
for (int i=2; i<=n; ++i)
{
dp[i] = (dp[i-1] + dp[i-2]) * (k-1);
} printf ("%I64d\n", dp[n]);
} return 0;
}

递推DP URAL 1009 K-based Numbers的更多相关文章

  1. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  2. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  3. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  4. 递推DP URAL 1031 Railway Tickets

    题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...

  5. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  6. 递推DP URAL 1260 Nudnik Photographer

    题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...

  7. 递推DP URAL 1586 Threeprime Numbers

    题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...

  8. 递推DP URAL 1081 Binary Lexicographic Sequence

    题目传送门 题意:问第k个长度为n的01串是什么(不能有相邻的1) 分析:dp[i][0/1] 表示前i个,当前第i个放1或0的方案数,先预处理计算,dp[i][1]只能有dp[i-1][0]转移过来 ...

  9. 递推DP URAL 1225 Flags

    题目传送门 /* 1 r; 2 b; 3 w 2不能在最前面,所以dp[1] = 2; dp[2] = 2: 13 or 31 dp[i] = dp[i-1] + dp[i-2]; 只加1或3时,总数 ...

随机推荐

  1. 存在使i > j || i <= j不成立的数吗?

    存在使i > j || i <= j不成立的数吗? 咋一看有点晕!一个数既不能大于也不能小于等于另一个数?那是什么数?答案是”非数“ 例子如下:‘ if(Double.NaN>Flo ...

  2. sql分页查询语句

    有关分页 SQL 的资料很多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是经过预编译的,执行效率高,也更灵活.先看看单条 SQL ...

  3. python 异步线程简单实现

    import threading def foo(): with open(r'./result.log','wb') as f: f.write('=some logs here ==') t = ...

  4. chrome打开本地文件目录

    chrome地址栏输入: file:///

  5. webservice 协议

    Web   Service使用的是   SOAP   (Simple   Object   Access   Protocol)协议soap协议只是用来封装消息用的.封装后的消息你可以通过各种已有的协 ...

  6. Lowest Common Ancestor

    Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes ...

  7. tcp粘包问题(封包)

    tcp粘包分析     http://blog.csdn.net/zhangxinrun/article/details/6721495 解决TCP网络传输“粘包”问题(经典)       http: ...

  8. 【转】java反射详解

    转自:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html 本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的 ...

  9. Live Archive 3644 X-Plosives 解题报告

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=1 ...

  10. iframe并排横着显示

    由于工作需要,两个iframe需要并排横着显示: 效果如下: