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+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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5185

题目大意:问按照题目所给的公式,有多少种不同的方法得到n,方法数对m取余

题目分析:因为n比较大,直接背包,时间空间都不允许,考虑公式性质,最大的情况下获得n,即1~ma求和,ma * (ma + 1) / 2 == n

化简可以得到ma = (sqrt(8n + 1) - 1) / 2,时间空间复杂度均化为nsqrt(n),考虑dp[i][j]表示前i个数字合成数字j的种类数,则转移方程为

dp[i][j] = dp[i - 1][j - i] + dp[i][j - i],前i个数字合成j的种类数等于合成j-i时放了i和没放i两种情况的和,dp[0][0] = 1

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int dp[317][50001];

int main()
{
    int T, n, m;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        dp[0][0] = 1;
        scanf("%d %d", &n, &m);
        int ans = 0, ma = (sqrt(8 * n + 1) - 1) / 2;
        for(int j = 1; j <= n; j++)
            for(int i = 1; i <= min(j, ma); i++)
                dp[i][j] = (dp[i][j - i] + dp[i - 1][j - i]) % m;
        for(int i = 1; i <= ma; i++)
            ans = (ans + dp[i][n]) % m;
        printf("Case #%d: %d\n", ca, ans);
    }
}
时间: 2024-10-03 11:24:06

HDU 5185 Equation (线性dp 完全背包)的相关文章

HDU 4293 Groups (线性dp)

OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多正确的个数,则dp[ n ] 为要求的.num[ i ][ j ]  保存说前面有i个人 , 后面有j个人的人数,显然num[ i ][ j ]不超过n - i - j; 转移方程dp[ i ] = max(dp[ i ] , dp[ j ]  + num[ j ][ n - i ])  ,详解见代

hdu 5185 Equation(分析+DP)

题意: Gorwin is very interested in equations. Nowadays she gets an equation like thisx1+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 answ

HDU 5185 Equation (DP)

题目:LINK 题意:求满足题目要求的x序列的种类数. 能够发现符合条件的序列去重后是一个0, 1, ..., k的连续序列(k满足k*(k+1)/2 <= n) ,则这个去重后的序列长度最长为sqrt(n)规模大小. 能够DP.dp[i][j]表示用到1~i的连续数字当前和为j的方法数.不用考虑长度是否满足n个,由于前面能够用0补上去. dp[i][j] = dp[i][j-i] + dp[i-1][j-i]; ans = sum(dp[i][n]) for i in range(1, k)

hdu 1421 搬寝室 dp 类似背包

//这道题的意思很明显呐,状态想出来了 //dp[i][j]表示在前i件物品中挑选j对物品的最小皮料值 //但是想着01背包的样子写,结果wa啦 //然后思索了很久之后,发现自己就状态想出来了,转移方程一塌糊涂 //dp[i][j]表示在前i件物品中挑选j对物品的最小皮料值 //最重要的是要进行排序,升序排列或者降序排列,这样连续取俩相 //邻的所得到的差的平方一定是最小的,因为差值最小 //分两种情况讨论: //1)如果当前的i件物品正好可以凑成j对 //dp[i][j] = dp[i-2][

ACM学习历程—HDU 1059 Dividing(dp &amp;&amp; 多重背包)

Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just spl

HDU 2602 Bone Collector DP(01背包)

Bone Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , c

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

HDU 5074 Hatsune Miku (线性dp)

Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 471 Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this