URAL1658 Sum of Digits(DP)

来源:互联网 发布:人工智能东南大学 编辑:程序博客网 时间:2024/06/05 18:05

题意:有一个不多于100位的数字,满足各位之和为S1,各位平方之和为S2,求满足该条件的最小数字

思路:首先要理解,S1<=900,为什么呢?假设每一位的数字都取9,那么100位之和就为900,不能再多了,同理,S2<=8100。然后就可以将这个问题泛化为一个二维完全背包问题,有1-9种物品可以任意选,最后的价值是它们的和,体积是它们的平方和,那么DP[i][j]可以表示为要取i价值,j体积最少要取多少件物品。DP方程见代码


#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 100000#define LL long longint cas=1,T;#define INF 1000000int dp[901][8110];int a[10];int num[910];int pos;void Print(int s1,int s2){if (!s1 && !s2)return;for (int i = 1;i<=9;i++){if (dp[s1-i][s2-a[i]] == dp[s1][s2]-1){num[pos++] =i;Print(s1-i,s2-a[i]);return;}}}int main(){for (int i = 1;i<=9;i++)a[i]=i*i;for (int i = 0;i<=900;i++)for (int j = 0;j<=8100;j++)dp[i][j]=1000000;dp[0][0]=0;for (int i = 1;i<=9;i++)for (int j = i;j<=900;j++)for (int k = a[i];k<=8100;k++)dp[j][k] = min(dp[j][k],dp[j-i][k-a[i]]+1);//freopen("in","r",stdin);scanf("%d",&T);while (T--)    {int s1,s2;scanf("%d%d",&s1,&s2);if (s1 > s2 || s1>900 || s2 > 8100)printf("No solution\n");else{if (dp[s1][s2]>100)printf("No solution\n");else{pos = 0;Print(s1,s2);                for (int i = 0;i<pos;i++)printf("%d",num[i]);printf("\n");}}}//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 0;}

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

49 8112 96 107 9
 

Sample Output

9No solution1122111112
 


0 0
原创粉丝点击