• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 7,comments - 3,trackbacks - 0

            NumberPyramids

            Time Limit: 20 Sec  Memory Limit: 128 MB
            Submissions: 103  Solved: 41

            Description

             

            Suppose that there are N numbers written in a row. A row above this one consists of N-1 numbers, the i-th of which is the sum of the i-th and (i+1)-th elements of the first row. Every next row contains one number less than the previous one and every element is the sum of the two corresponding elements in the row below. The N-th row contains a single number. For example, if the initial numbers are {2,1,2,4}, the whole structure will look like this:

            15
            6 9
            3 3 6
            2 1 2 4

            We shall refer to such a structure as a number pyramid. Two number pyramids are equal if all the numbers at corresponding positions are equal. Given ints baseLength and top, compute the total number of different number pyramids consisting of positive integers, having baseLength elements in the first row and the value at the top equal to top. Since the number of such pyramids might be enormous, return the result modulo 1,000,000,009.

             

            Input

             

            Two numbers -- baseLength and top.
            baseLengthwill be between 2 and 1,000,000, inclusive.
            topwill be between 1 and 1,000,000, inclusive.

             

            Output

             

            The total number of different number pyramids Constraints

             

            Sample Input

            3 5
            5 16
            4 15
            15 31556
            150 500
            

            Sample Output

            2
            1
            24
            74280915
            0
            

            HINT

             

            1) The following are two possible pyramids with 3 numbers in the base and the number 5 at the top:

            2) The only number pyramid with base of size 5 and 16 at the top looks like this:

             

            Source

            Topcoder SRM





            非常V5的一道題,一開是以為但是DP,1,000,000的數(shù)據(jù)范圍真的有點不能接受......
            看了一個大牛的題解,用數(shù)論證明了這個題可以轉(zhuǎn)化成多重背包.....

            思路:
            首先,我們可以證明金字塔最頂端的數(shù)和最低端的數(shù)是有關(guān)系的,關(guān)系就是
            C0N-1*a0 + C1N-1*a1 + C2n-1*a2 + ...... Cn-1n-1*an-1 = T      (1)

            而且因為T <= 1,000,000。可以推出n最大是20.....
            繼續(xù)觀察上述(1),因為必須符合金字塔,所以a序列都至少為1,所以,我們可以發(fā)現(xiàn),先用T減去每個系數(shù)(因為至少一次),之后用那n-1個數(shù)做多重背包,求T的方案就行了。

            復(fù)雜度是(N * 1,000,000),可以接受。

            代碼:
            #include <cstdio>
            #include 
            <cstring>
            #include 
            <iostream>
            using namespace std;

            const int mod = 1000000009;
            int dp[1000100];
            int n, top;
            int c[21][21];
            void init()
            {
                
            for (int i = 1; i < 21++i)
                {
                    c[i][
            0= c[i][i]  = 1;
                    c[i][
            1= c[i][i - 1= i;
                    
            for (int j = 2; j < i - 1++j)
                    {
                        c[i][j] 
            = c[i - 1][j] + c[i - 1][j - 1];
                    }
                }
            }

            int work(int n, int top)
            {
                
            if (n > 20return 0;
                
            if (1 << (n - 1> top) return 0;
                top 
            -= 1 << (n - 1);
                memset(dp, 
            0sizeof(dp));
                dp[
            0= 1;
                
            for (int i = 0; i <= n - 1++i)
                {
                    
            for (int k = 0; k <= top; ++k)
                    {
                        
            if ((dp[k] && k + c[n - 1][i] <= top) || k == 0)
                        {
                            dp[k 
            + c[n - 1][i]] = (dp[k + c[n - 1][i]] + dp[k]) % mod;
                        }
                    }
                }
                
            return dp[top];
            }

            int main()
            {
                memset(c, 
            -1sizeof(c));
                init();
                
            while (scanf("%d%d"&n, &top) != EOF)
                {
                    printf(
            "%d\n", work(n ,top));
                }
                
            return 0;
            }

            posted on 2011-10-15 22:15 LLawliet 閱讀(111) 評論(0)  編輯 收藏 引用 所屬分類: 動態(tài)規(guī)劃
            AV无码久久久久不卡蜜桃| 亚洲欧美精品伊人久久| 一本大道久久香蕉成人网| 免费无码国产欧美久久18| 久久99国产综合精品| 国产成人无码精品久久久久免费| 亚洲AV伊人久久青青草原| 久久精品国产亚洲精品2020| 久久九九久精品国产免费直播| 久久综合偷偷噜噜噜色| 精品久久香蕉国产线看观看亚洲| 久久成人18免费网站| 久久久无码一区二区三区| 亚洲欧美日韩久久精品| 亚洲伊人久久大香线蕉苏妲己| 浪潮AV色综合久久天堂| 久久午夜无码鲁丝片午夜精品| 久久国产精品一区二区| 日韩人妻无码一区二区三区久久 | 精品国际久久久久999波多野| 久久久网中文字幕| 国产99久久久久久免费看| 久久国产精品77777| 久久天天躁狠狠躁夜夜躁2014| 久久久国产一区二区三区| 欧美精品一本久久男人的天堂| 亚洲国产精品高清久久久| 久久久久av无码免费网| 久久久久亚洲精品无码网址| 久久精品成人免费网站| 久久久久久毛片免费播放| 久久久久人妻精品一区| 奇米综合四色77777久久| 亚洲精品蜜桃久久久久久| 久久精品卫校国产小美女| 亚洲精品乱码久久久久久蜜桃图片 | 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 91亚洲国产成人久久精品网址| 亚洲成色999久久网站| 久久这里只有精品首页| 中文字幕亚洲综合久久|