QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#428048 | #8769. Champernowne Substring | ucup-team3564# | WA | 2030ms | 3732kb | C++14 | 4.0kb | 2024-06-01 17:10:32 | 2024-06-01 17:10:33 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
#define int __int128
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,ll y,int p=mod){
int ans=1;y%=(p-1);
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}
string out(int x){
string res="";res+=((char)(x%10+'0'));
if(x>=10)return out(x/10)+res;
return res;
}
int getpos(int w){
int ans=0;
for(int t=0,P=1;t<=30;t++,P*=10)if(w>=P)ans+=w-P;
return ans+1;
}
int chk_pos(string A,string B){
for(int i=0;i+B.size()<=A.size();i++){
bool chk=true;
for(int j=0;j<B.size();j++)if(B[j]!='?'&&A[i+j]!=B[j]){chk=false;break;}
if(chk)return i;
}
return -1;
}
const int D=26;
string get_str(int x){
string res="";
for(int i=x-D;i<=x+D;i++)if(i>=1)res+=out(i);
return res;
}
ostream &operator<<(ostream &fout,__int128 num){
fout<<out(num);
return fout;
}
void solve(){
string str;cin>>str;
// case 1
int w=0,ans=0;
if(str[0]=='?')w=1;
else if(str[0]=='0')w=10,ans++;
else w=str[0]-'0';
for(int i=1;i<str.size();i++){
w=w*10;
if(str[i]!='?')w+=str[i]-'0';
}
ans+=getpos(w);
// for(int i=1;i<=3000000;i++)if(getpos(i)>=8058869){cout<<"i = "<<out(i)<<endl;break;}
// cout<<"w = "<<out(w)<<" ans = "<<out(ans)<<" "<<out(ans%mod)<<endl;
// case 2
auto get_val=[&](int l,int r,int B)->__int128 {
// cout<<"get_val l,r = "<<l<<" "<<r<<" B = "<<B<<endl;
if(l>r)return 0;
int nl=l,nr=r;
string now="";for(int i=l;i<=r;i++)now+=str[i];
while(l>=0)l-=B,r-=B;
while(l<str.size()){
for(int i=l;i<=r;i++)if(i>=0&&i<str.size()){
if(str[i]=='?')continue;
if(now[i-l]=='?')now[i-l]=str[i];
else if(now[i-l]!=str[i])return -1;
}
l+=B,r+=B;
}
if(now[0]=='0')return -1;
if(now[0]=='?')now[0]='1';
int w=0;
for(int i=0;i<=r-l;i++){
w=w*10;
if(now[i]!='?')w+=now[i]-'0';
}
return w;
};
string tmp=str;
auto test=[&](int w){
string A=get_str(w);
int P=chk_pos(A,tmp);
if(P!=-1)cmin(ans,P+getpos(max((__int128)(1),w-D)));
};
for(int l=0;l<str.size();l++){
for(int r=l;r<str.size();r++){
// cout<<"try l,r = "<<l<<" "<<r<<endl;
// int l=1,r=5;
int t=max(l,r-2);
function<void(int)>dfs=[&](int now){
if(now==r+1){
// cout<<"try str = "<<str<<endl;
int w=get_val(l,t-1,r-l+1);
if(w==-1)return ;
for(int i=t;i<=r;i++)w=w*10+str[i]-'0';
// if(w==65054)cout<<" w = "<<w<<endl;
test(w);return ;
}
if(str[now]!='?')dfs(now+1);
else{
for(int c=0;c<=9;c++)str[now]=(char)(c+'0'),dfs(now+1),str[now]='?';
}
};
dfs(t);
}
}
// cout<<"now ans = "<<ans<<endl;
for(int l=0;l<str.size();l++){
for(int r=l;r<str.size();r++){
for(int t=r;t>=l+1;t--){
if(str[t]!='9'&&str[t]!='?')break;
int w=get_val(l,t-2,r-l+1);
if(w==-1)continue;
if(str[t-1]=='?')w=w*10;
else w=w*10+str[t-1]-'0';
for(int i=t;i<=r;i++)w=w*10+9;
// cout<<"test l,r = "<<l<<" "<<r<<" t = "<<t<<" w = "<<w<<endl;
test(w);
// cout<<" -> ans = "<<ans<<endl;
}
}
}
for(int t=1,P=9;t<=25;t++,P=P*10+9)test(P);
cout<<out(ans%mod)<<endl;
// ans=1e30;
// for(int w=1;w<=200000;w++)test(w);
// cout<<ans%mod<<endl;
}
signed main(void){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
// cout<<out(getpos(9))<<" "<<out(getpos(11))<<endl;
int tt=read();while(tt--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1564ms
memory: 3724kb
input:
9 0 ???1 121 1?1?1 ??5?54?50?5?505?65?5 000000000000 ?2222222 ?3????????9??8???????1??0 9?9??0????????????2
output:
11 7 14 10 314159 796889014 7777 8058869 38886
result:
ok 9 lines
Test #2:
score: -100
Wrong Answer
time: 2030ms
memory: 3732kb
input:
10 0000000000000000000000000 0000000?002100000000000?0 6999?999?999999989?999999 0???0?1000?0??000?????0?1 9??9?999998?9?999999100?0 96?9997999?8999991????010 99?99??999999999??????99? ?0?0000?00000000?0210?0?0 99?999?999?99?9??999?9?9? 9?????9?99?99??9??99??9??
output:
545305036 574985081 788888865 5889591 902934046 488873 902933157 830780534 68888820 5882870
result:
wrong answer 7th lines differ - expected: '902034054', found: '902933157'