-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1658. Sum of Digits.java
101 lines (80 loc) · 1.88 KB
/
1658. Sum of Digits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
public class SumOfDigits {
public static int digits[] = new int[100];
public static void main(String[] args) {
int N = 900;
int M = 8100;
byte Q[][] = new byte[N + 1][M + 1];
byte P[][] = new byte[N + 1][M + 1];
Q[0][0] = 0;
for (int i = 1; i <= N; i++) {
if (i < Byte.MAX_VALUE) {
Q[i][i] = (byte)i;
P[i][i] = 1;
}
else
Q[i][i] = Byte.MAX_VALUE;
}
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= M; j++) {
byte min = Byte.MAX_VALUE;
byte mink = Byte.MAX_VALUE;
for (int k = 9; k > 0; k--) {
int s = i - k;
int d = j - k*k;
if (s > 0 && d > 0 && s <= d) {
if (Q[s][d] < min) {
min = (byte) (Q[s][d] + 1);
mink = (byte)k;
}
}
else if (s== 0 && d == 0) {
min = 1;
mink = (byte)k;
}
}
Q[i][j] = min;
P[i][j] = mink;
}
}
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int testCases = sc.nextInt();
for (int i = 0; i < testCases; i++) {
int s1 = sc.nextInt();
int s2 = sc.nextInt();
if (s1 == 0 & s2 == 0) {
sb.append("No Solution\n");
continue;
}
if (s1 > 900 || s2 > 8100 || s1 > s2) {
sb.append("No solution\n");
continue;
}
else if (Q[s1][s2] > 100) {
sb.append("No solution\n");
continue;
}
byte digits[] = new byte[10];
while (s1 != 0 && s2 != 0) {
byte d = P[s1][s2];
digits[d]++;
s1 -= d;
s2 -= d*d;
}
for (int j = 0; j < 10; j++)
for (int m = 0; m < digits[j]; m++) {
sb.append(j);
}
sb.append("\n");
}
System.out.println(sb);
}
}