Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int mas[901][8101];
- int main() {
- int s1, s2, t;
- int prom;
- memset(mas, -1, sizeof(mas));
- mas[0][0] = 0;
- for (int i = 0; i <= 900; ++i) {
- for (int j = 0; j <= 8100; ++j) {
- for (int d = 1; d <= 9; ++d) {
- if (i >= d && j >= d * d && mas[i - d][j - d * d] != -1) {
- prom = 1 + mas[i - d][j - d * d];
- if (prom <= 100 && (mas[i][j] == -1 || prom < mas[i][j])) mas[i][j] = prom;
- }
- }
- }
- }
- cin >> t;
- while (t--) {
- cin >> s1 >> s2;
- if (s1 > 900 || s2 > 8100 || mas[s1][s2] == -1) {
- cout << "No solution";
- } else {
- while (s1 != 0 && s2 != 0) {
- for (int d = 1; d <= 9; d++) {
- if (d <= s1 && d * d <= s2 && mas[s1 - d][s2 - d * d] + 1 == mas[s1][s2]) {
- cout << d;
- s1 -= d;
- s2 -= d * d;
- break;
- }
- }
- }
- }
- cout<<"\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement