Advertisement
artemgf

Сумма цифр

Jun 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define _USE_MATH_DEFINES
  4. #include <iostream>
  5. #include <string>
  6. #include <map>
  7. #include <set>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <stdio.h>
  11. #include <cmath>
  12. #include <math.h>
  13. #include <queue>
  14. #include <stack>
  15. #include <climits>
  16. #include <deque>
  17. #include <ctime>
  18. #include <iomanip>
  19. #include <bitset>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef unsigned int ui;
  28.  
  29. #define mh() make_heap()
  30. #define poph() pop_heap()
  31. #define pushh() push_heap()
  32. #define sor(n) n.begin(), n.end()
  33. #define rsor(n) n.rbegin(), n.rend()
  34. #define mp make_pair
  35. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  36. #define p(T) pair<T,T>
  37. #define toch(x) cout.precision(x), cout.setf(ios::fixed)
  38. #define znac(l) abs(l)/l
  39. #define IOS ios::sync_with_stdio(false)
  40. #define IOSB cin.tie(0), cout.tie(0);
  41. const ll ok = ll(1e9 + 7);
  42. char arr[8110][910];
  43. char arr1[8110][910];
  44. int main()
  45. {
  46.     IOSB;
  47.     IOS;
  48. #ifdef TheCompiler
  49.     files;
  50. #endif
  51.     arr[1][1] = '1';
  52.     arr[4][2] = '2';
  53.     arr[9][3] = '3';
  54.     arr[16][4] = '4';
  55.     arr[25][5] = '5';
  56.     arr[36][6] = '6';
  57.     arr[49][7] = '7';
  58.     arr[64][8] = '8';
  59.     arr[81][9] = '9';
  60.     arr1[1][1] = 1;
  61.     arr1[4][2] = 1;
  62.     arr1[9][3] = 1;
  63.     arr1[16][4] = 1;
  64.     arr1[25][5] = 1;
  65.     arr1[36][6] = 1;
  66.     arr1[49][7] = 1;
  67.     arr1[64][8] = 1;
  68.     arr1[81][9] = 1;
  69.     for(int i=1;i<=8100;i++)
  70.         for (int j = 1; j <= 900; j++)
  71.         {
  72.             for (int k = 1; k <= 9; k++)
  73.             {
  74.                 if (arr[i][j] != '\0'&&i + k*k<=8100&&j+k<=900&&arr1[i][j]<100)
  75.                 {
  76.                     if (arr[i + k*k][j + k] != '\0')
  77.                     {
  78.                         if (arr1[i][j] + 1 <= arr1[i + k*k][j + k])
  79.                         {
  80.                             arr[i + k*k][j + k] = k + '0';
  81.                             arr1[i + k*k][j + k]= arr1[i][j] + 1;
  82.                         }
  83.                     }
  84.                     else
  85.                     {
  86.                         arr[i + k*k][j + k] = k + '0';
  87.                         arr1[i + k*k][j + k]= arr1[i][j] + 1;
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.    
  93.     ll n;
  94.     cin >> n;
  95.     for (int i = 1; i <= n; i++)
  96.     {
  97.         ll a, b;
  98.         cin >> a >> b;
  99.         if (a == 0 && b == 0)
  100.         {
  101.             cout << 0 << endl;
  102.         }
  103.         else
  104.             if (b <= 8100 && a <= 900) {
  105.                 if (arr[b][a] != '\0')
  106.                 {
  107.                     if (arr1[b][a] > 100)
  108.                         cout << "No solution" << endl;
  109.                     else
  110.                     {
  111.                         string s = "";
  112.                         while (b != 0 && a != 0)
  113.                         {
  114.                             ll p = arr[b][a] - '0';
  115.                             b -= p*p;
  116.                             a -= p;
  117.                             s += p + '0';
  118.                         }
  119.                         sort(sor(s));
  120.                         cout << s << endl;
  121.                     }
  122.                 }
  123.                 else
  124.                     cout << "No solution" << endl;
  125.             }
  126.             else
  127.                 cout << "No solution" << endl;
  128.     }
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement