• <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

            Sum of Digits

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
            Total Submission(s): 149    Accepted Submission(s): 46


            Problem Description
            Petka thought of a positive integer n and reported to Chapayev the sum of its digits and the sum of its squared digits. Chapayev scratched his head and said: "Well, Petka, I won't find just your number, but I can find the smallest fitting number." Can you do the same?
             

            Input
            The first line contains the number of test cases t (no more than 10000). In each of the following t lines there are numbers s1 and s2 (1 ≤ s1, s2 ≤ 10000) separated by a space. They are the sum of digits and the sum of squared digits of the number n.
             

            Output
            For each test case, output in a separate line the smallest fitting number n, or "No solution" if there is no such number or if it contains more than 100 digits.
             

            Sample Input
            4 9 81 12 9 6 10 7 9
             

            Sample Output
            9 No solution 1122 111112
             

            Source
             

            Recommend
            gaojie
             

            根據output的提示,結果不可能大于100位,我們可以猜出,s1最大就是900,s2最大是8100,所以,完全可以(s1 * s2)的復雜度。
            dp[i][j] = min(dp[i - k][j - k * k] + 1, dp[i][j]),其中i表示和,j表示平方和是,k表示多出最高位是k,dp數組記錄位數,同時開一個d數組記錄可行方案的首位。

            根據貪心思想,讓結果最小,所以先盡量求出最小的位數,之后根據位數構造最小的數。得出最小位數后,可以遞歸求出答案(d數組記錄的是可行方案的首位,所以遞歸很好實現。)

            代碼:

            #include <iostream>
            #include 
            <cstdio>
            #include 
            <cstring>
            #include 
            <cmath>
            using namespace std;

            int dp[901][8101], d[901][8101];
            int T, n, m;

            int main()
            {
                memset(dp, 
            0sizeof(dp));
                
            for (int i = 1; i <= 9++i)
                {
                    dp[i][i 
            * i] = 1;
                    d[i][i 
            * i] = i;
                }
                
            for (int i = 1; i <= 900++i)
                {
                    
            for (int j = i; j <= 8100++j)
                    {
                        
            for (int k = 1; k <= 9++k)
                        {
                            
            if (i < k || j < k * k) break;
                            
            if (dp[i - k][j - k * k] == 0 || dp[i - k][j - k * k] == 100continue;
                            
            if (dp[i][j] == 0 || dp[i - k][j - k * k] + 1 < dp[i][j])
                            {
                                dp[i][j] 
            = dp[i - k][j - k * k] + 1;
                                d[i][j] 
            = k;
                            }
                        }
                    }
                }
                scanf(
            "%d"&T);
                
            while (T--)
                {
                    scanf(
            "%d%d"&n, &m);
                    
            if (n > 900 || m > 8100 || dp[n][m] == 0)
                    {
                        printf(
            "No solution\n");
                        
            continue;
                    }
                    
            while (dp[n][m])
                    {
                        
            int dig = d[n][m];
                        printf(
            "%d", dig);
                        n 
            -= dig;
                        m 
            -= dig * dig;
                    }
                    printf(
            "\n");
                }
                
            return 0;
            }
            posted on 2011-10-15 22:14 LLawliet 閱讀(209) 評論(0)  編輯 收藏 引用 所屬分類: 動態規劃
            欧美一区二区精品久久| 一本久久a久久精品vr综合| 亚洲国产成人久久综合一 | 精品亚洲综合久久中文字幕| 香蕉久久夜色精品国产小说| 热久久视久久精品18| 久久精品国产亚洲一区二区| 日本亚洲色大成网站WWW久久| 老色鬼久久亚洲AV综合| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 久久免费视频一区| 99国产欧美精品久久久蜜芽| 亚洲欧美一级久久精品| 久久亚洲国产精品一区二区| 亚洲综合伊人久久综合| 久久这里有精品视频| 97超级碰碰碰碰久久久久| 色8久久人人97超碰香蕉987| 三级三级久久三级久久| 久久久久久噜噜精品免费直播| 97久久精品无码一区二区天美| 亚洲午夜久久久影院伊人| 99久久免费国产精品特黄| 久久久精品视频免费观看| 亚洲一本综合久久| 久久精品国产只有精品2020| 久久99国产精品久久99| 无码伊人66久久大杳蕉网站谷歌| 久久午夜福利无码1000合集| 热综合一本伊人久久精品| 色8激情欧美成人久久综合电| 成人精品一区二区久久久| 国产精品va久久久久久久| 色综合久久中文综合网| 麻豆精品久久久一区二区| 亚洲国产天堂久久综合网站| 久久亚洲高清观看| 精品综合久久久久久88小说| 久久久噜噜噜久久| 超级碰碰碰碰97久久久久| 亚洲AV无码成人网站久久精品大|