Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- int N,suma,cuadrado,limite,flag;
- std::vector<int> candidatos;
- bool Dp[901][8101];
- void Calcula(int profundidad,int sumas,int cuadrados)
- {
- if(Dp[sumas][cuadrados])
- return;
- if (profundidad==limite or flag)
- {
- if (sumas==suma and cuadrados==cuadrado)
- flag=1;
- else if(sumas>suma or cuadrados>cuadrado)
- Dp[sumas][cuadrados]=1;
- return;
- }
- for (int i = 1; i <= 9 and sumas+i<=suma and cuadrados+i*i<=cuadrado; ++i)
- {
- candidatos.push_back(i);
- Calcula(profundidad+1,sumas+i,cuadrados+i*i);
- if (flag)
- return;
- candidatos.pop_back();
- }
- Dp[sumas][cuadrados]=1;
- }
- int main()
- {
- scanf("%d",&N);
- for (int i = 0; i < N; ++i)
- {
- limite=suma/9,flag=0;
- scanf("%d %d",&suma,&cuadrado);
- if(suma<=cuadrado)
- {
- while(!flag and limite!=101)
- {
- memset (Dp,0,sizeof(Dp));
- Calcula(0,0,0);
- limite++;
- }
- if(!flag)
- printf("No solution\n");
- else
- {
- for (int j = 0; j < candidatos.size(); ++j)
- printf("%d",candidatos[j]);
- printf("\n");
- }
- }
- else
- printf("No solution\n");
- candidatos.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement