Advertisement
Falexom

Untitled

May 30th, 2022
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include<iostream>
  2. #include<string.h>
  3. #include<stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. #define L 900
  8. #define R 8100
  9. int arr[L][R];
  10. int mas[L][R];
  11.  
  12. int zap(int a[L][R], int n)
  13. {
  14.     for (int i = 0; i < L; i++)
  15.     {
  16.         for (int j = 0; j < R; j++)
  17.         {
  18.             a[i][j] = n;
  19.         }
  20.     }
  21.     return 0;
  22. }
  23.  
  24.  
  25. int istrue(int a, int b)
  26. {
  27.     if (arr[a][b]!=-1)
  28.     {
  29.         return arr[a][b];
  30.     }
  31.     if (a==0 || b==0)
  32.     {
  33.         if (a==0 && b==0)
  34.         {
  35.             arr[a][b] = 0;
  36.         }
  37.         else
  38.         {
  39.             arr[a][b] = 101;
  40.             return arr[a][b];
  41.         }
  42.     }    
  43.     if (a > b)
  44.     {
  45.         arr[a][b] = 101;
  46.         return arr[a][b];
  47.     }
  48.     arr[a][b] = 101;
  49.     return arr[a][b];
  50. }
  51.  
  52. int fun(int a, int b)
  53. {
  54.  
  55.     int c;
  56.     for (int i = 1; i <= 9; ++i)
  57.     {
  58.         if (a-i >= 0)
  59.         {
  60.             if (b - i*i >= 0)
  61.             {
  62.                 c = istrue(a-i, b-i*i);
  63.                 c = fun(a-i, b-i*i);
  64.                
  65.                 if (c + 1 < arr[a][b])
  66.                 {
  67.                     mas[a][b] = i;
  68.                     arr[a][b] = c+1;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     return arr[a][b];
  74. }
  75.  
  76. int check(int a, int b)
  77. {
  78.     if (a > b || a >= b)
  79.     {
  80.         printf("No solution");
  81.         printf("\n");
  82.  
  83.         return 0;
  84.     }
  85.     if (a > L)
  86.     {
  87.  
  88.         printf("No solution");
  89.         printf("\n");
  90.         return 0;
  91.     }
  92.     if (b > R)
  93.     {
  94.  
  95.         printf("No solution");
  96.         printf("\n");
  97.         return 0;
  98.     }
  99. }
  100.  
  101. int main()
  102. {
  103.     zap(arr, -1);
  104.     zap(mas, -1);
  105.     int n;
  106.     scanf("%d",&n);
  107.     if (n > 10000)
  108.     {
  109.         return 0;
  110.     }
  111.     for (int i = 0; i < n; i++)
  112.     {
  113.         int a, b;
  114.         cin >> a >> b;
  115.         int res = 101;
  116.         check(a, b);
  117.         res = fun(a, b);
  118.         if (res > 1)
  119.         {
  120.             while (res > 1)
  121.             {
  122.                 printf("%d",mas[a][b]);
  123.                 a -= mas[a][b];
  124.                 b -= mas[a][b]*mas[a][b];
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement