题目链接

自己的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. hifi/ headphone test

    https://www.youtube.com/watch?v=-r0gRjqN0N8 https://www.youtube.com/watch?v=sMh_zvCw6us

  2. tomcat 7 用mod_jk做 负载均衡

    在Win7中使用apache为tomcat做负载均衡,各组件及版本如下: 两个tomcat v 7.0.57 一个apache v 2.2.14 一个mod_jk v 1.2.33(for windo ...

  3. 如何用js获取当前url的参数值

    <script language = javascript> function request(paras){ var url = location.href; var paraStrin ...

  4. HTTP 错误 500.21 - Internal Server ErrorHTTP

    应用程序“DEFAULT WEB SITE/WINDRP_TB/TBFXWS”中的服务器错误Internet Information Services 7.5错误摘要HTTP 错误 500.21 - ...

  5. 使用node的http模块实现爬虫功能,并把爬到的数据存入mongondb

    刚开始使用http中间件做爬虫其实蛮多坑的,最主要的坑就是编码问题,有很多中文网站的采用的gb2313的编码方式,这个在爬到的报文解析就很蛋碎, 因为http中间件对utf-8支持的比较好,所以针对这 ...

  6. javascript数学计算

    ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总 ...

  7. ios开发小技巧之提示音播放与震动

    在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...

  8. redis 资料链接

    redis 资料链接  http://blog.nosqlfan.com/html/3537.html

  9. 同一机器 部署 两个 jboss

    当jboss和oracle在同一机器上时,通常oracle占用8080端口,这时只需要去修改\deploy\jbossweb-tomcat50.sar\server.xml中.当在同一台机器上运行两个 ...

  10. Test a ; vs Test a( ) ;

    一. Test a();   Test a;  //前提声明了Test类 前者声明一个返回值为Test,名为a的函数,后者声明了Test类的一个对象(把Test当成int) struct Test{ ...