Equation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 500    Accepted Submission(s): 168

Problem Description
Gorwin is very interested in equations. Nowadays she gets an equation like this
x1+x2+x3+⋯+xn=n, and here

0≤xi≤nfor1≤i≤nxi≤xi+1≤xi+1for1≤i≤n−1

For a certain n, Gorwin wants to know how many combinations of xi satisfies above condition.
For the answer may be very large, you are expected output the result after it modular m.

 
Input
Multi test cases. The first line of the file is an integer T indicates the number of test cases.
In the next T lines, every line contain two integer n,m.

[Technical Specification]
1≤T<20
1≤n≤50000
1≤m≤1000000000

 
Output
For
each case output should occupies one line, the output format is Case
#id: ans, here id is the data number starting from 1, ans is the result
you are expected to output.
See the samples for more details.
 
Sample Input
2
3 100
5 100
 
Sample Output
Case #1: 2
Case #2: 3
 
Source
 
 
题意:求解满足条件的 x1+x2+ ... +xn = n 的种类数。
题解:设计状态为 dp[i][j] 1- i 种数组成 j 的方案数。首先,我们可以确定 xi 的最大值为 1+2...+k = n 里面这个 k ,这样就可以将 dp[i][j]中的 i 确定成 k 了.然后 dp方程是 dp[i][j] = dp[i][j-i]+dp[i-1][j-i] 代表当选择第 i 种数的时候,前面要么选择 i 要么选择 i-1 。。然后最后再求一次和就可以了。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
int dp[][];
int main()
{
int tcase,t=;
scanf("%d",&tcase);
while(tcase--){
int n,m;
scanf("%d%d",&n,&m);
int k = ;
while(k*(k+)<=*n) k++;
memset(dp,,sizeof(dp));
dp[][] = ;
for(int i=;i<=k;i++){
for(int j=i;j<=n;j++){
dp[i][j] = (dp[i][j-i]+dp[i-][j-i])%m;
}
}
int ans = ;
for(int i=;i<=k;i++){
ans = (ans+dp[i][n])%m;
}
printf("Case #%d: %d\n",t++,ans);
}
return ;
}

hdu 5185(动态规划)的更多相关文章

  1. hdu 5185 动态规划 分析降低复杂度

    这题说的是 x[1]+x[2]+x[3]+…+x[n]=n, 这里 0 <= x[i] <= n && 1 <= i <= n x[i] <= x[i+1 ...

  2. hdu 1087 动态规划之最长上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...

  3. HDU 1003 动态规划

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...

  4. hdu 4055 && hdu 4489 动态规划

    hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...

  5. hdu 4745 动态规划

    思路:特水的一个最长回文子序列动态规划.比赛时硬卡第一题,49WA后终于AC,可惜没时间做这题,结果成绩也就可想而知了.兔子跳一样权值的石头,并且一个正跳,一个反跳,这不就是个回文子序列吗?????! ...

  6. hdu 4711 动态规划

    思路:其实这题是个挺水的动态规划,一开始就能AC,可是不知道错哪了,瞎改瞎交,WA了数十次.AC之后怎么改都是AC,也不知道改了什么地方,郁闷死了~~~难道开始时的测试数据有问题??? dp[i][j ...

  7. HDU 6076 (动态规划)

    HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...

  8. HDU 1171 Big Event in HDU (动态规划、01背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. hdu 4719 动态规划

    思路:dp[i]表示到第i个点为结尾能获得的最大值,那么dp[i]=h[i]*h[i]+dp[i-x]-h[i-x];(i-l<=x<=i);那么我们可以转换下,以dp[i]-h[i]为新 ...

随机推荐

  1. lintcode-78-最长公共前缀

    78-最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中, LCP 为 ...

  2. 编译程序提示配置PKG_CONFIG_PATH

    http://blog.csdn.net/langeldep/article/details/6804331 在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接 ...

  3. C - 红与黑

    C - 红与黑 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descri ...

  4. To Chromium之VS调试追踪

    启动的code: for(;;){...WaitForWork()}base.dll!base::MessagePumpForUI::DoRunLoop                         ...

  5. 在Linux下调试Python代码的各种方法

    这是一个我用于调试或分析工具概述,不一定是完整全面,如果你知道更好的工具,请在评论处标记. 日志 是的,的确,不得不强调足够的日志记录对应用程序是多么的重要.您应该记录重要的东西,如果你的记录足够好的 ...

  6. Jira & filter & subscribe & issues

    Jira & filter & subscribe & issues https://confluence.atlassian.com/search/?query=subscr ...

  7. 左右躲避障碍-神手ts版本

    TypeScript-左右躲避障碍-神手 学习typescript,第一步应该是学习官方文档,理解最基础的语法.第二步开始用typescript实现一些js+css 或者canvas类型的游行.现在开 ...

  8. JSP表单提交出现中文乱码的解决方法

    1)post方式 在servlet的doGet( )  doPost( )  中增加以下代码: response.setContentType("text/html;charset=utf- ...

  9. [洛谷P4838]P哥破解密码

    题目大意:求长度为$n$的$01$串中,没有连续至少$3$个$1$的串的个数 题解:令$a_1$为结尾一个$1$的串个数,$a_2$为结尾两个$1$的串的个数,$b$为结尾是$0$的串的个数.$a_1 ...

  10. BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap

    一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...