题目链接:P4156 [WC2016]论战捆竹竿
本题解同步发布于 My Blog
题意:
给定长度为 的字符串 ,现有一个空串 ,每次可将 去掉一个 后接在 上。问 的长度可以是 中的多少个数。
。
每次去掉一个 加入,等价于加入一个 。设 的 长度构成集合 ,取出 中一个元素 。
借用同余最短路的思想,设 表示 的所有长度中,能被 中元素所拼出的最小长度。则能够被拼出的,长度 的串长为 。可以通过该数组快速求出答案。
则现在需要做的是加速同余最短路的过程,快速求出 。
考虑以下结论:
一个字符串 的 长度构成 个等差数列。
而 对应 ,所以也是等差数列。这启发我们将一个等差数列中的 放在一起考虑。
对于一个等差数列 ,若当前已经得到之前的等差数列所求出的, 意义下的 ,考虑如何快速用一个等差数列更新该数组。
在 意义下,公差为 的等差数列将所有 中的元素划分为 个等价类,每个等价类互不干扰。
对于一个等价类,找出其中 值最小的位置 ,显然在这轮更新中, 不改变。则从 开始,每次考虑使用等差数列中一个元素来更新后面的元素。
具体来说,对于一个下标为 的位置,若 ,则可以使用 来更新 。(此处将在 意义下的环展开,并认为 是其中第一个元素,则 )
这显然可以使用单调队列来实现。
还剩下一个问题,求解完当前等差数列后,如何跳转到下一个等差数列?对于一个首项为 的等差数列,求解完后 是在 的意义下,而下一个首项为 的等差数列中需在 意义下进行。
首先,可以使用 转移到 ,同时,还需要考虑每一个长度为 的转移,即 也可能更新 。这和上面是类似的问题,但是由于没有项数限制,所以甚至不用单调队列,记一个前缀 即可。
时间复杂度 。
//Code By CXY07
#include<bits/stdc++.h>
using namespace std;
//#define FILE
#define int long long
#define file(FILENAME) freopen(FILENAME".in", "r", stdin), freopen(FILENAME".out", "w", stdout)
#define randint(l, r) (rand() % ((r) - (l) + 1) + (l))
#define LINE() cout << "LINE = " << __LINE__ << endl
#define debug(x) cout << #x << " = " << x << endl
#define abs(x) ((x) < 0 ? (-(x)) : (x))
#define min(a, b) (a < b ? a : b)
#define inv(x) qpow((x), mod - 2)
#define lowbit(x) ((x) & (-(x)))
#define ull unsigned long long
#define pii pair<int, int>
#define LL long long
#define mp make_pair
#define pb push_back
#define scd second
#define vec vector
#define fst first
#define endl '\n'
const int MAXN = 5e5 + 10;
const int INF = 1.1e18;
const double PI = acos(-1);
const double eps = 1e-6;
//const int mod = 1e9 + 7;
//const int mod = 998244353;
//const int G = 3;
//const int base = 131;
int T, n, w, mod, Ans;
int fail[MAXN], period[MAXN], cnt;
int dis[MAXN];
char s[MAXN];
template<typename T> inline bool read(T &a) {
a = 0; char c = getchar(); int f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {a = a * 10 + (c ^ 48); c = getchar();}
a *= f;
return 1;
}
template<typename A, typename ...B>
inline bool read(A &x, B &...y) {return read(x) && read(y...);}
int Gcd(int x, int y) {
if(!y) return x;
return Gcd(y, x % y);
}
void clear() {
memset(dis, 0x3f, sizeof dis);
memset(fail, 0, sizeof fail);
}
void GetPeriod() {
fail[1] = 0, fail[0] = -1, cnt = 0;
for(int i = 2, j; i <= n; ++i) {
j = fail[i - 1];
while((~j) && s[j + 1] != s[i]) j = fail[j];
fail[i] = ++j;
}
for(int i = fail[n]; ~i; i = fail[i]) period[++cnt] = n - i;
}
void _run(int p, int gap, int c) { // i % mod == p, mod...mod + gap * c
static int t[MAXN], m, pos; m = 0, pos = -1;
static int q[MAXN], head, tail; head = 1, tail = 0;
for(int i = p; ;) {
if(pos == -1 || dis[i] < dis[pos]) pos = i;
i = (i + gap) % mod; if(i == p) break;
}
for(int i = pos; ;) {
t[++m] = i; i = (i + gap) % mod;
if(i == pos) break;
} assert(m == mod / Gcd(gap, mod));
q[++tail] = 1;
for(int i = 2; i <= m; ++i) {
while(head <= tail && i - q[head] > c) head++;
if(head <= tail && i - q[head] <= c) dis[t[i]] = min(dis[t[i]], dis[t[q[head]]] + (i - q[head]) * gap + mod);
while(head <= tail && dis[t[i]] - i * gap < dis[t[q[tail]]] - q[tail] * gap) tail--;
q[++tail] = i;
}
}
void run(int gap, int c) { // mod...mod + gap * c
int lim = Gcd(gap, mod);
for(int i = 0; i < lim; ++i) _run(i, gap, c);
}
void trans(int _new) { // mod -> _new
static int _d[MAXN]; memset(_d, 0x3f, sizeof _d);
static int t[MAXN], m;
int lim = Gcd(mod, _new);
for(int i = 0; i < mod; ++i) _d[dis[i] % _new] = min(_d[dis[i] % _new], dis[i]);
for(int p = 0, pos, mn; p < lim; ++p) {
pos = -1, m = 0, mn = INF;
for(int i = p; ;) {
if(pos == -1 || _d[i] < _d[pos]) pos = i;
i = (i + mod) % _new; if(i == p) break;
}
for(int i = pos; ;) {
t[++m] = i;
i = (i + mod) % _new; if(i == pos) break;
} assert(m == _new / lim);
for(int i = 1; i <= m; ++i) {
_d[t[i]] = min(_d[t[i]], mn + i * mod);
mn = min(mn, _d[t[i]] - i * mod);
}
}
mod = _new;
memcpy(dis, _d, sizeof dis);
}
void calc() {
int L = 1, R; mod = period[L];
while(L < cnt) {
R = L + 1;
while(R + 1 <= cnt && period[R + 1] - period[R] == period[R] - period[R - 1]) R++;
run(period[L + 1] - period[L], R - L);
L = R + 1; if(L <= cnt) trans(period[L]);
}
}
void solve() {
Ans = 0;
clear(); read(n), read(w), scanf("%s", s + 1); w -= n;
GetPeriod(); dis[0] = 0; calc();
for(int i = 0; i < mod; ++i)
if(dis[i] <= w) Ans = Ans + (w - dis[i]) / mod + 1;
printf("%lld\n", Ans);
}
signed main () {
#ifdef FILE
freopen("P4156.in", "r", stdin);
freopen("P4156.out", "w", stdout);
#endif
read(T);
while(T--) solve();
return 0;
}