• Cut the rope


    http://acm.nyist.net/JudgeOnline/problem.php?pid=651

    描述
    We have a rope whose length is L. We will cut the rope into two or more parts, the length of each part must be an integer, and no two parts have the same length.
      Your task is to calculate there exists how many results after the cutting. We say two results are different if and only if there is at least one part, which we can find in one result but can not find in the other result

    输入
    There is an integer T (1 <= T <= 50000) in the first line, which indicates there are T test cases in total.
      For each test case, there is only one integer L (1 <= L <= 50000) which has the same meaning as above.
    输出
    For each test case, you should output the correct answer of the above task in one line.
      Because the answer may be very large, you should just output the remainder of it divided by 1000000 instead
    样例输入
    3
    2
    3
    6
    样例输出
    0
    1
    3

    /*
    1+2+3+4+...+k=(k+1)*k/2
    N = 500000 (长度)
    解得k = 316
    dp[n][k]表示长度为n的线段分解成k段的方案数。
    有2种情况:
    1.有长度为1的:dp[n-k][k-1]
    2.没有长度为1的;每段的长度减1后,与dp[n-k][k]的方案数相同
    
    所以dp[n][k] = (dp[n-k][k-1]+dp[n-k][k])%Mod
    观察dp,dp[n][k]需要的是左上角和上的信息。
    所以递归基础是k=2和n=2的情况。(第一行第一列)
    n=2, dp[2][i] = 0
    k=2, dp[i][2] = (i-1)/2
    
    ans[i]就是对dp[i][*]求和.
    */
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    #define N 50002
    #define K 316
    #define Mod 1000000  
    int m;
    int n;
    int dp[N][K]; 
    int ans[N];
    
    void pre()
    {    
        ans[1] = ans[2] = 0;
        
        for (int i = 0; i < K; ++i)
            dp[2][i] = 0;
        for (int i = 3; i < N; ++i)
            dp[i][2] = (i-1)/2;
        
        for (int i = 3; i < N; ++i)
        {
            for (int j = 3; j < K; ++j)
            {
                if (i-j >= 2)
                    dp[i][j] = (dp[i-j][j-1] + dp[i-j][j])%Mod;
            }
        }
    
        for (int i = 3; i < N; ++i)
            for (int j = 2; j < K; ++j)
                ans[i] = (ans[i]+dp[i][j])%Mod; 
    }
    
    int main()
    {
        pre();
        
        cin >> m;
        
        while (m--)
        {
            cin >> n;
            
            cout << ans[n] << endl;
        }
    }
    View Code
  • 相关阅读:
    2020/11/4
    2020/11/10
    2020/11/12
    2020/11/5
    20201113 千锤百炼软工人
    20201112 千锤百炼软工人
    Java_swing_打开文件目录
    千锤百炼软工周报二
    9.29 课下动手动脑
    开学小测感想
  • 原文地址:https://www.cnblogs.com/chenyg32/p/3147518.html
Copyright © 2020-2023  润新知