Advertisement
Stingaleed

Untitled

May 17th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int mas[901][8101];
  4. int main() {
  5. int s1, s2, t;
  6. int prom;
  7. memset(mas, -1, sizeof(mas));
  8. mas[0][0] = 0;
  9. for (int i = 0; i <= 900; ++i) {
  10. for (int j = 0; j <= 8100; ++j) {
  11. for (int d = 1; d <= 9; ++d) {
  12. if (i >= d && j >= d * d && mas[i - d][j - d * d] != -1) {
  13. prom = 1 + mas[i - d][j - d * d];
  14. if (prom <= 100 && (mas[i][j] == -1 || prom < mas[i][j])) mas[i][j] = prom;
  15. }
  16. }
  17. }
  18. }
  19.  
  20. cin >> t;
  21. while (t--) {
  22. cin >> s1 >> s2;
  23. if (s1 > 900 || s2 > 8100 || mas[s1][s2] == -1) {
  24. cout << "No solution";
  25. } else {
  26. while (s1 != 0 && s2 != 0) {
  27. for (int d = 1; d <= 9; d++) {
  28. if (d <= s1 && d * d <= s2 && mas[s1 - d][s2 - d * d] + 1 == mas[s1][s2]) {
  29. cout << d;
  30. s1 -= d;
  31. s2 -= d * d;
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. cout<<"\n";
  38. }
  39. return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement