QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#91042 | #6129. Magic Multiplication | yyyyxh | AC ✓ | 24ms | 3660kb | C++17 | 1.2kb | 2023-03-26 20:22:24 | 2023-03-26 20:22:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
string s;
int n,m,len;
pair<string,string> ans;
void decode(int x){
string a="",b="";
a+=x^48;
int pos=0;
for(int i=0;i<len;++i){
int c=s[i]^48;
if(c%x){
if(i==len-1) return;
c=(c<<1)+(c<<3)+(s[++i]^48);
}
if(c%x) return;
if(c/x>9) return;
b+=(c/x)^48;
if(int(b.length())==m){pos=i;break;}
}
if(int(b.length())<m) return;
int p=0;
bool all0=1;
for(int i=pos+1;i<len;++i){
int c=s[i]^48;
int bp=b[p]^48;
if(bp){
if(c%bp){
if(i==len-1) return;
c=(c<<1)+(c<<3)+(s[++i]^48);
}
if(c%bp) return;
if(c/bp>9) return;
if(!p||all0) a+=(c/bp)^48;
else{if(a.back()!=((c/bp)^48)) return;}
all0=0;
}
else if(c) return;
++p;
if(p==m){
if(all0) a+=48;
p=0;all0=1;
}
if(int(a.length())>n) return;
}
if(int(a.length())<n) return;
ans=min(ans,make_pair(a,b));
}
int main(){
ios::sync_with_stdio(0);
int tc;
cin>>tc;
while(tc--){
cin>>n>>m>>s;
ans=make_pair("~","~");len=s.length();
for(int i=1;i<=9;++i) decode(i);
if(ans.first=="~") cout<<"Impossible"<<endl;
else cout<<ans.first<<' '<<ans.second<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3244kb
input:
4 2 2 8101215 3 4 100000001000 2 2 80101215 3 4 1000000010000
output:
23 45 101 1000 Impossible Impossible
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 24ms
memory: 3660kb
input:
1025 11 18 1461416814188088414188241035153540203545200202010354520510254921495628496328028281449632871435351535402035452002020103545205102500000000000000000000000000004000000063276372366381360363618638136918454921495628496328028281449632871435492149562849632802828144963287143514614168141880884141882...
output:
Impossible 3583 5 161650357972 65354104569 597523997017 7693 Impossible 406723924695110 973937089831524 59331138450754 554 4 189401911962950 980565699171 84748728972992 Impossible 62155650672 4241405 9458752764004792353 8717596993614 Impossible 941952596 49242258343771276739 Impossible 64053045751 4...
result:
ok 1025 lines