HDU 5185 Equation (linear dp full backpack), hdu5185

Source: Internet
Author: User

HDU 5185 Equation (linear dp full backpack), hdu5185


Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission (s): 64 Accepted Submission (s): 20
Problem DescriptionGorwin is very interested in equations. Nowadays she gets an equation like this
X1 + x2 + x3 + records + 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 Xi Satisfies above condition.
For the answer may be very large, you are expected output the result after it modular M .
InputMulti 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 ≤ MB ≤ 1000000000  
OutputFor each case output shoshould 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
23 1005 100
Sample Output
Case #1: 2Case #2: 3
Source BestCoder Round #32

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5185

Question: according to the formula given by the question, how many different methods can be used to obtain n, and the number of methods is equal to m?

Question Analysis: Because n is relatively large, it is not allowed to directly carry a backpack and time space. Considering the nature of the formula, n is obtained at the maximum, that is, 1 ~ Ma summation, ma * (ma + 1)/2 = n
To simplify the process, we can get ma = (sqrt (8n + 1)-1)/2. The time space complexity is reduced to nsqrt (n ), considering that dp [I] [j] represents the number of types of the first I number to synthesize the number j, the transfer equation is
Dp [I] [j] = dp [I-1] [j-I] + dp [I] [j-I], the number of types of j in the first numeric synthesis is equal to the sum of I and I in the j-I synthesis. 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);    }}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.