QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#665795#9514. 研心Tony2100 ✓3789ms1654348kbC++1412.9kb2024-10-22 15:19:402024-10-22 15:20:26

Judging History

你现在查看的是最新测评结果

  • [2024-10-22 15:20:26]
  • 评测
  • 测评结果:100
  • 用时:3789ms
  • 内存:1654348kb
  • [2024-10-22 15:19:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 4e5 + 5, lim = 4e5, S = 19260817;
ull pw[N];
int n, m;
ll ans;
struct Str{
	int len;
	string s;
	vector<ull> hs;
	vector<int> vr;
	ull gethash(int l, int r){
		return (hs[r] - hs[l - 1]) * pw[lim - r];
	}
	void init(){
		len = (int)s.size();
		s.insert(s.begin(), ' ');
		hs = vector<ull>(len + 2, 0);
		for (int i = 1; i <= len; i++) hs[i] = hs[i - 1] + pw[i] * s[i];
	}
}a[N], b[N];
struct Trie{
	int tot;
	struct node{
		vector<int> ed;
		int son[26];
	}a[N];
	unordered_map<ull, int> mp;
	int dfsnum, dfn[N], rev[N], pos[N];
	int log2n, ff[N][25], de[N];
	int tp, sta[N];
	int f[N], g[N];
	vector<int> e2[N];
	vector<array<int, 3>> vp[N];
	struct Msg{
		int u, v, k, id;
	};
	vector<Msg> vec1, vec2;
	Trie(){
		tot = 1;
	}
	void insert(string s, int id){
		int p = 1;
		for (char c : s){
			if (!a[p].son[c - 'a']) a[p].son[c - 'a'] = ++tot;
			p = a[p].son[c - 'a'];
		}
		a[p].ed.push_back(id);
		pos[id] = p;
	}
	void dfs(int u, ull hs){
		mp[hs * pw[lim - de[u]]] = u;
		for (int i = 1; i <= log2n; i++) ff[u][i] = ff[ff[u][i - 1]][i - 1];
		dfn[u] = ++dfsnum;
		rev[dfsnum] = u;
		for (int i = 0; i < 26; i++){
			int v = a[u].son[i];
			if (v){
				ff[v][0] = u;
				de[v] = de[u] + 1;
				dfs(v, hs + pw[de[v]] * ('a' + i));
			}
		}
	}
	int kfa(int u, int k){
		for (int i = 0; k; i++, k >>= 1)
			if (k & 1)
				u = ff[u][i];
		return u;
	}
	int lca(int u, int v){
		if (de[u] < de[v]) swap(u, v);
		u = kfa(u, de[u] - de[v]);
		if (u == v) return u;
		for (int i = log2n; i >= 0; i--)
			if (ff[u][i] != ff[v][i])
				u = ff[u][i], v = ff[v][i];
		return ff[u][0];
	}
	void init(){
		log2n = 20;
		dfs(1, 0);
		fill(f + 1, f + 1 + tot, -1e9);
	}
	void insert(int u){
		if (tp <= 1){
			sta[++tp] = u;
			return;
		}
		int l = lca(u, sta[tp]);
		while (tp > 1 && de[sta[tp - 1]] >= de[l]){
			e2[sta[tp - 1]].push_back(sta[tp]);
			tp--;
		}
		if (sta[tp] != l){
			e2[l].push_back(sta[tp]);
			sta[tp] = l;
		}
		sta[++tp] = u;
	}
	void dfs1(int u){
		g[u] = 0;
		for (int v : e2[u]){
			dfs1(v);
			f[u] = max(f[v], f[u]); 
		}
	}
	void dfs2(int u, int id){
		g[u] = max(f[u] + de[u], g[u]);
		vp[u].push_back({g[u], id, 1});
		for (int v : e2[u]){
			vp[kfa(v, de[v] - de[u] - 1)].push_back({g[u], id, -1});
			g[v] = max(g[u], g[v]);
			dfs2(v, id);
			int d = g[u] - (g[v] - (de[v] - de[u]));
			if (d){
				int w = kfa(v, de[v] - de[u] - d);
				vec1.push_back(Msg{kfa(v, de[v] - de[u] - 1), w, g[u], id});
				if (w != v) vec2.push_back(Msg{w, v, g[u], id});
			}else vec2.push_back(Msg{kfa(v, de[v] - de[u] - 1), v, g[u] + 1, id});
		}
		e2[u].clear();
		f[u] = -1e9;
	}
	void getchain(Str s, int id){
		int n = s.len, mx = 0;
		for (int i = 1; i <= n; i++) mx = max(s.vr[i], mx);
		unordered_map<int, int> mp2;
		mp2[1] = mx;
		for (int i = 1; i <= n; i += 2)
			if (s.vr[(i + 1) / 2] == (i + 1) / 2){
				int l = 0, r = n - i, mid;
				while (l <= r){
					mid = (l + r) >> 1;
					if (mp.find(s.gethash(i + 1, i + mid)) != mp.end()) l = mid + 1;
					else r = mid - 1;
				}
				int u = mp[s.gethash(i + 1, i + r)];
				mp2[u] = max((i + 1) / 2, mp2[u]);
			}
		vector<int> vec;
		for (auto [x, y] : mp2) vec.push_back(x);
		sort(vec.begin(), vec.end(), [&](int x, int y){return dfn[x] < dfn[y];});
		tp = 0;
		for (auto x : vec) insert(x);
		while (tp > 1){
			e2[sta[tp - 1]].push_back(sta[tp]);
			tp--;
		}
		for (auto [x, y] : mp2) f[x] = max(y, f[x]);
		dfs1(1);
		dfs2(1, id);
	}
}ta, tb;
struct Tree{
	Trie *t;
	vector<int> e[N];
	int de[N], fa[N], sz[N], son[N], top[N], topid[N], botid[N];
	int tot;
	vector<int> chain[N];
	struct node{
		int lu, ru, ls, rs, fa;
	}a[N * 2];
	vector<array<int, 3>> t1[N * 2], t2[N * 2];
	void dfs1(int u){
		sz[u] = 1;
		for (int v : e[u]){
			fa[v] = u;
			de[v] = de[u] + 1;
			dfs1(v);
			sz[u] += sz[v];
			if (sz[v] > sz[son[u]]) son[u] = v;
		}
	}
	void dfs2(int u){
		if (son[u]){
			top[son[u]] = top[u];
			dfs2(son[u]);
		}
		for (int v : e[u])
			if (v != son[u]){
				top[v] = v;
				dfs2(v);
			}
	}
	int dfs4(int l, int r){
		int u = ++tot;
		a[u].lu = l, a[u].ru = r;
		if (l == r){
			botid[l] = u;
			return u;
		}
		int sum = sz[l] - sz[son[r]];
		pair<int, int> p(1e9, 0);
		for (int x = l; x != r; x = son[x]){
			int s = sz[son[x]] - sz[son[r]];
			p = min(make_pair(abs(2 * s - sum), x), p);
		}
		int mid = p.second;
		a[a[u].ls = dfs4(l, mid)].fa = u;
		a[a[u].rs = dfs4(son[mid], r)].fa = u;
		return u;
	}
	void dfs3(int u){
		int x;
		for (x = u; son[x]; x = son[x]);
		int id = dfs4(u, x);
		a[id].fa = botid[fa[u]];
		for (x = u; x; x = son[x]){
			topid[x] = id;
			for (int v : e[x])
				if (v != son[x])
					dfs3(v);
		}
	}
	void dfs5(int now, int lu, int ru, int k, int k2, int id){
		if (de[lu] <= de[a[now].lu] && de[a[now].ru] <= de[ru]){
			t1[now].push_back({k, id, k2});
			return;
		}
		int mid = a[a[now].ls].ru;
		if (de[lu] <= de[mid]) dfs5(a[now].ls, lu, ru, k, k2, id);
		if (de[mid] < de[ru]) dfs5(a[now].rs, lu, ru, k, k2, id);
	}
	void dfs6(int now, int lu, int ru, int k, int k2, int id){
		if (de[lu] <= de[a[now].lu] && de[a[now].ru] <= de[ru]){
			t2[now].push_back({k, id, k2});
			return;
		}
		int mid = a[a[now].ls].ru;
		if (de[lu] <= de[mid]) dfs6(a[now].ls, lu, ru, k, k2, id);
		if (de[mid] < de[ru]) dfs6(a[now].rs, lu, ru, k, k2, id);
	}
	void adds(int u, int k, int k2, int id){
		int tid = topid[u];
		dfs5(tid, u, a[tid].ru, k, k2, id);
	}
	void add1(int u, int v, int k, int id){
		adds(u, k, 1, id);
		adds(v, k, -1, id);
	}
	void add2(int u, int v, int k, int id){
		while (top[v] != top[u]){
			int tid = topid[v], tid2 = topid[fa[top[v]]];
			if (v != top[v]) dfs6(tid, top[v], fa[v], k, 1, id);
			dfs5(tid2, fa[top[v]], a[tid2].ru, k + de[fa[top[v]]], 1, id);
			dfs5(tid, top[v], a[tid].ru, k + de[fa[top[v]]], -1, id);
			v = fa[top[v]];
		}
		if (u != v) dfs6(topid[v], u, fa[v], k, 1, id);
	}
	void solve(){
		for (int i = 1; i <= t->tot; i++)
			for (int j = 0; j < 26; j++)
				if (t->a[i].son[j])
					e[i].push_back(t->a[i].son[j]);
		dfs1(1);
		top[1] = 1;
		dfs2(1);
		dfs3(1);
		for (int i = 1; i <= t->tot; i++)
			for (auto [k, id, k2] : t->vp[i])
				adds(i, k, k2, id);
		for (auto [u, v, k, id] : t->vec1) add1(u, v, k, id);
		for (auto [u, v, k, id] : t->vec2) add2(u, v, k - de[u], id);
	}
}ta2, tb2;
void manacher(Str &c){
	static int p[N];
	int n = c.len;
	c.vr = vector<int>(n + 2, 0);
	for (int i = 1, mid = 0, r = 0; i <= n; i++){
		if (i <= r) p[i] = min(p[mid * 2 - i], r - i + 1);
		else p[i] = 0;
		while (i + p[i] <= n && c.s[i + p[i]] == c.s[i - p[i]]) p[i]++;
		if (p[i] > r - mid + 1) r = i + p[i] - 1, mid = i;
		c.vr[i] = p[i];
	}
}
namespace Solve{
	vector<array<int, 3>> vr[N * 2][2][2], to[N * 2];
	bool vis[N * 2];
	vector<int> e[N * 2], e2[N * 2];
	vector<pair<int, int>> tag[N * 2][2][2], ques[N * 2][2];
	ll sum1[N * 2], sum2[N * 2];
	template<class t> struct cmp{
		bool operator () (const vector<t> &v1, const vector<t> &v2){
			return v1.size() > v2.size();
		}
	};
	template<class t> vector<t> merge(vector<vector<t>> vec){
		priority_queue<vector<t>, vector<vector<t>>, cmp<t>> q;
		for (vector<t> v : vec) q.push(v);
		while ((int)q.size() > 1){
			vector<t> v1 = q.top();
			q.pop();
			vector<t> v2 = q.top();
			q.pop();
			vector<t> v3(v1.size() + v2.size());
			merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
			q.push(v3);
		}
		return q.top();
	}
	void dfs3(int u, int c){
		if (tb2.a[u].lu == tb2.a[u].ru){
			vector<vector<pair<int, int>>> vec;
			vec.push_back(ques[u][0]); 
			for (int v : e2[u]){
				dfs3(v, c);
				vec.push_back(ques[v][0]);
				ques[v][0].clear();
			}
			ques[u][0] = merge(vec);
			for (auto [k, k2] : ques[u][0]) ques[u][1].emplace_back(k + tb.de[tb2.a[u].lu], k2);
		}else{
			vector<vector<pair<int, int>>> vec[2];
			for (int v : e2[u]){
				dfs3(v, c);
				vec[0].push_back(ques[v][0]);
				vec[1].push_back(ques[v][1]);
				ques[v][0].clear(), ques[v][1].clear();
			}
			if (e2[u].size() == 1) ques[u][0] = vec[0][0], ques[u][1] = vec[1][0];
			else for (int p : {0, 1}){
				ques[u][p].resize(vec[p][0].size() + vec[p][1].size());
				merge(vec[p][0].begin(), vec[p][0].end(), vec[p][1].begin(), vec[p][1].end(), ques[u][p].begin());
			}
		}
		for (int p : {0, 1}){
			int j = 0, sumq = 0, sump = 0;
			for (int i = 0; i < (int)ques[u][p].size(); i++){
				for (; j < (int)tag[u][p][c].size() && tag[u][p][c][j].first <= ques[u][p][i].first; j++){
					ans += 1ll * sumq * tag[u][p][c][j].second * tag[u][p][c][j].first;
					sump += tag[u][p][c][j].second;
				}
				ans += 1ll * sump * ques[u][p][i].second * ques[u][p][i].first;
				sumq += ques[u][p][i].second;
			}
			for (; j < (int)tag[u][p][c].size(); j++)
				ans += 1ll * sumq * tag[u][p][c][j].second * tag[u][p][c][j].first;
		}
	}
	void dfs4(int u){
		for (int v : e2[u]) dfs4(v);
		vis[u] = 0;
		ques[u][0].clear();
		ques[u][1].clear();
		e2[u].clear();
	}
	void dfs5(int u){
		for (auto [k, i, k2] : to[u]){
			sum1[i] += k2 * k;
			sum2[i] += k2;
		}
		for (int v : e[u]) dfs5(v);
	}
	void dfs6(int u){
		for (auto [k, i, k2] : to[u]) sum1[i] = sum2[i] = 0;
		for (int v : e[u]) dfs6(v);
	}
	void dfs2(int u){
		if (ta2.a[u].ls){
			dfs2(ta2.a[u].ls);
			dfs2(ta2.a[u].rs);
			for (int p : {0, 1})
				for (int q : {0, 1}){
					vector<array<int, 3>> &v1 = vr[ta2.a[u].ls][p][q], &v2 = vr[ta2.a[u].rs][p][q];
					if (v1.size() < v2.size()) swap(v1, v2);
					int s = v1.size();
					swap(vr[u][p][q], v1);
					vr[u][p][q].insert(vr[u][p][q].end(), v2.begin(), v2.end());
					inplace_merge(vr[u][p][q].begin(), vr[u][p][q].begin() + s, vr[u][p][q].end());
				}
		}
		dfs5(u);
		for (int p : {0, 1})
			for (int q : {0, 1})
				for (auto [k, i, k2] : vr[u][p][q])
					tag[i][p][q].emplace_back(k, k2);
		sort(ta2.t1[u].begin(), ta2.t1[u].end(), greater<array<int, 3>>());
		for (auto [k, id, k2] : ta2.t1[u]){
			for (int x = tb2.botid[tb.pos[id]]; x; x = tb2.a[x].fa){
				if (!vis[x]){
					vis[x] = 1;
					if (x > 1) e2[tb2.a[x].fa].push_back(x);
				}
				ans += k2 * sum1[x] + 1ll * k2 * sum2[x] * k;
			}
			ques[tb2.botid[tb.pos[id]]][0].emplace_back(-k, k2);
		}
		if (vis[1]){
			dfs3(1, 0);
			dfs4(1);
		}
		sort(ta2.t2[u].begin(), ta2.t2[u].end(), greater<array<int, 3>>());
		for (auto [k, id, k2] : ta2.t2[u]){
			for (int x = tb2.botid[tb.pos[id]]; x; x = tb2.a[x].fa){
				if (!vis[x]){
					vis[x] = 1;
					if (x > 1) e2[tb2.a[x].fa].push_back(x);
				}
				ans += k2 * sum1[x] + 1ll * k2 * sum2[x] * k;
			}
			ques[tb2.botid[tb.pos[id]]][0].emplace_back(-k, k2);
		}
		if (vis[1]){
			dfs3(1, 1);
			dfs4(1);
		}
		dfs6(u);
		for (int p : {0, 1})
			for (int q : {0, 1})
				for (auto [k, i, k2] : vr[u][p][q])
					tag[i][p][q].clear();
	}
	void dfs1(int u){
		for (int x = ta2.a[u].lu; x; x = ta2.son[x]){
			vector<vector<array<int, 3>>> vec[2];
			int xx = ta2.botid[x];
			sort(vr[xx][0][0].begin(), vr[xx][0][0].end());
			vec[0].push_back(vr[xx][0][0]);
			sort(vr[xx][1][0].begin(), vr[xx][1][0].end());
			vec[1].push_back(vr[xx][1][0]);
			for (int v : ta2.e[x])
				if (v != ta2.son[x]){
					dfs1(ta2.topid[v]);
					vec[0].push_back(vr[ta2.topid[v]][0][0]);
					vec[1].push_back(vr[ta2.topid[v]][1][0]);
				}
			vr[xx][0][0] = merge(vec[0]);
			vr[xx][1][0] = merge(vec[1]);
			for (auto [k, i, k2] : vr[xx][0][0]) vr[xx][0][1].push_back({k + ta.de[x], i, k2});
			for (auto [k, i, k2] : vr[xx][1][0]) vr[xx][1][1].push_back({k + ta.de[x], i, k2});
		}
		dfs2(u);
	}
	void solve(){
		for (int i = 1; i <= tb2.tot; i++){
			for (auto [k, id, k2] : tb2.t1[i]){
				vr[ta2.botid[ta.pos[id]]][0][0].push_back({-k, i, k2});
				to[ta2.botid[ta.pos[id]]].push_back({k, i, k2});
			}
			for (auto [k, id, k2] : tb2.t2[i]){
				vr[ta2.botid[ta.pos[id]]][1][0].push_back({-k, i, k2});
				to[ta2.botid[ta.pos[id]]].push_back({k, i, k2});
			}
		}
		for (int i = 2; i <= ta2.tot; i++) e[ta2.a[i].fa].push_back(i);
		dfs1(1);
	}
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	freopen("s1.in", "r", stdin);
//	freopen("s1.out", "w", stdout);
	pw[0] = 1;
	for (int i = 1; i <= lim; i++) pw[i] = pw[i - 1] * S;
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		cin >> a[i].s;
		reverse(a[i].s.begin(), a[i].s.end());
		ta.insert(a[i].s, i);
		a[i].init();
		manacher(a[i]);
	}
	for (int i = 1; i <= m; i++){
		cin >> b[i].s;
		tb.insert(b[i].s, i);
		b[i].init();
		manacher(b[i]);
	}
	ta.init();
	tb.init();
	for (int i = 1; i <= m; i++) ta.getchain(b[i], i);
	for (int i = 1; i <= n; i++) tb.getchain(a[i], i);
	ta2.t = &ta;
	tb2.t = &tb;
	ta2.solve();
	tb2.solve();
	Solve::solve();
	cout << ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 92ms
memory: 586096kb

input:

10 100
aabaababaaaababbabaaaababaababbbaabbbaabaabaabbbaababaaababbaababababbbaababbbbbbbbaabbbbaabbbbbbbbaabaabbbaabbbbbabbbbbaabbbaabaa
bbaaabaabbbbaaabaaaaabbbbbabaabbabbaabbaabbaabbabbababaabaabbaabbbbbbaaaabbbbabaaaabaaabbbabaaaaaabbaaaabbbabbbbbaaaabbabaababbaabbaaabbbbbbaaaabbbababaabbaaaba
b...

output:

10468

result:

ok single line: '10468'

Test #2:

score: 20
Accepted
time: 106ms
memory: 590036kb

input:

10 100
abbacbbaabababcaabbaabbbabcaccabcabbabbbccaacabaabacccbacaaabbaaabbbaacaabbabcabbbcbacaaaaabaaaabaaacbbccbacacbbaaccacbabcbccacbaaacaaacccbabbaccacaaacaabbbccaaccbbabaabcbbcbcbb
bccabcacbacccbcababaccccaaabbccacaccccabbaabbcacabaccabacbbbaaaabaabccbcaacaaccbcacabcbbbcccccabccbaababccccabcabab...

output:

6384

result:

ok single line: '6384'

Test #3:

score: 20
Accepted
time: 71ms
memory: 582960kb

input:

50 50
aaaaabbabbbbbaababbaaaababaababababbbbaabaaaaaaaabaabbaaabaaaabaaabaabbbabbbababbbbabbbabbaaababbabaabbabbabbabbaaababaaaaabbbbbbaabaabaabbbbbabaaababb
abbabbbaabaaaaabababaabbabaabbbbab
bababbbbaabaabbaabbbbaabaabbbaabbbbaaabbabbbaaabbbbaaabaabbaaaaaabbbabaaaaabbaabaabaaabaaababbbbbaaabbbbbba...

output:

21362

result:

ok single line: '21362'

Test #4:

score: 20
Accepted
time: 79ms
memory: 584616kb

input:

50 50
aababcabbbcbaacaaaaccababbacbbaaabcbaaccabbababbcccaacaaaacacbcbaaabaacabbbbbbaccaaacbbacaacacbcccbbacabbcbcbacababcbbacaccaacacbcccbababaccabccccbabaccababccccbcbabcabcabccaaccababcbccacbbaaaacc
cbaccababccbaaacbccabbcccacacacaaaabaaababcacabaccbccacbcbaaaaabbacbbaccabba
ccbcbcacbcaccabbbccaa...

output:

13421

result:

ok single line: '13421'

Test #5:

score: 20
Accepted
time: 105ms
memory: 589816kb

input:

1000 1000
a
aabbab
bbbbababbbba
bb
baaaaa
ba
a
baa
a
bbaaaabaaaba
ba
a
a
a
bbababbbbbb
b
aaabb
bbbbbaabbabab
bbaaa
aaaa
aa
aaaaaababb
a
bbaba
baaa
aabbab
babaab
b
aab
bbbabb
aaaabbbbbaaaaaa
bbbbbbbaabab
bb
ab
aaa
aaababb
babaaaabab
aa
aaabaaababa
abbabaaaaabb
bbaa
abaabb
baa
abba
aaaa
abbbb
aab
b
aa...

output:

3159935

result:

ok single line: '3159935'

Subtask #2:

score: 30
Accepted

Test #6:

score: 30
Accepted
time: 911ms
memory: 844912kb

input:

1 10000
bcacacbcdadbcbccbdddaaddabcaccccbaabbcdcdaabdacccadacbbbabdaaacadacdccadcbdadadcaaabdbdbdcdcccbacbdbccacbdadcaaaacbabdadcacdcacadcadaabdaaabccccdcbcdbcadcaabbbaacaadaccdaccadbddcdbccbcddcaadcaadaadbbaadbcdbddbcbadbccbaddaacabcdccbadaddcadcbababccabcaadbcbdbcbbcdcbdbcdbdaaddbacddcabbbdccbddba...

output:

1160913325

result:

ok single line: '1160913325'

Test #7:

score: 30
Accepted
time: 645ms
memory: 850388kb

input:

1 1000
caaadbacdaaccbdcaccbdbddcdaccaaaaaccdccaadbadbcdccaacdadcccaddadcbacbbdcaadbbcdaddcadddccdbabccdadcaacabcabbbdbbbbdbbaaccddcdddcddddddbdbbadbddcbacdcdbdabcbbdbbaaadbdacaccdbaaabacbacabdaccaabaadddccbabacdbdddbdcadadcdccabaabbccbacddddcbcdbabaadaddbbdabadaccbdbaaabdadadaadabbdbadacacbdcbcbdccc...

output:

134272327

result:

ok single line: '134272327'

Test #8:

score: 30
Accepted
time: 874ms
memory: 847304kb

input:

1 10000
bbcbcaacaaaacacbacacbaacbacbaababaacacbacbacbccaacbbacbbccababcaaaccabaaccbbbaaaabbababaacbbccabcaccaaccbacccaacabcaacacaccababbbbbcbacaaabbcbaccacaaaaabbacbbcbbcabccaabcabcabaabbbabbababbabcacbcaccabacaababbacccabbcaaacacccbcbcccbbbbaababbabcaaabaccccbbbabccabacbcbabccccaccbcccccbcccccccbbc...

output:

1375114968

result:

ok single line: '1375114968'

Test #9:

score: 30
Accepted
time: 864ms
memory: 846660kb

input:

1 10000
cbaacaaaaccacaacbbcacabccbbabbababbababbacbababbcabcbacabbaaaacccacbbaaacacbacccbababaabccbbcabbbcaacbcbbbcbcbccccbabcacccccaabbbccacccacabccbcccbaaaccbbbacbaabbcbaabccccaabacabccaaccbbbaccbbcbcaabcaacbcbbcacbcbcabbacbababcbcabcbaabbbbbcccccbcacbabbcccccaacccbaccbaacbccbcababbabcbbccbbbbacab...

output:

1363955024

result:

ok single line: '1363955024'

Test #10:

score: 30
Accepted
time: 870ms
memory: 845036kb

input:

1 10000
aacbabccacabcccbccbbcacbbcbcacbbcacbbcbcacacbaaabbbbccaabcbbccccaccccbbcbbcacbbcbbcacbbcbcacacbaaabbbbccaabcbbccccaccccbbcbcbbccccaccccbbbccccaccccbbcbcbbccccaccccbbcbaaccbbbbaaaaabbbbccaabcbbccccaccccbbcbcbbccccaccccbbbccccaccccbbcbcbbccccaccccbbcbaaccbbbbaaabcacacbcbbcacbbcacacbaaabbbbccaa...

output:

1951994915

result:

ok single line: '1951994915'

Test #11:

score: 30
Accepted
time: 1175ms
memory: 845660kb

input:

1 10000
bbaabbaaaabbbaaabbbaabaabaababbbbbabaabbababaaabbbbbbabbaaaabbbbbbaabababbbbabbaababbbbabaabaabaaabbabababbbbbaabababbaabbabbbababbbabaaaaabaaaaaaabaabaaabababbbabbabbaaaabababbbbabaabbaaababbaabbbbabbabbabbabbabaababaaaabbbaabbaababbabbbbaaaaaaaaaababbbbabbbabbaabbbbaaabaabbbbbaabbaababbaba...

output:

424739578

result:

ok single line: '424739578'

Subtask #3:

score: 20
Accepted

Test #12:

score: 20
Accepted
time: 657ms
memory: 805620kb

input:

100 1000
abbabbbbabbbbabaabaabbbbabaaabbbaabaabbabaabaabbaabbaabbababbabbababbbaabaabbaabbbabaabbbaaaabaaaabaababbaaaaaaaaabaabbaababbbbbbabbbabbbbababbbaaaaaabaaaabaaabbabbaaaaabbbabbabaabbabababbaaababbbaaaabbabbbabbabbabbbbbbabaabaabbbaabaaabbaabbabbbbaabbabaaabaabaabbbabbbbbabaababbbabababaabbba...

output:

1289287

result:

ok single line: '1289287'

Test #13:

score: 20
Accepted
time: 720ms
memory: 815864kb

input:

1000 1000
babbbabaabaaaabbbbbababaaabbaabbbbbabbaababbbbaaabbbbaaabbbababbaaaabaaabbbbbbbbaabbabbbabbabbaaabaabbbbbbbababaabbbbabaaabbaaaaabbbaababbaaaaabbbbaaaabbbaabaaababaaabbabbbabbabbbabbaabbabbbabaaaaaabaabbabbababbbbbabaaabbbbababbbaabbbaaabbababaababbbbbaaabbbabbabaaaababbbaabaaaabbbabaaabbb...

output:

10431998

result:

ok single line: '10431998'

Test #14:

score: 20
Accepted
time: 1286ms
memory: 811464kb

input:

1000 10000
babbababbbaaaaaaaaababbabbbababbbbbbbbabaaaaaabbaababbbbbbbaaaabbaaaaabbbbbbbababbabaaaaabbaaabaaaaaaabbbabbabbaaabbbabbbbababababbabbababbabbbbaabbbbaababbaababbababbbaaabbbbaaabaaaabababbbbaabaababbabbbaabbbbbabbbaaabaabaabbaabaaaaabbabbbbaaabbabbbaababbbaabaaaabbbababbbbbbbabababbaabab...

output:

94347164

result:

ok single line: '94347164'

Test #15:

score: 20
Accepted
time: 1754ms
memory: 896412kb

input:

10000 10000
bbbabbaababbbbbababbbbaabbabaaaababbabbbabababaaaaaababbbaabaabbababbbbbaa
b
aaabbbbbbabbaabbbbbbaabaaababbbabbabaaabababbababaaabbaabaabaabbbbabb
abbaabbaababbbabaabababaaaaaaaabaabbbb
bbbbbabbaabbaaaabaabaababbbababbbbbaaaabbabbbabbaabbbabaaababababaabbaaaabaabbaaabbbaaaaaababababbbabb...

output:

694099162

result:

ok single line: '694099162'

Test #16:

score: 20
Accepted
time: 611ms
memory: 805856kb

input:

100 100
ababababbbababbabbaabbbbbaabaaaaabbabaaaaabbbaabbbabababbbaaaaababbaabbbababaababbaabbaaababaabbaabbbbbbaaababbbaabaaaababbaababbbaaaabbbbaababaabaabaaabaaaabaababaaababbbbabbaabbbbababbaaabaaabaabaabaaaabaaaabbbbbabaaaabababaaababbbbbabbaaaabbbaaaaaaabababbaaababbbabbbbbaaaaaaaaaabbaabababa...

output:

138444

result:

ok single line: '138444'

Subtask #4:

score: 30
Accepted

Test #17:

score: 30
Accepted
time: 651ms
memory: 806100kb

input:

100 1000
bcbccabbbabacabaabbccbaaabbbbcbbbaaaacaaabbccbababbacbbabbbaccccccccbccabcaacbbccbbbbcaaacabcccaccbaacccbcbaaaccbcaaaaaaaabccaaaacccbabacbcababbacbbbcaabaaacbbcaccbaccccbbcbacabaacbcaccacbcbcbbcbccccbbcacccaabbabcaabbacabaaacccbbcbccbabbcbccbcbbbabbaabacbccbacababbacbababbbaacccacbabcabcaba...

output:

833103

result:

ok single line: '833103'

Test #18:

score: 30
Accepted
time: 630ms
memory: 811436kb

input:

1000 1000
cacccababcaaccbaaabcbcabcbbccbcbcbbcacbbcabcaaccabcccaacabccaacaaaababbbbbcbbcbcbbbcacbaabccaccbaacaaacabbbcbcbaabbcccacaababbbabcaabbaabaabaabaabccaacccbabcbaacccbbbccaacbbacacacaacccbcbcbaccbccaaacabaacbcbaacbcbacaccbacbbcaccbabbbabbbaccccbbbcacccccaacbbaaaccacabaaacaccabacacbaaaaacbacab...

output:

6757759

result:

ok single line: '6757759'

Test #19:

score: 30
Accepted
time: 960ms
memory: 811972kb

input:

1000 10000
cbacbccbcbbcccbbcccaacccbbbacbccaaaccabacaabacccbbbcbabcaacabbabbabcaaaabccbabcaaacaaabbbacccacabcacccaaababcacbaacaccbccbcaaaaaaaaccabbcaaaaabccacaacbccabacabcccaabbcaccbcabbccbabbbccaacbabcbacacabbaaaababbccbcbacbbbcaccaabacbcaabbaaacaacccaaabbbcabbbcccacacaccaacbccabccbcacbbbbbabcbabbc...

output:

61388196

result:

ok single line: '61388196'

Test #20:

score: 30
Accepted
time: 1176ms
memory: 852068kb

input:

10000 10000
aababaaccbcbabaccbaccbcaaabbcbbbbacbcbababbccbccbbac
cbcccccacaccabcbbbaaabcaaccabccccccbabacbbabbaaacbabccbcaacacbbbabbbbcbbcbcbaaccaccbabccabbbbbabcabcbcccababbcabaccbbccbaaacaabacbcbcbbabbcbaabcaccaaaccbbcacaaabcabacabccbaabbacabccacbabbaababb
cacbcbabc
caacccaabaaccbbbabaababbbbcbcac...

output:

462062051

result:

ok single line: '462062051'

Test #21:

score: 30
Accepted
time: 544ms
memory: 802440kb

input:

100 100
abcababbbbbcaccccbcacacabbccccbbccacbbabacabbbbacacaacaaabbcaabcaaaaabaaccbaaacbbcbbcabbaaababacabcccaabbcbbbbaaaacaabbbacbcabacbbccbbacacbaaabacabbcbbcaabaccababccaaababbcbcacbabababcbcccccccababcbbbaaabbacbaabbaababcabaacbccacbbaccbbbbcbbabbccaacbaccaaabaccbaacaaaaabbcaccbbbaaccaaccccbbbaa...

output:

90325

result:

ok single line: '90325'

Test #22:

score: 30
Accepted
time: 483ms
memory: 735952kb

input:

430 800
aaaccaccaacbcccbccbccaaaaaabccabbcbccabcacbcaabcbacbccbbacccaccabccacbbcccccbacaacbabbbaaaaacbbbcabacbccaabcabcbacccacbabaacacbcaacbabbabbbbbaacacabbaccaabcccbacbbabccccccbabbaaaaababcababaabcaabbcbbaaccaccabbaabcabccbbcbacacaaabcbaaccccbcbacbccacaaacbbaabaabccabaacbccccbbbbcabccbbcbbcbccaba...

output:

157989035

result:

ok single line: '157989035'

Test #23:

score: 30
Accepted
time: 825ms
memory: 864440kb

input:

400 400
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

40039936

result:

ok single line: '40039936'

Test #24:

score: 30
Accepted
time: 3789ms
memory: 1654348kb

input:

400 400
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbabaaaaabbbbaabbb...

output:

108484268

result:

ok single line: '108484268'