zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    小程序 生成二维码
    uni-app调用wifi接口
    微信小程序代码上传,审核发布小程序
    uni-app开发经验分享十五: uni-app 蓝牙打印功能
    面试题 16.11. 跳水板
    LeetCode 63. 不同路径 II
    LeetCode 44. 通配符匹配
    LeetCode 108. 将有序数组转换为二叉搜索树
    LeetCode 718. 最长重复子数组
    LeetCode 814. 二叉树剪枝
  • 原文地址:https://www.cnblogs.com/chenyg32/p/3147518.html
Copyright © 2011-2022 走看看