• <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 閱讀(200) 評論(0)  編輯 收藏 引用 所屬分類: 動態規劃
            精品多毛少妇人妻AV免费久久| 久久嫩草影院免费看夜色| 久久精品国产日本波多野结衣| 国产精品乱码久久久久久软件| 久久天天躁夜夜躁狠狠躁2022 | 精品久久人人做人人爽综合| 久久午夜综合久久| 日韩精品久久久久久久电影蜜臀| 久久久青草青青亚洲国产免观| 国产三级观看久久| 日韩精品久久久肉伦网站| 久久久久久一区国产精品| 亚洲va久久久噜噜噜久久| 国产精品99久久久久久www| 亚洲精品乱码久久久久久蜜桃图片 | 久久天天躁狠狠躁夜夜2020老熟妇 | 久久婷婷五月综合国产尤物app| 99久久久精品| 久久久久亚洲AV成人网人人网站| 国产∨亚洲V天堂无码久久久| 久久午夜夜伦鲁鲁片免费无码影视| 国产高潮国产高潮久久久| 久久久久亚洲av成人网人人软件 | 久久综合久久自在自线精品自| 精品久久久无码中文字幕| 久久精品无码一区二区三区| 99精品久久精品一区二区| 亚洲Av无码国产情品久久| 99久久精品国产一区二区三区| 99久久免费国产特黄| 亚洲中文字幕无码久久2020| 性做久久久久久久久浪潮| 久久国产精品一区| 国产A级毛片久久久精品毛片| 2022年国产精品久久久久| 无码人妻精品一区二区三区久久 | 一级做a爰片久久毛片免费陪| 国产综合精品久久亚洲| 精品国产青草久久久久福利| 国内精品久久久久久久coent| 久久精品免费大片国产大片|