QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#427549 | #8769. Champernowne Substring | ucup-team1447# | TL | 5ms | 3804kb | C++14 | 6.4kb | 2024-06-01 13:44:54 | 2024-06-01 13:44:55 |
Judging History
answer
// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define int long long
#define ull unsigned long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 1000045
#define inf 0x3f3f3f3f
int n;
string s;
string res; int pos;
bool small(string a,string b){
if(a.size()!=b.size()) return a.size()<b.size();
return a<b;
}
void check(string res2,int pos2){
if(res==res2){
if(pos2<pos) pos=pos2;
}else{
if(small(res2,res)) res=res2,pos=pos2;
}
}
int m,l,pl;
string mp[30];
vector<int>ch[30];
bool vis[30];
string fir;
string add(string s){
reverse(s.begin(),s.end());
s[0]+=1;
int pos=0;
while(!isdigit(s[pos])){
s[pos]='0';
if(pos+1==s.size()) s+='0';
++s[pos+1];
++pos;
}
reverse(s.begin(),s.end());
return s;
}
//string sub(string s){
//
//}
bool eq(char a,char b){
return b=='?' || a==b;
}
void qwq(string ans,int p){
bool FL=(ans=="9999");
// if(ans=="65053") cout<<"check "<<ans<<" "<<p<<"\n";
// if(FL)cout<<"qwq "<<ans<<" "<<p<<"\n";
string ans1=ans;int p1=p;
For(i,0,n-1){
// if(FL)cout<<"ANS[p],s[i] "<<ans[p]<<" "<<s[i]<<"\n";
if(!eq(ans[p],s[i])) return /*cout<<"fail "<<i<<" "<<ans[p]<<" "<<s[i]<<"\n",*/void();
++p;
if(p==ans.size()) {
p=0;
ans=add(ans);
}
}
// cout<<"check "<<ans1<<" "<<p1<<"\n";
check(ans1,p1);
}
bool flag;
void dfs(int u){
if(u==l){
// if(flag) cout<<"fir "<<fir<<" "<<pl<<"\n";
qwq(fir,(l-pl)%l);
return;
}
for(int c:ch[u]) {
if(u==0 && c==0) continue;
fir[u]=((char)(c+'0')),dfs(u+1);
}
}
modint pw[233],ss[233];
modint calcmod(string s){
int n=s.size();
modint x=0;
for(auto c:s) x=x*10+(c&15);
modint res=(x-pw[n-1])*n;
res+=ss[n-1];
return res;
}
void work_mp()
{
// cout<<"workmp:\n";
// cout<<"l,pl "<<l<<" "<<pl<<"\n";
// For(i,1,m) cout<<mp[i]<<"\n";
For(i,0,l-1) ch[i].clear();
For(i,0,9) ch[l-1].pb(i);
For(i,0,l-2){
For(j,0,9) vis[j]=0;
For(j,1,m) if(mp[j][i]!='?') vis[mp[j][i]&15]=1;
// For(j,0,9) if(vis[j]) For(k,0,9) if(k!=j && vis[k]) {
// if(())
// }
// For(j,0,9) cout<<vis[j]; cout<<"\n";
int hav=-1;
For(j,0,9) if(vis[j]) {
// hav=j;
if(vis[(j+1)%10]){
int k=(j+1)%10;
hav=j;
For(x,0,9) if(x!=j && x!=k && vis[x]) return ;
ch[i].pb(j),ch[i].pb(k);
break;
}
}
if(hav!=-1) continue;
For(j,0,9) if(vis[j]){
hav=j;
For(x,0,9) if(x!=j && vis[x]) return;
ch[i].pb(j),ch[i].pb((j-1+10)%10);
// cout<<"add "<<i<<" "<<j<<"\n";
break;
}
if(hav!=-1) continue;
if(i==0) ch[i].pb(1);
else ch[i].pb(9),ch[i].pb(0);
}
// if(mp[1]=="?????" && mp[2]=="?5?54") flag=1;
// if(flag){
// cout<<"QAQ\n";
// For(i,0,l-1){
// for(int x:ch[i]) cout<<x<<" "; cout<<"---";
// cout<<"\n";
// }
// }
fir.resize(l);
dfs(0);
}
void work()
{
cin>>s; n=s.size();
string in="99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
res=in,pos=0;
For(len,1,n+1){
For(i,0,len-1){
// cout<<"wk "<<len<<" "<<i<<"\n";
m=0;
if(i!=0){
string tmp;
For(j,1,len-i) tmp+='?';
For(j,0,i-1) tmp+=s[i];
assert(tmp.size()==len);
mp[++m]=tmp;
}
for(int l=i,r; l<n ; l=r+1){
r=l+len-1;
// cout<<"l,r "<<l<<" "<<r<<"\n";
string tmp;
For(j,l,r) if(j<n) tmp+=s[j]; else tmp+='?';
mp[++m]=tmp;
}
l=len;
pl=i;
work_mp();
}
string tmp,mx;
For(i,1,len) tmp+='9';
mx=tmp;
// mx=
if(len==1) tmp="1";
else tmp[tmp.size()-1-1]='7';
// cout<<"Len "<<len<<" "<<tmp<<" "<<mx<<"\n";
do{
tmp=add(tmp);
// cout<<"TMP "<<tmp<<"\n";
For(j,0,len-1) qwq(tmp,j);
}while(tmp!=mx);
if(res!=in){
// cout<<"res: "<<res<<" "<<pos<<"\n";
modint out=calcmod(res);
out+=pos+1;
cout<<out.x<<"\n";
return;
}
}
}
signed main()
{
// cout<<add("9999")<<"\n";
pw[0]=1;
For(i,1,30)pw[i]=pw[i-1]*10;
For(i,1,30){
ss[i]=ss[i-1]+(pw[i]-pw[i-1])*i;
}
int T=read();
For(_,1,T)work();
return 0;
}
/*
650526505365054650556505665057
??5?54?50?5?505?65?5
(6)
9?9??0????????????2
999799989999100001000110002
?????
?5?54
?50?5
?505?
65?5?
10 10
1 2 2 2 5 2 3 4 3
1 10 5 7
2 4 3 4 5 6
*/
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 3804kb
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
Time Limit Exceeded
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??