题目链接

自己的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. VS2013中设置大小写的快捷键

    1.我们在定义头文件时,通常需要定义: #ifndef  _MainMenu_H_#define  _MainMenu_H_ your code... #endif 我们需要将头文件名设置为大写的: ...

  2. bzoj 1295: [SCOI2009]最长距离 暴力+bfs最短路

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 题解: 对每个点暴力跑一遍bfs,看能够到达的最远位置,这里如果有障碍物则距离为1 ...

  3. MSAA

    多重采样抗锯齿(MultiSampling Anti-Aliasing,簡稱MSAA)是一种特殊的超级采样抗锯齿(SSAA).MSAA首先来自于OpenGL.具体是MSAA只对Z缓存(Z-Buffer ...

  4. [设计模式] 10 外观模式 facade

    外观模式应该是用的很多的一种模式,特别是当一个系统很复杂时,系统提供给客户的是一个简单的对外接口,而把里面复杂的结构都封装了起来.客户只需使用这些简单接口就能使用这个系统,而不需要关注内部复杂的结构. ...

  5. <%@page include%>、<%@include%>、<jsp:include>三者之间的本质区别

    <%@page include%>.<%@include%>.<jsp:include>三者之间的本质区别 先从它的几个内置对象说起. application和se ...

  6. XEE介绍

    摘要: XMl Entity Expansion(攻击)某种程度上类似于 XML Entity Expansion,但是它主要试图通过消耗目标程序的服务器环境来进行DOS攻击的.这种攻击基于XML E ...

  7. #define x do{......} while(0)的用处

    比如定义宏,#define FREE1(p) if (p) free (p)然后这样调用:if (expression)FREE1(p);elseprintf(“expression was fals ...

  8. hadoop聚群的安装

    第一部分,安装单机hadoop 1,安装ssh sudo apt-get install ssh 注意:如果执行不了这句,那就先执行:sudo apt-get update 2,安装rsync sud ...

  9. 改写java TreeMap制造方便查询的ip内存库

    http://www.54chen.com/java-ee/chang-java-treemap-store-ip.html

  10. @QueryParam和@PathParam比较

    来源:http://jackyrong.iteye.com/blog/1128364 1 先来看@queryparam Path("/users") public class Us ...