人傻不會作...因而看的題解...ios
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL long long
#define MAXN 500010
#define INF (1LL<<62)
LL N;
char S[MAXN];
int last=1,sz=1,root=1,par[MAXN<<1],len[MAXN<<1],son[MAXN][4];
LL f[MAXN][4];
inline void Extend(int c)
{
int cur=++sz,p=last;
len[cur]=len[p]+1;
while (p && !son[p][c]) son[p][c]=cur,p=par[p];
if (!p) par[cur]=root;
else {
int q=son[p][c];
if (len[p]+1==len[q]) par[cur]=q;
else {
int nq=++sz;
memcpy(son[nq],son[q],sizeof(son[nq]));
len[nq]=len[p]+1; par[nq]=par[q];
while (p && son[p][c]==q) son[p][c]=nq,p=par[p];
par[q]=par[cur]=nq;
}
}
last=cur;
}
bool visit[MAXN<<1];
inline void Dfs(int now)
{
if (visit[now]) return;
visit[now]=1;
for (int c=0; c<4; c++)
if (son[now][c])
Dfs(son[now][c]),f[now][c]=INF;
else f[now][c]=1;
for (int c=0; c<4; c++)
if (son[now][c])
for (int cc=0; cc<4; cc++)
f[now][cc]=min(f[now][cc],f[son[now][c]][cc]+1);
}
struct Matrix{
LL a[4][4];
Matrix() {for (int i=0; i<4; i++) for (int j=0; j<4; j++) a[i][j]=INF;}
LL* operator [](int x) {
return a[x];
}
}X;
Matrix operator * (Matrix &A,Matrix &B) {
Matrix C;
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
for (int k=0; k<4; k++)
C[i][j]=min(C[i][j],A[i][k]+B[k][j]);
return C;
}
Matrix operator ^ (Matrix x,LL y) {
Matrix re;
memset(re.a,0,sizeof(re.a));
for (int i=0; i<4; i++) re[i][i]=1;
for ( ; y; y>>=1,x=x*x) if (y&1) re=re*x;
return re;
}
inline bool check(LL x)
{
Matrix B=X^x;
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
if (B[i][j]+1<=N) return 1;
return 0;
}
int main()
{
scanf("%lld%s",&N,S+1); int L=strlen(S+1);
for (int i=1; i<=L; i++) Extend(S[i]-'A');
Dfs(root);
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
X[i][j]=f[son[root][i]][j];
// for (int i=0; i<4; i++,puts(""))
// for (int j=0; j<4; j++) printf("%I64d ",X[i][j]==INF? -1:X[i][j]);
LL l=0,r=N+1,mid,ans=0;
while (l<=r) {
mid=(l+r)>>1;
if (check(mid)) l=mid+1;
else r=(ans=mid)-1;
}
printf("%lld\n",ans);
return 0;
}
/*
5
ABCCAD
*/