hdu 5185(动态规划)

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

BestCoder Round #32

题意:求解满足条件的 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[320][50005];
int main()
{
    int tcase,t=1;
    scanf("%d",&tcase);
    while(tcase--){
        int n,m;
        scanf("%d%d",&n,&m);
        int k = 0;
        while(k*(k+1)<=2*n) k++;
        memset(dp,0,sizeof(dp));
        dp[0][0] = 1;
        for(int i=1;i<=k;i++){
            for(int j=i;j<=n;j++){
                dp[i][j] = (dp[i][j-i]+dp[i-1][j-i])%m;
            }
        }
        int ans = 0;
        for(int i=1;i<=k;i++){
            ans = (ans+dp[i][n])%m;
        }
        printf("Case #%d: %d\n",t++,ans);
    }
    return 0;
}
时间: 2024-08-28 13:57:34

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

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

这题说的是 x[1]+x[2]+x[3]+…+x[n]=n, 这里 0 <= x[i] <= n && 1 <= i <= n x[i] <= x[i+1] <= x[i]+1 && 1 <= i <= n-1 对于一个给定的n,Gorwin想要知道有多少xi的组合满足上述等式.由于结果比较大,输出答案对m取余的结果就行. n<=50000 经过分析我们知道第一个数必须是 0  或者 1 ,如果是0 那么我们分析,第一个

FatMouse&#39;s Speed hdu 1160(动态规划,最长上升子序列+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:现给出老鼠的体重与速度,要求你找出符合要求的最长子序列.       要求是 W[m[1]] < W[m[2]] < ... < W[m[n]](体重) && S[m[1]] > S[m[2]] > ... > S[m[n]] (速度) 分析:有两个变量的话比较不好控制,自然需要先排序.再仔细思考的话,觉得和之前做的防御导弹有点类似,都是求最多能有几个符合

HDU 5185 Equation (线性dp 完全背包)

Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 64    Accepted Submission(s): 20 Problem Description Gorwin is very interested in equations. Nowadays she gets an equation like this x1+

C - Monkey and Banana HDU 1069( 动态规划+叠放长方体)

C - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1069 Description A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof

hdu 2571 动态规划

简单的动态规划 #include<cstdio> #include<cstring> #define Max(a,b) (a>b?a:b) int dp[21][1001]; int map[21][1001]; int main() { int t; scanf("%d",&t); while(t--) { int m,n; scanf("%d%d",&n,&m); for(int i=1;i<=n;i+

HDU 1003 动态规划

http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int donser[100000]; int d[100000]; int main() { int i,j,k,test,n,m

hdoj-1171-Big Event in HDU【动态规划】

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28852 Accepted Submission(s): 10143 Problem Description Nowadays, we all know that Computer College is the biggest department in H

HDU 1074 动态规划

HDU 1074 慢慢写了半个多小时的dp,思路很清楚,二进制保存状态,然后如果这个状态为0,就添上1转移,并且记录路径,做之前一定要想好要记录哪些东西,由于一个状态不管cost是啥,总之是一样的.因为不管什么状态,加到这一步,总天数都是一样的. #include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <ma

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)Total Submission(s): 57986    Accepted Submission(s): 19484   Problem Description Nowadays, we all know that Computer College is the biggest departme