URAL 1658. Sum of Digits(简单dp)
2014-05-20 17:17
435 查看
给你n,m。然后输出100位以内一个数字,每一位的和为n,每一位的平方和为m。
以n,m为dp[i][j]。pre[i][j]记录位置上的数字。
Time limit: 2.0 second
Memory limit: 64 MB
Petka thought of a positive integer n and reported to Chapaev the sum of its digits and the sum of its squared digits. Chapaev 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?
separated by a space. They are the sum of digits and the sum of squared digits of the number n.
以n,m为dp[i][j]。pre[i][j]记录位置上的数字。
1658. Sum of Digits
Time limit: 2.0 secondMemory limit: 64 MB
Petka thought of a positive integer n and reported to Chapaev the sum of its digits and the sum of its squared digits. Chapaev 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 tlines 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 | output |
---|---|
4 9 81 12 9 6 10 7 9 | 9 No solution 1122 111112 |
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> #define eps 1e-8 #define M 1000100 #define LL __int64 ///#define LL long long #define INF 0x3f3f3ff #define PI 3.1415926535898 #define MOD 1000000009 const int maxn = 10100; using namespace std; int dp[910][8110]; int pre[910][8110]; void Pre() { for(int i = 0; i <= 900; i++) for(int j = 0; j <= 8100; j++) dp[i][j] = 101; dp[0][0] = 0; for(int i = 1; i <= 900; i++) { for(int j = 1; j <= 8100; j++) { for(int k = 1; k<=9&&k<=i&&k*k<=j; k++) { if(i-k > j-k*k) break; if(dp[i][j] > dp[i-k][j-k*k]+1) { dp[i][j] = dp[i-k][j-k*k]+1; pre[i][j] = k; } } } } } int main() { Pre(); int n, m; int T; cin >>T; while(T--) { scanf("%d %d",&n, &m); if(n>m || n > 900 || m > 8100 || dp [m] > 100 ) { cout<<"No solution"<<endl; continue; } int t; while(n&&m) { t = pre [m]; printf("%d",t); n -= t; m -= t*t; } cout<<endl; } return 0; }
相关文章推荐
- URAL1658. Sum of Digits(DP)
- URAL 1658. Sum of Digits(DP)
- 【URAL 1658】1658. Sum of Digits(DP)
- URAL 1658 Sum of Digits (DP) #by Plato
- ural 1658. Sum of Digits
- URAL 1203 Scientific Conference 简单dp 难度:0
- URAL1658 Sum of Digits(DP)
- URAL 1225 Flags 简单DP,一重循环
- URAL - 1009 简单DP..新的开始...
- URAL 1577. E-mail(简单二维dp)
- URAL 1326. Bottle Taps(简单的状压dp)
- URAL 1018 二叉苹果树(简单树形dp)
- URAL 1152 False Mirrors(简单的状态压缩dp)
- ural 1009. K-based Numbers(简单dp)
- URAL 1108 简单的树形dp背包问题
- 简单DP Stone Pile Ural_1005
- Ural-1014. Product of Digits(简单题)
- URAL 1119. Metro(简单dp)
- URAL 1081. Binary Lexicographic Sequence(简单dp)
- URAL - 1009 - K-based Numbers (简单DP)