zoukankan      html  css  js  c++  java
  • 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
    0xinfor1inxixi+1xi+1for1in1

    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]
    1T<20
    1n50000
    1m1000000000
     
    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[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
    ",t++,ans);
        }
        return 0;
    }
  • 相关阅读:
    7
    6
    5.1
    5
    C#类库帮助类
    Asp.net 数据库依赖那些事
    C#使用NLog记录日志
    JQuery常用操作实现方式
    常用Sql 标量值函数
    Sql语句查询XML
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5691186.html
Copyright © 2011-2022 走看看