QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#177738 | #7187. Hardcore String Counting | 怎么有退役选手还参加这个的 (Yaoyu Pan) | AC ✓ | 969ms | 17180kb | C++20 | 9.2kb | 2023-09-13 12:08:23 | 2023-09-13 12:08:24 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
using LL = long long;
const int maxn = 2e6 + 5, mod = 998244353, G = 3, Gi = 332748118;
int qpow(int a, int b, int mod){
int res = 1;
while (b){
if (b & 1) res = 1LL * res * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return res;
}
inline int mul(int a, int b){
return 1LL * a * b % mod;
}
inline void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
}
inline void sub(int &a, int b){
a -= b;
if (a < 0) a += mod;
}
int inv[(1 << 21) + 5], fact[(1 << 21) + 5], invfact[(1 << 21) + 5];
void init(int n){
inv[1] = 1;
for(int i = 2; i <= n; i++)
inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
fact[0] = invfact[0] = 1;
for(int i = 1; i <= n; i++)
fact[i] = mul(fact[i - 1], i);
invfact[n] = qpow(fact[n], mod - 2, mod);
for(int i = n - 1; i >= 1; i--)
invfact[i] = mul(invfact[i + 1], i + 1);
}
int C(int a, int b){
if (a < 0 || b < 0 || a < b) return 0;
return mul(mul(fact[a], invfact[b]), invfact[a - b]);
}
namespace NTT{
vector<int> Omega(int L){
int wn = qpow(G, mod / L, mod);
vector<int> w(L);
w[L >> 1] = 1;
for(int i = L / 2 + 1; i < L; i++) w[i] = mul(w[i - 1], wn);
for(int i = L / 2 - 1; i >= 1; i--) w[i] = w[i << 1];
return w;
}
auto W = Omega(1 << 21);
void DIF(int *a, int n) {
for(int k = n >> 1; k; k >>= 1)
for(int i = 0, y; i < n; i += k << 1)
for(int j = 0; j < k; j++){
y = a[i + j + k], a[i + j + k] = mul(a[i + j] - y + mod, W[k + j]),
add(a[i + j], y);
}
}
void IDIT(int *a, int n) {
for (int k = 1; k < n; k <<= 1)
for (int i = 0, x, y; i < n; i += k << 1)
for(int j = 0; j < k; j++){
x = a[i + j], y = mul(a[i + j + k], W[k + j]),
a[i + j + k] = x - y < 0 ? x - y + mod : x - y;
add(a[i + j], y);
}
int inv = mod - (mod - 1) / n;
for(int i = 0; i < n; i++) a[i] = mul(a[i], inv);
reverse(a + 1, a + n);
}
}
using Poly = std::vector<int>;
void DFT(Poly &a){
NTT::DIF(a.data(), a.size());
}
void IDFT(Poly &a){
NTT::IDIT(a.data(), a.size());
}
int normed(int n) {
return n == 1 ? 1 : (1 << (32 - __builtin_clz(n - 1)));
}
void norm(Poly &a) {
if (!a.empty()) a.resize(normed((int)a.size()), 0);
}
void dot(Poly &a, Poly &b) {
for(int i = 0; i < a.size(); i++)
a[i] = mul(a[i], b[i]);
}
Poly operator*(Poly a, Poly b) {
int n = a.size() + b.size() - 1, L = normed(n);
if (a.size() <= 8 || b.size() <= 8) {
Poly c(n);
for(int i = 0; i < a.size(); i++)
for(int j = 0; j < b.size(); j++)
c[i + j] = (c[i + j] + 1LL * a[i] * b[j]) % mod;
return c;
}
a.resize(L), b.resize(L);
DFT(a), DFT(b), dot(a, b), IDFT(a);
return a.resize(n), a;
}
Poly Inv2k(Poly a) { // a.size() = 2^k
int n = a.size(), m = n >> 1;
if (n == 0) return {0};
if (n == 1) return {qpow(a[0], mod - 2, mod)};
Poly b = Inv2k(Poly(a.begin(), a.begin() + m)), c = b;
b.resize(n), DFT(a), DFT(b), dot(a, b), IDFT(a);
for(int i = 0; i < n; i++) a[i] = i < m ? 0 : mod - a[i];
DFT(a), dot(a, b), IDFT(a);
return move(c.begin(), c.end(), a.begin()), a;
}
Poly Inv(Poly a) {
int n = a.size();
norm(a), a = Inv2k(a);
return a.resize(n), a;
}
Poly &operator*=(Poly &a, int b){
for(auto &x : a) x = mul(x, b);
return a;
}
Poly operator*(Poly a, int b){
return a *= b;
}
Poly operator*(int a, Poly b){
return b * a;
}
Poly &operator/=(Poly &a, int b){
return a *= qpow(b, mod - 2, mod);
}
Poly operator/(Poly a, int b){
return a /= b;
}
Poly &operator+=(Poly &a, Poly b) {
a.resize(max(a.size(), b.size()));
for(int i = 0; i < b.size(); i++) add(a[i], b[i]);
return a;
}
Poly operator+(Poly a, Poly b){
return a += b;
}
Poly &operator-=(Poly &a, Poly b) {
a.resize(max(a.size(), b.size()));
for(int i = 0; i < b.size(); i++) sub(a[i], b[i]);
return a;
}
Poly operator-(Poly a, Poly b) {
return a -= b;
}
Poly operator/(Poly a, Poly b){
int k = a.size() - b.size() + 1;
if (k < 0) return {0};
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
b.resize(k), a = a * Inv(b);
a.resize(k), reverse(a.begin(), a.end());
return a;
}
pair<Poly, Poly> operator%(Poly a, const Poly& b) {
Poly c = a / b;
a -= b * c, a.resize(b.size() - 1);
return {c, a};
}
Poly Sqrt(Poly a) { // a0 = 1
int n = a.size(), k = normed(n);
Poly b = {1}, c;
a.resize(k * 2, 0);
for (int L = 2; L <= k; L <<= 1) {
b.resize(2 * L, 0), c = Poly(a.begin(), a.begin() + L) * Inv(b);
for(int i = 0; i < 2 * L - 1; i++) b[i] = mul(b[i] + c[i], (mod + 1) / 2);
}
return b.resize(n), b;
}
void Derivative(Poly &a) {
for(int i = 1; i < a.size(); i++)
a[i - 1] = mul(i, a[i]);
a.pop_back();
}
void Integral(Poly &a) {
for(int i = a.size() - 1; i >= 1; i--)
a[i] = mul(inv[i], a[i - 1]);
a[0] = 0;
}
Poly Ln(Poly a){
int n = a.size();
Poly b = a;
Derivative(b);
a = b * Inv(a);
a.resize(n);
Integral(a);
return a;
}
Poly Exp2k(Poly a){ // a.size() = 2^k
int n = a.size(), m = n >> 1;
if (n == 0) return {0};
if (n == 1) return Poly(1, 1);
Poly b = Exp2k(Poly(a.begin(), a.begin() + m));
b.resize(n);
Poly c = Ln(b);
for(int i = 0; i < n; i++)
c[i] = (a[i] - c[i] + mod) % mod;
c[0]++;
b = b * c;
return b.resize(n), b;
}
Poly Exp(Poly a) { // a0 = 0
int n = a.size();
norm(a), a = Exp2k(a);
return a.resize(n), a;
}
Poly Pow(Poly a, int k){ // a0 = 1, k % mod not mod - 1
return Exp(Ln(a) * k);
}
// D&C NTT
Poly MulAll(int l, int r, vector<Poly> &a){
if (l == r) return a[r];
int mid = (l + r) / 2;
return MulAll(l, mid, a) * MulAll(mid + 1, r, a);
}
Poly Evaluate(const Poly &f, const vector<int> &x) {
int n = x.size();
if (n == 0) return {0};
vector<Poly> up(n * 2);
for (int i = 0; i < n; ++i) {
up[i + n] = Poly{(mod - x[i]) % mod, 1};
}
for (int i = n - 1; i; --i) {
up[i] = up[i << 1] * up[i << 1 | 1];
}
vector<Poly> down(n * 2);
down[1] = (f % up[1]).second;
for (int i = 2; i < n * 2; ++i)
down[i] = (down[i >> 1] % up[i]).second;
Poly y(n);
for (int i = 0; i < n; ++i)
y[i] = down[i + n][0];
return y;
}
Poly Interpolate(const vector<int> &x, const vector<int> &y) {
int n = x.size();
vector<Poly> up(n * 2);
for (int i = 0; i < n; ++i)
up[i + n] = Poly{(mod - x[i]) % mod, 1};
for (int i = n - 1; i; --i)
up[i] = up[i << 1] * up[i << 1 | 1];
Derivative(up[1]);
Poly a = Evaluate(up[1], x);
for (int i = 0; i < n; ++i)
a[i] = mul(y[i], qpow(a[i], mod - 2, mod));
vector<Poly> down(n * 2);
for (int i = 0; i < n; ++i)
down[i + n] = Poly(1, a[i]);
for (int i = n - 1; i; --i)
down[i] = down[i << 1] * up[i << 1 | 1] + down[i << 1 | 1] * up[i << 1];
return down[1];
}
int divAt(Poly f, Poly g, int64_t k) {
int n = int(max(f.size(), g.size())), m = 1 << __lg(n * 2 - 1);
f.resize(n), g.resize(n);
for (; k; k >>= 1) {
f.resize(m * 2), g.resize(m * 2);
DFT(f), DFT(g);
for (int i = 0; i < m * 2; i++) f[i] = mul(f[i], g[i ^ 1]);
for (int i = 0; i < m; i++) g[i] = mul(g[i * 2], g[i * 2 + 1]);
g.resize(m);
IDFT(f), IDFT(g);
for (int i = 0, j = k & 1; i < n; i++, j += 2) f[i] = f[j];
f.resize(n), g.resize(n);
}
return f[0];
}
int kth_term_of_linearly_recurrent_sequence(Poly a, Poly f, LL k){
if (k < a.size()) return a[k];
f[0] = mod - 1;
a = a * f, a.resize(f.size()), a.back() = 0;
return divAt(a, f, k);
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
string s;
cin >> s;
s = " " + s;
vector<int> fail(n + 1);
for(int i = 2, j = 0; i <= n; i++){
while(j && s[j + 1] != s[i]) j = fail[j];
if (s[j + 1] == s[i]) j++;
fail[i] = j;
}
Poly f(n + 1);
add(f[1], 26);
sub(f[n], 1);
vector<int> border;
for(int i = fail[n]; i; i = fail[i]){
border.push_back(i);
}
for(auto p : border){
sub(f[n - p], 1);
add(f[n - p + 1], 26);
}
Poly a(n);
a[0] = 1;
for(int i = 1; i < n; i++)
a[i] = mul(a[i - 1], 26);
int ans = kth_term_of_linearly_recurrent_sequence(a, f, m - 1);
ans = mul(ans, 26);
sub(ans, kth_term_of_linearly_recurrent_sequence(a, f, m));
cout << ans << '\n';
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 11308kb
input:
6 7 aaaaaa
output:
25
result:
ok answer is '25'
Test #2:
score: 0
Accepted
time: 3ms
memory: 11336kb
input:
3 5 aba
output:
675
result:
ok answer is '675'
Test #3:
score: 0
Accepted
time: 8ms
memory: 11188kb
input:
1 1 a
output:
1
result:
ok answer is '1'
Test #4:
score: 0
Accepted
time: 4ms
memory: 11324kb
input:
5 7 ababa
output:
675
result:
ok answer is '675'
Test #5:
score: 0
Accepted
time: 3ms
memory: 11300kb
input:
1 3 a
output:
625
result:
ok answer is '625'
Test #6:
score: 0
Accepted
time: 3ms
memory: 11284kb
input:
10 536870912 njjnttnjjn
output:
826157401
result:
ok answer is '826157401'
Test #7:
score: 0
Accepted
time: 455ms
memory: 14640kb
input:
65535 536870912 aaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaaeaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaaeaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaaeaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaayaaaoaaaoaaaoaaaraaaoaaaoaaaoaaayaaaoaaaoaaao...
output:
996824286
result:
ok answer is '996824286'
Test #8:
score: 0
Accepted
time: 947ms
memory: 16856kb
input:
99892 536870912 wwwwbwwwwbwwwwqwwwwbwwwwbwwwwqwwwwbwwwwbwwwweewwwwbwwwwbwwwwqwwwwbwwwwbwwwwqwwwwbwwwwbwwwweewwwwbwwwwbwwwwqwwwwbwwwwbwwwwqwwwwbwwwwbwwwwawwwwbwwwwbwwwwqwwwwbwwwwbwwwwqwwwwbwwwwbwwwweewwwwbwwwwbwwwwqwwwwbwwwwbwwwwqwwwwbwwwwbwwwweewwwwbwwwwbwwwwqwwwwbwwwwbwwwwqwwwwbwwwwbwwwwawwwwbwwwwb...
output:
718505966
result:
ok answer is '718505966'
Test #9:
score: 0
Accepted
time: 953ms
memory: 16796kb
input:
100000 536870912 rrmrrqrrmrrcrrmrrqrrmrrbrrmrrqrrmrrcrrmrrqrrmrrnnrrmrrqrrmrrcrrmrrqrrmrrbrrmrrqrrmrrcrrmrrqrrmrrttrrmrrqrrmrrcrrmrrqrrmrrbrrmrrqrrmrrcrrmrrqrrmrrnnrrmrrqrrmrrcrrmrrqrrmrrbrrmrrqrrmrrcrrmrrqrrmrrarrmrrqrrmrrcrrmrrqrrmrrbrrmrrqrrmrrcrrmrrqrrmrrnnrrmrrqrrmrrcrrmrrqrrmrrbrrmrrqrrmrrcrrm...
output:
824845147
result:
ok answer is '824845147'
Test #10:
score: 0
Accepted
time: 967ms
memory: 16916kb
input:
99892 1000000000 ggggjggggjggggxggggjggggjggggxggggjggggjggggeeggggjggggjggggxggggjggggjggggxggggjggggjggggeeggggjggggjggggxggggjggggjggggxggggjggggjggggbggggjggggjggggxggggjggggjggggxggggjggggjggggeeggggjggggjggggxggggjggggjggggxggggjggggjggggeeggggjggggjggggxggggjggggjggggxggggjggggjggggbggggjgggg...
output:
971128221
result:
ok answer is '971128221'
Test #11:
score: 0
Accepted
time: 964ms
memory: 16780kb
input:
100000 625346716 kwfuguxrbiwlvyqsbujelgcafpsnxsgefwxqoeeiwoolreyxvaahagoibdrznebsgelthdzqwxcdglvbpawhdgaxpiyjglzhiamhtptsyyzyyhzjvnqfyqhnrtbwgeyotmltodidutmyvzfqfctnqugmrdtuyiyttgcsjeupuuygwqrzfibxhaefmbtzfhvopmtwwycopheuacgwibxlsjpupdmchvzneodwuzzteqlzlfizpleildqqpcuiechcwearxlvplatyrzxfochdfjqcmzt...
output:
0
result:
ok answer is '0'
Test #12:
score: 0
Accepted
time: 831ms
memory: 15784kb
input:
65536 35420792 pkmyknsqmhwuevibxjgrftrinkulizarxbkmgorddvuvtrhdadnlxfrxsyqhueuefdkanysaixmhbdqyskjdrzntlaqtwoscxldmyzahzwximvjgsjuddejbsbwtxgkbzfzdusucccohjwjuaasnkindxjjtxdbxmitcixrcmawdezafgnigghdtoyzazyfedzsuwsrlkdtarcmzqnszgnyiqvzamjtamvfrhzucdsfscyzdbvbxutwraktnmfrdfbejcbhjcgczgwiucklwydmuuozlu...
output:
0
result:
ok answer is '0'
Test #13:
score: 0
Accepted
time: 968ms
memory: 17180kb
input:
100000 1000000000 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn...
output:
545362217
result:
ok answer is '545362217'
Test #14:
score: 0
Accepted
time: 927ms
memory: 17172kb
input:
100000 536870911 ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...
output:
332737929
result:
ok answer is '332737929'
Test #15:
score: 0
Accepted
time: 938ms
memory: 16936kb
input:
100000 536870911 qodtwstdnykduvzvvvzmpawqaajvcdatuzzjisoezaqtvqhghmixvlfyhznvrlhdslyyhxoqchflfdjiefikpfrykekhjqywxpwmihiojcfzcmqelrkddbpkcnqcaopdyhldawyrvkqfbqpybewrtusifbfdtxiflxtkzdjqbocozdpupunehraytkhqnobhzeohkvbjyrdfebstqfjlvrcabimlybsnuaqgfcldvklwnyuywvfpdqwmortctexzaufmazyatybltglyonllufofiyr...
output:
592710827
result:
ok answer is '592710827'
Test #16:
score: 0
Accepted
time: 286ms
memory: 16812kb
input:
100000 100000 ciawhxojdqnivfonswbklnoocigwmkbjtkzahqgysihfdeqhialusobeeazqaqzryakqycapfswxpithldpuiflxzpgsysjwnpinfubqlyadphswzvzbrxcdbbhavtzkvwrcqecfnzawisgkvsopjnfzfnlecuesnffqzcknunwsxlrbvdzqbduypfrwgqqnrjstxgjaeuqxxajfbmidkwhrgkpjduftivfwnuugxomyznpbtbcstdkdaitvpdtuvyzipygztosvjwwdascbqthqdgkbit...
output:
1
result:
ok answer is '1'
Test #17:
score: 0
Accepted
time: 969ms
memory: 16788kb
input:
100000 1000000000 zujpixywgppdzqtwikoyhvlwqvxrfdylopuqgprrqpgqmgfkmhbucwkgdljyfzzbtaxxnltmbptwhknjjqlbeuiowdblqppqeeuunexkghdxjtbidlacmycgwvulgaeazyiwzedaxhtskacflodouylwxfjydzfbthotdwrfcpwrkcgnxpjsmkafaaojlctmqckabidgalvptziemzphncrgtqxlvllgwwgkoqxwhziuxvkadgaohdlceuggwwzmpywsgoecwwhhbotaleesjexdxg...
output:
879141501
result:
ok answer is '879141501'
Extra Test:
score: 0
Extra Test Passed