QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#553716 | #9244. Counting Strings | ucup-team1004 | RE | 2ms | 16716kb | C++14 | 3.0kb | 2024-09-08 18:53:27 | 2024-09-08 18:53:28 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=2e5+5,M=(1<<20)+5,K=1e7+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const ll INF=1e18+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
char s[N];int n;
int id[N];
vector<int> S[N];
namespace SAM{
int cnt=1,La=1,son[N][26],link[N],len[N];
void extend(char c){
len[++cnt]=len[La]+1;int p=La;La=cnt;
while(p&&!son[p][c]) son[p][c]=La,p=link[p];
if(!p){link[La]=1;return;}
int q=son[p][c];
if(len[q]==len[p]+1){link[La]=q;return;}
len[++cnt]=len[p]+1;link[cnt]=link[q];link[q]=link[La]=cnt;Mc(son[cnt],son[q]);
while(p&&son[p][c]==q) son[p][c]=cnt,p=link[p];
}
void build(){
for(int i=2;i<=cnt;i++) S[link[i]].push_back(i);
}
}
int lim=0;
ull f[N][8];
int pr[N],ph,flag[N],La[N];
ull g[N][8];
void init(){
for(int i=2;i<=n;i++){
if(!flag[i]) pr[++ph]=i,La[i]=i;
for(int j=1;j<=ph&&i*pr[j]<=n;j++){
flag[i*pr[j]]=1;La[i*pr[j]]=pr[j];
if(i%pr[j]==0) break;
}
}
}
int qry(ull x){
int ans=0;
for(int i=0;i<64;i++) if(x>>i&1) ans+=i;
return ans;
}
ll ans=0;
void dfs(int x,int La){
if(id[x]) Mc(g[x],f[id[x]]);
else Me(g[x],0);
for(int i:S[x]){
dfs(i,x);
for(int j=0;j<8;j++) g[x][j]|=g[i][j];
}
if(x^1){
int r=min(SAM::len[x]-1,lim+8*64-1),l=max(SAM::len[La],lim);
if(l>r) return;
for(int i=0;i<8;i++){
int L=max(l-i*64-lim,0),R=min(r-lim-i*64,63);
if(L>R) continue;
ull w=g[x][i];
if(R^63) w&=(1ull<<R+1)-1;
w>>=L;
ans+=__builtin_popcountll(w)*(lim+i*64+L+1)+qry(w);
gdb(w,x,l,r,qry(w),ans);
}
}
}
void Solve(){
scanf("%d%s",&n,s+1);
init();
for(int i=n;i;i--) SAM::extend(s[i]-'a'),id[SAM::La]=i;
SAM::build();
lim=0;
while(lim<=n){
Me(f[1],-1);
for(int i=2;i<=n;i++){
Me(f[i],-1);
if(!flag[i]){
for(int j=(lim+i-1)/i*i;j<lim+8*64;j+=i) f[i][j-lim>>6]^=1ull<<(j-lim&63);
}else for(int j=0;j<8;j++) f[i][j]=f[i/La[i]][j]&f[La[i]][j];
for(int j=0;j<n;j++) assert((__gcd(i,j)==1)==(f[i][j-lim>>6]>>(j-lim&63)&1));
}
dfs(1,0);
lim+=8*64;
}
printf("%lld\n",ans);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11148kb
input:
4 abca
output:
14
result:
ok answer is '14'
Test #2:
score: 0
Accepted
time: 2ms
memory: 12368kb
input:
1 a
output:
1
result:
ok answer is '1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 10264kb
input:
2 aa
output:
3
result:
ok answer is '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 8992kb
input:
2 ab
output:
3
result:
ok answer is '3'
Test #5:
score: 0
Accepted
time: 2ms
memory: 10108kb
input:
100 xgljgljgljjljjjjljtjljtgljgljgljjljjjjljtjljtjljgljljgljgljjljjjjljtjljtjljgljllljgllgljgljjglljgljl
output:
101808
result:
ok answer is '101808'
Test #6:
score: 0
Accepted
time: 0ms
memory: 16716kb
input:
100 ilhliaiehaiehldgeieieedveldgeaeaehldgeldgeiiedeiaiehaiehldgeieieedveiaehldgeihleiaeaehldgeiaeaeeheia
output:
103718
result:
ok answer is '103718'
Test #7:
score: 0
Accepted
time: 2ms
memory: 11088kb
input:
100 xoakgbazaclazfrmucgodlhrvazkyrqcwufonvcmqpvulhuudtmcfhsklletywchvvrxrjfgsfaoozzouzwfrtdlryfiqdtzjkcp
output:
104574
result:
ok answer is '104574'
Test #8:
score: 0
Accepted
time: 2ms
memory: 12392kb
input:
100 aabbaabbabaaabaaaaaabababbbabaabaaabaaabaaaabbbbababbbbbbabbbaabbbaaaaababababaabababaababaabbbabaaa
output:
103589
result:
ok answer is '103589'
Test #9:
score: -100
Runtime Error
input:
2000 mbrpuyrsqimuuwnmxsukdkfmhwytmfwwccfdkzljulnvlueciggqniuyocqxoiepzuzhnfwwccvmxyticsttujuzhnfwwccfwkbuuecinfwwccfwkbuueciggqniuyodfkhnfwwccfdkzljulnvlueciggqniuyocqccfwkbuueciggquyciggqniuyodfkevrpjzuzwyocqccfwkbuueciggquyciggqniuyodfkevrpjzuzhnfwwcoiynpzlqsjimovvcrzbggnciggqniuyodfkevrpjzuzhnfwy...