题目链接

自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单。

题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为d 的路径有多少条。

思路:d[i][l][sum] 表示第i 行最大值为l, 总和为sum的路径数。

注意:我的代码中 sum + j 有可能会超过数组最大值,即越界,刚开始我以为不会产生影响,后来

发现不知道为什么 在数组里越界以后比如 越界到d[][][111] 会对 d[][][1]产生影响,还是要注意写代码 不要让数据越界吧。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int mo = + ;
int dp[][][]; int main()
{
int n, k, d, i, j, l, sum, ans;
while(cin>>n>>k>>d)
{
memset(dp, , sizeof(dp));
for(i = ; i <= k; i++)
dp[][i][i] = ;
for(i = ; i < n; i++)
{
for(l = ; l <= k; l++)
for(sum = ; sum <= ; sum++)
{
if(dp[i-][l][sum] == )
continue;
for(j = ; j <= k; j++)
{
if(sum + j > n) //注意,不然数组会越界导致结果错误
break;
if(j > l)
{
dp[i][j][sum+j] += dp[i-][l][sum];
dp[i][j][sum+j] %= mo;
}
else
{
dp[i][l][sum+j] += dp[i-][l][sum];
dp[i][l][sum+j] %= mo; }
}
}
}
ans = ;
for(j = ; j < n; j++)
for(i = d; i <= ; i++)
{
ans += dp[j][i][n];
ans %= mo;
}
printf("%d\n", ans);
}
return ;
}

贴一下我找我的越界错误的代码,

做完题后 我又找了好长时间错误。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int mo = + ;
int dp[][][]; int main()
{
int n, k, d, i, j, l, sum, ans, flag;
while(cin>>n>>k>>d)
{
flag = ;
memset(dp, , sizeof(dp));
for(i = ; i <= k; i++)
dp[][i][i] = ;
for(i = ; i < n; i++)
{
for(l = ; l <= k; l++)
{
for(sum = ; sum <= ; sum++)
{
if(dp[i-][l][sum] == )
continue;
for(j = ; j <= k; j++)
{
/*if(sum + j > n)
break;*/
if(j > l)
{
printf("%d\n", dp[][][]);
dp[i][j][sum+j] += dp[i-][l][sum];
dp[i][j][sum+j] %= mo; if(dp[][][])
{
printf("%d %d %d\n", i, j, sum+j);
printf("%d\n", dp[i][j][sum+j]);
printf("%d %d %d\n", dp[][][], dp[][][], dp[][][]);
flag = ;
break;
}
}
else
{
dp[i][l][sum+j] += dp[i-][l][sum]; dp[i][l][sum+j] %= mo; } if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
ans = ;
for(j = ; j < n; j++)
for(i = d; i <= ; i++)
{
ans += dp[j][i][n];
ans %= mo;
}
// printf("%d\n", ans);
}
return ;
}

Codeforces Round #247 (Div. 2) C. k-Tree (dp)的更多相关文章

  1. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  2. Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...

  3. Codeforces Round #369 (Div. 2) C. Coloring Trees (DP)

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)

    C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)

    题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...

  6. Codeforces Round #245 (Div. 1) B. Working out (dp)

    题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...

  7. Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)

    题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...

  8. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  9. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  10. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

随机推荐

  1. VBS基础篇 - 条件语句

    经常地,当我们编写代码时,我们需要根据不同的判断执行不同操作,我们可以使用条件语句完成这个工作. If...Then...Else 在下面的情况中,您可以使用 If...Then...Else 语句: ...

  2. FMS (端口问题)如何穿透防火墙

    转自http://www.cnblogs.com/zhchongyao/archive/2010/01/22/1653803.html 先是管理端口,就是fms2_console文件连接到server ...

  3. bzoj 4010: [HNOI2015]菜肴制作 拓扑排序

    题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...

  4. 【转】2-SAT题集

    转自:http://blog.csdn.net/shahdza/article/details/7779369 [HDU]3062 Party1824 Let's go home3622 Bomb G ...

  5. Matlab实现二进制矩阵转换为十进制

    一.问题描述 [1 1 1 0 1 0 1 1 0 1 0 0 1 1 0] 每两位3转换为一个十进制数,共5列,那么转换后是ceil(5/3)=2列. [7 1 6 1 1 2] 二.问题分析 1. ...

  6. [设计模式] 7 桥接模式 bridge

    #include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...

  7. 《head first java 》读书笔记(二)

    Updated 2014/03/27 P402-P454 Updated 2014/04/03 P454- 世界三大首席管理器: border, flow, box borderLayout: 五个区 ...

  8. nginx去掉单个目录和多个目录PHP执行权限方法

    我们经常希望某些目录不能执行php代码,如果是nginx的话,我们怎么设置Nginx对于某些目录禁止执行PHP权限呢.以前不知道,其实nginx去掉单个目录和多个目录PHP执行权限方法也很简单. 首先 ...

  9. oracle 表空间 用户

    Oracle创建表空间.创建用户以及授权.查看权限 创建临时表空间 CREATE TEMPORARY TABLESPACE test_temp TEMPFILE 'C:\oracle\product\ ...

  10. 【C++基础】 类中static private public protected

    静态成员在一个类的所有实例间共享数据 “类属性”,是描述类的所有对象共同特征的一个数据项,对所有对象,它的值相同,static定义,为整个类所共有.相对于“实例属性” 如果static成员是私有类型, ...