题目链接

自己的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. 微软职位内部推荐-Enterprise Architect - BDE - BJ

    微软近期Open的职位: Enterprise ArchitectCloud, HTML5, Big Data and Mobile are technology trends driving pro ...

  2. python学习小结5:封装、继承、多态

    面向对象程序设计中的类有三大特性: 继承,封装,多态 继承:以普通的类为基础建立专门的类对象 封装:对外部世界隐藏对象的工作细节 多态:可对不同类的对象使用同样的操作 在Python中类的继承定义基本 ...

  3. iOS - 指定视图的圆角个数-b

    平常设置视图的圆角最普遍的就是设置四个角的,方法也就是一句代码解决: view.layer.cornerRadius = 10; 四个圆角 但有时需求会是指定某个,或者特定哪几个角设置圆角,所以我们需 ...

  4. win8 ubuntu

    点进去看到几点注意: 1. 如果Windows是UEFI方式安装的,那Ubuntu必须也用UEFI方式安装 2. 必须用64位的Ubuntu安装文件,32位的不能探测EFI 3. 必须用UEFI的方式 ...

  5. mMathf -》 Unity3d通用脚本

    public class mMathf { /// <summary> /// 辗转 相除法 求 最大公约数 /// a / b = k /// a % b = r /// 原理 gcd( ...

  6. Using command-line Subversion to access project source files

    Help index About source code version control with Software Configuration Management (Subversion) Usi ...

  7. sun.misc.unsafe类的使用

    http://blog.csdn.net/fenglibing/article/details/17138079

  8. 0环境设置 - Statspack设置

    简单说明 Statspack主要用于永久存储performance statistics 信息 只有作为sysdba连接时才能安装Statspack. 然后改目录到#cd $ORACLE_HOME/r ...

  9. java split IP地址要用双斜杠

    示例代码: public void test() { String address = "11.12.13.14:800"; System.out.println(address. ...

  10. [转]Ubuntu 常用快捷键10个

    转自:http://www.linuxeden.com/html/news/20100613/103374.html 1.前一个后一个工作区的切换 如果你经常使用工作区,那你就可以用Ctrl + Al ...