Given a character string \(s\), we
define \(L(k,s)\) to be the length of
the longest substring of \(s\) which
appears at least \(k\) times in \(s\), or \(0\) if such a substring does not exist. For
example, \(L(3,\text{“bbabcabcabcacba”})=4\) because
of the three occurrences of the substring \(\text{“abca”}\), and \(L(2,\text{“bbabcabcabcacba”})=7\) because
of the repeated substring \(\text{“abcabca”}\). Note that the
occurrences can overlap.
Let \(a_n\), \(b_n\) and \(c_n\) be the \(0/1\) sequences defined by:
\(a_0 = 0\)
\(a_{2n} = a_{n}\)
\(a_{2n+1} = 1-a_{n}\)
\(b_n =
\left\lfloor\dfrac{n+1}{\varphi}\right\rfloor -
\left\lfloor\dfrac{n}{\varphi}\right\rfloor\) (where \(\varphi\) is the golden ratio)
\(c_n = a_n + b_n - 2a_nb_n\)
and \(S_n\) the character string
\(c_0\ldots c_{n-1}\). You are given
that \(L(2,S_{10})=5, L(3,S_{10})=2,
L(2,S_{100})=14, L(4,S_{100})=6, L(2,S_{1000})=86, L(3,S_{1000}) = 45,
L(5,S_{1000}) = 31\), and that the sum of non-zero \(L(k,S_{1000})\) for \(k\ge 1\) is \(2460\).
Find the sum of non-zero \(L(k,S_{5000000})\) for \(k\ge 1\).
int c[N+4]; int sa[N+4], rk[N+4], oldrk[(N+4) << 1], id[N+4], key1[N+4], cnt[N+4]; int h[N + 4]; boolcmp(int x, int y, int w){ return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w]; } voidrun_sa(int *s,int n,int m){ for (int i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]]; for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i; for (int w = 1, p;; w <<= 1, m = p) { p = 0; for (int i = n; i > n - w; --i) id[++p] = i; for (int i = 1; i <= n; ++i) if (sa[i] > w) id[++p] = sa[i] - w;
memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; ++i) ++cnt[key1[i] = rk[id[i]]]; for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; --i) sa[cnt[key1[i]]--] = id[i]; memcpy(oldrk + 1, rk + 1, n * sizeof(int)); p = 0; for (int i = 1; i <= n; ++i) rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p; if (p == n) { for (int i = 1; i <= n; ++i) sa[rk[i]] = i; break; } } for (int i = 1, k = 0; i <= n; ++i) { if (rk[i] == 0) continue; if (k) --k; while (max(sa[rk[i] - 1], i) + k <= n && s[i + k] == s[sa[rk[i] - 1] + k]) ++k; h[rk[i]] = k; } } int fa[N+4],sz[N+4],vis[N+4],mx = 0; intfind(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} voidmerge(int x,int y){ int u=find(x),v=find(y); if(u!=v){ fa[v]=u; sz[u]+=sz[v]; } } vector<int>pt[N+4]; int l[N+4]; intmain(){ clock_t t = clock(); for(int i=1;i<=N;i++) c[i]=__builtin_popcount(i-1)&1;