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. Elicpse新建userLibrary导入jar包时抛出NotFoundException异常

    最近刚开始学Struts2.0框架.再导入jar包的时候突然心血来潮.为方便区分jar包的功能,于是想在WEB-INF -> lib 目录下想新建一个struts文件夹,以方便分类查看. 像上图 ...

  2. 怎么获取textarea中选中文字

    textarea设置select="saveSelectionText()" //保存选中内容 saveSelectionText: function () { var focus ...

  3. 利用 spring 的 task:scheduled-tasks 执行定期任务

    ref是工作类 method是工作类中要执行的方法 initial-delay是任务第一次被调用前的延时,单位毫秒 fixed-delay是上一个调用完成后再次调用的延时 fixed-rate是上一个 ...

  4. 【Python】Linux crontab定时任务配置方法(详解)

    CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...

  5. bzoj1726 第二短路

    一道严格次短路题,WA了一下午回家才发现bellman_ford中的vis [ o ] = false 写成了 vis [ S ] = false.被自己脑残了一脸.... #include<c ...

  6. [bzoj5321] [Jxoi2017]加法

    Description 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个 ...

  7. [洛谷P4291][HAOI2008]排名系统

    题目大意:三种操作: $+Name\;Socore:$上传最新得分记录,把以前的记录删除. $?Name:$ 查询玩家排名.如果两个玩家的得分相同,则先得到该得分的玩家排在前面. $?Index:$ ...

  8. 【BZOJ3038】上帝造题的七分钟2 线段树

    根据一个数六次√必死,我们可以打标记死了就不管他了,于是有贡献的操作复杂度为O(n*logn*6),然而我们还有由于盲目修改造成的多余代价我们把每次查询的区间分成三部分前全死,中残,后全死,对于中残, ...

  9. 运动目标前景检测之ViBe源代码分析

    一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差.背景减除(GMM.CodeBook. SOBS. SACON. VIBE. W4.多帧平均……).光流(稀 ...

  10. Clevo P950系列拆机

    Clevo P950系列(包括神舟精盾T96/T97/T96C/T96E/T97E,炫龙耀9000,铁头人T800同模具机型)拆机 拆机恢复时间:20181203 12:28-14:58   一.普通 ...