Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string.h>
- #include<stdio.h>
- using namespace std;
- #define L 900
- #define R 8100
- int arr[L][R];
- int mas[L][R];
- int zap(int a[L][R], int n)
- {
- for (int i = 0; i < L; i++)
- {
- for (int j = 0; j < R; j++)
- {
- a[i][j] = n;
- }
- }
- return 0;
- }
- int istrue(int a, int b)
- {
- if (arr[a][b]!=-1)
- {
- return arr[a][b];
- }
- if (a==0 || b==0)
- {
- if (a==0 && b==0)
- {
- arr[a][b] = 0;
- }
- else
- {
- arr[a][b] = 101;
- return arr[a][b];
- }
- }
- if (a > b)
- {
- arr[a][b] = 101;
- return arr[a][b];
- }
- arr[a][b] = 101;
- return arr[a][b];
- }
- int fun(int a, int b)
- {
- int c;
- for (int i = 1; i <= 9; ++i)
- {
- if (a-i >= 0)
- {
- if (b - i*i >= 0)
- {
- c = istrue(a-i, b-i*i);
- c = fun(a-i, b-i*i);
- if (c + 1 < arr[a][b])
- {
- mas[a][b] = i;
- arr[a][b] = c+1;
- }
- }
- }
- }
- return arr[a][b];
- }
- int check(int a, int b)
- {
- if (a > b || a >= b)
- {
- printf("No solution");
- printf("\n");
- return 0;
- }
- if (a > L)
- {
- printf("No solution");
- printf("\n");
- return 0;
- }
- if (b > R)
- {
- printf("No solution");
- printf("\n");
- return 0;
- }
- }
- int main()
- {
- zap(arr, -1);
- zap(mas, -1);
- int n;
- scanf("%d",&n);
- if (n > 10000)
- {
- return 0;
- }
- for (int i = 0; i < n; i++)
- {
- int a, b;
- cin >> a >> b;
- int res = 101;
- check(a, b);
- res = fun(a, b);
- if (res > 1)
- {
- while (res > 1)
- {
- printf("%d",mas[a][b]);
- a -= mas[a][b];
- b -= mas[a][b]*mas[a][b];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement