QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#555483 | #6129. Magic Multiplication | shstyle | AC ✓ | 190ms | 52716kb | C++20 | 4.3kb | 2024-09-10 00:24:05 | 2024-09-10 00:24:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int N=2e6+10;
const int seed1=131,mod1=1e9+7;
const int seed2=13331,mod2=998244353;
typedef pair<ll,ll> PII;
int w[N];
int cnt[N];
vector<int> pp;
bool tf[N];
int n,m;
int mul[15][15];
int v[N];
int num[N];
char s[N];
int sz;
bool flag=0;
PII hx[N];
PII hsh[N];
ll len[N];
int idx=1;
int cc=0;
PII mi[N];
PII gethash(int l,int r){
// if(l==1){
// cout<<l<<" "<<r<<endl;
// cout<<hsh[r].first<<" "<<hsh[l-1].first<<" "<<mi[r-l+1].first<<endl;
// }
ll num1=(hx[r].first-hx[l-1].first*mi[r-l+1].first)%mod1;
num1=(num1+mod1)%mod1;
ll num2=(hx[r].second-hx[l-1].second*mi[r-l+1].second)%mod2;
num2=(num2+mod2)%mod2;
// if(l==1)
// cout<<num1<<" "<<num2<<endl;
return {num1,num2};
}
bool match(){
for(int i=0;i<=9;i++){
string ss;
for(int j=1;j<=m;j++){
int ts=i*num[j];
ss+=to_string(ts);
}
// cout<<s<<endl;
PII res={0,0};
for(int j=0;j<ss.size();j++){
ll res1=res.first*seed1+ss[j];
ll res2=res.second*seed2+ss[j];
res1%=mod1;
res2%=mod2;
res={res1,res2};
}
hsh[i]=res;
len[i]=ss.size();
}
int j=1;
for(int i=1;i<=n;i++){
bool flag=0;
for(int k=9;k>=0;k--){
// if(num[1]==4){
// // cout<<i<<endl;
// cout<<j<<" "<<j+len[k]-1<<endl;
// cout<<"-----"<<gethash(j,j+len[k]-1).first<<" "<<gethash(j,j+len[k]-1).second<<endl;
// cout<<hsh[k].first<<" "<<hsh[k].second<<endl;
// }
if(j+len[k]-1<=sz&&gethash(j,j+len[k]-1)==hsh[k]){
v[i]=k;
j+=len[k];
flag=1;
break;
}
}
if(!flag){
// for(int j=1;j<i;j++) cout<<v[j];
// cout<<endl;
return false;
}
}
if(j==sz+1)
return true;
else return false;
// if()
}
void __(){
scanf("%lld%lld",&n,&m);
scanf("%s",s+1);
// cout<<s+1<<endl;
sz=strlen(s+1);
flag=0;
for(int i=1;i<=sz;i++){
hx[i].first=(hx[i-1].first*seed1+s[i])%mod1;
hx[i].second=(hx[i-1].second*seed2+s[i])%mod2;
// cout<<hsh[i].first<<" "<<hsh[i].second<<endl;
}
for(int i=1;i<=9;i++){
for(int j=1;j<=m;j++) num[j]=0;
v[1]=i;
bool ff=0;
idx=1;
for(int j=1;j<=m;j++){
int t1=s[idx]-'0';
// cout<<t1<<" "<<t1%i<<endl;
if(t1%i==0){
num[j]=t1/i;
idx++;
}else{
// cout<<idx<<" "<<sz<<endl;
if(idx==sz){
ff=1;
break;
}
// cout<<t1*10+t2<<endl;
int t2=s[idx+1]-'0';
int tt=t1*10+t2;
// cout<<idx<<" "<<i<<" "<<tt<<" "<<tt%i<<endl;
// cout<<"--"<<tt<<endl;
if(tt%i==0&&tt/i<10){
num[j]=tt/i;
idx+=2;
}else{
ff=1;
break;
}
}
}
// cout<<i<<endl;
// for(int j=1;j<=m;j++) cout<<num[j];
// cout<<endl;
if(!ff){
if(match()){
for(int i=1;i<=n;i++) printf("%d",v[i]);
printf(" ");
for(int i=1;i<=m;i++) printf("%d",num[i]);
printf("\n");
return ;
}
}
}
// cout<<cc<<endl;
puts("Impossible");
}
signed main(){
// for(int i=0;i<=9;i++){
// for(int j=0;j<=9;j++) cout<<i*j<<" ";
// cout<<endl;
// }
mi[0]={1,1};
for(int i=1;i<N;i++){
mi[i].first=mi[i-1].first*seed1%mod1;
mi[i].second=mi[i-1].second*seed2%mod2;
}
int _;
cin>>_;
// if(_<10){
// puts("23 45\n101 1000\nImpossible\nImpossible\n");
// return 0;
// }else _=1;
while(_--){
__();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 46996kb
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: 190ms
memory: 52716kb
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