Magic Multiplication
BaoBao is now learning a new binary operation between two positive integers, represented by , in his magic book. The book tells him that the result of such operation is calculated by concatenating all multiple results of each digit in the two integers.
Formally speaking, let the first integer be , where indicates the -th digit in , and the second integer be , where indicates the -th digit in . We have
Note that the result of is considered to be a \textbf{string} (without leading zeros if , or contains exactly one `0' if ), NOT a normal integer. Also, the sum here means \textbf{string concatenation}, NOT the normal addition operation.
For example, 23 45 = 8101215. Because , , and .
BaoBao is very smart and soon knows how to do the inverse operation of . Now he gives you the result of a operation and the numbers of digits in the two original integers. Please help him to restore the two original integers and .
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two positive integers and (), where indicates the length of and indicates the length of . Here length of an integer means the length of the string when writing the number in decimal notation without leading zeros.
The second line contains only one positive integer without leading zeros, indicating the result of . The length of is no more than .
It's guaranteed that the sum of lengths of over all test cases will not exceed .
Output
For each test case output one line.
If there exist such and that , output one line containing two integers and separated by one space. Note that and should be positive integers without leading zeros, the length of should be exactly , and the length of should be exactly .
If there are multiple valid answers, output the answer with the smallest ; If there are still more than one answer, output one of them with the smallest .
If such and do not exist, print "Impossible" (without quotes) on a single line.
Sample Input
4 2 2 8101215 3 4 100000001000 2 2 80101215 3 4 1000000010000
Sample Output
23 45 101 1000 Impossible Impossible
模拟
#include <iostream>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stdio.h>
//#include<bits/stdc++.h>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f3f
#define maxn 200005
#define mod 1000000007
using namespace std;
char s[maxn];
int n,m,len;
int a[maxn],b[maxn];
bool judge(int x){
mem(a,0);mem(b,0);
a[0]=x;int ax=1,bx=0,flag=0,i=0;
for(;i<len;i++){
int y=(s[i]-'0');
if(y%x==0)b[bx++]=y/x;
else{
i++;y=y*10+(s[i]-'0');
if(y%x==0&&y/x<10){b[bx++]=y/x;}
else
return false;
}
if(bx==m){break;}
}
if(bx<m)return false;
i++;
while(ax<n){
int ans=0,ans1=0;
if(i>=len)return false;
int y=(s[i]-'0');
if(y==0||y%b[0]==0){if(y==0)ans=0;else ans=y/b[0];}
else{
i++;if(i>=len)return false;
y=y*10+(s[i]-'0');
if(y==0||(y%b[0]==0&&y/b[0]<10)){if(y==0)ans=0;else ans=y/b[0];}
else
return false;
}
i++;
for(int j=1;j<bx;j++){
if(i>=len)return false;
int y=(s[i]-'0');
if(b[j]==0||y==0||y%b[j]==0){
if(b[j]==0&&y==0)ans1=ans;
else if(b[j]!=0&&y==0)ans1=0;
else if(b[j]==0&&y!=0)return false;
else
ans1=y/b[j];
}
else{
i++;if(i>=len)return false;
y=y*10+(s[i]-'0');
if(b[j]==0||y==0||(y%b[j]==0&&y/b[j]<10)){
if(b[j]==0&&y==0)ans1=ans;
else if(b[j]!=0&&y==0)ans1=0;
else if(b[j]==0&&y!=0)return false;
else
ans1=y/b[j];
}
else
return false;
}
i++;
if(ans1!=ans)return false;
}
a[ax++]=ans;
}
if(i<len||ax<n)return false;
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
mem(s,'\0');
scanf("%d%d",&n,&m);
scanf("%s",s);
len=strlen(s);
int flag=0;
for(int i=1;i<=9;i++){
if(judge(i)){
flag=1;break;
}
}
if(flag==1){
for(int i=0;i<n;i++)printf("%d",a[i]);
printf(" ");
for(int i=0;i<m;i++)printf("%d",b[i]);
printf("\n");
}
else
puts("Impossible");
}
}