Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/STACK:65777216")
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <stack>
- #include <map>
- #include <queue>
- #include <string>
- #include <memory.h>
- #include <iterator>
- #define mem(A,X) memset(A,X,sizeof(A))
- #define memo(A) memset(A,0,sizeof(A))
- #define forn(I,B) for (int I=1;I<=(B);I++)
- #define forg(H,V) for (int H=first[V];h;h=next[H])
- #define rep(I,B) for (int I=0;I<(B);I++)
- #define labs(X) (((X)>0)?(X):(-(X)))
- #define ropen(X) freopen(X,"r",stdin)
- #define wopen(X) freopen(X,"w",stdout)
- #define pb push_back
- #define mp make_pair
- #define all(X) (X).begin(),(X).end()
- #define sqr(X) ((X)*(X))
- using namespace std;
- typedef pair <int,int> pii;
- typedef pair <long long,long long> pll;
- typedef double ld;
- typedef long long ll;
- typedef vector<int> vi;
- const int N=11111;
- const int K=111;
- const int INF=111111111;
- const double eps=1e-9;
- const double pi=3.14159265358979;
- int f[K][N],g[K][N],s,q;
- vi ans;
- bool rec(int n,int c,int p){
- bool ok=1;
- ans.clear();
- int tt=n;
- while (n && ok){
- ok=0;
- forn(last,9) if (last<=p && last*last<=c &&f[n-1][p-last] && g[n-1][c-last*last]){
- --n;p-=last;c-=last*last;
- ans.pb(last);
- ok=1;
- }
- if (!ok && tt==100){
- printf("-1\n");
- return 0;
- }
- if (!ok && tt!=100) return ok;
- }
- sort(all(ans));
- rep(i,tt) printf("%d",ans[i]);
- putchar('\n');
- return ok;
- }
- int main(){
- f[0][0]=g[0][0]=1;
- forn(i,100) forn(j,10000) forn(last,9){
- if (last>j) break;
- f[i][j]|=f[i-1][j-last];
- }
- forn(i,100) forn(j,10000) forn(last,9){
- if (last*last>j) break;
- g[i][j]|=g[i-1][j-last*last];
- }
- int kol;
- scanf("%d",&kol);
- while(kol--){
- scanf("%d%d",&s,&q);
- forn(i,100) if (rec(i,q,s)) break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment