Codeforces Round #247 (Div. 2) C. k-Tree (dp)
自己的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)的更多相关文章
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)
题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...
- Codeforces Round #245 (Div. 1) B. Working out (dp)
题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...
- 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 ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
随机推荐
- 动态模板中 SWIPER 划不动问题
原文: 地址:http://hao.jser.com/archive/8030/ 作者:segmentfault 问题: 动态循环生成swiper-slide类,在swiper-wrapper里生成6 ...
- iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):4 同步编码
本文档描述Video Toolbox实现同步编码的办法. Video Toolbox在头文件描述了编码方式为异步,实际开发中也确实为异步. This function may be called as ...
- iOS - 指定视图的圆角个数-b
平常设置视图的圆角最普遍的就是设置四个角的,方法也就是一句代码解决: view.layer.cornerRadius = 10; 四个圆角 但有时需求会是指定某个,或者特定哪几个角设置圆角,所以我们需 ...
- c语言编程之队列(链表实现)
用链表实现了队列,完成了队列的入队和出队功能. #include"stdio.h" typedef int element; typedef struct Node{ struct ...
- Netty4.x中文教程系列(三) ChannelHandler
Netty4.x中文教程系列(四) ChannelHandler 上一篇文章详细解释了Hello World示例的代码.里面涉及了一些Netty框架的基础. 这篇文章用以解释ChannelHandl ...
- 关于去哪儿网的UI自动化测试脚本(Python实现)
UI自动化测试Qunar机票搜索场景访问Qunar机票首页http://flight.qunar.com,选择“单程”,输入出发.到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索 ...
- CentOS5下配置JDK1.6+TOMCAT6
CentOS5下配置JDK1.6+TOMCAT6 安装环境:centos 5.5+jdk1.6.0_25+tomcat6.0.32 一.下载安装jdk1.6.0_25 mkdir /usr/java ...
- Ubuntu 装JDK
我是按照这篇文章安装jdk的: http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html 先去 Oracle下载Linux下 ...
- 老韩思考:一个卖豆腐的能转行IT吗? 你的卖点在哪里?
前言: 我带过的学生很多,各行各业都有,泰牛程序员招生消息放出去后,还有一个在菜市场上卖豆腐的也看我的视频教程,决定转换IT行业,我想,北大毕业的可以卖猪肉,那么卖豆腐的为什么就不能从事IT行业呢?那 ...
- SpringMVC,MyBatis商品的增删改查
一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...