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
  • 相关阅读:
    友盟页面统计
    为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?
    让jQuery的ajaxFileUpload插件支持onchange事件
    sublime双击选择全选带中划线
    配置Chrome Workspace功能
    font-face 跨域解决
    Yahoo团队经验:网站性能优化的34条黄金法则
    require.js+knockout.js+.underscore模板引擎的使用
    使用livereload实现自动刷新
    WebStorm 7.0 支持更多的Web技术
  • 原文地址:https://www.cnblogs.com/chenyg32/p/3147518.html
Copyright © 2011-2022 走看看