QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#211778 | #5051. Namomo Subsequence | bigJ | TL | 17ms | 18648kb | C++20 | 3.5kb | 2023-10-12 21:09:35 | 2023-10-12 21:09:35 |
Judging History
answer
#include <bits/stdc++.h>
#define dug(x) cerr << "死了吧小丑" << x << endl
using namespace std;
using i64 = long long;
template <class T> bool chmax(T &a, T b)
{
a > b ? (a = a) : (a = b);
return a == b;
}
template <class T> bool chmin(T &a, T b)
{
a > b ? (a = b) : (a = a);
return a == b;
}
const int M = 998244353, N = 1e6 + 10;
struct Comb
{
#define ll long long
ll n, p;
vector<ll> fac, inv; // 阶乘 逆元
long long qmi(long long k, long long p, long long M)
{
long long res = 1;
while (p)
{
if (p & 1)
res = res * k % M;
k = k * k % M;
p >>= 1;
}
return res;
}
Comb()
{
}
Comb(int a, int b)
{
init(a, b);
}
void init(int a, int b)
{
n = a, p = b;
fac.resize(n + 1), inv.resize(n + 1);
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
inv[n] = qmi(fac[n], p - 2, p);
for (int i = n; i; i--)
inv[i - 1] = inv[i] * i % p;
}
int C(int n, int m) // 在n里选m
{
if (n < m || m < 0)
return 0;
return (fac[n] * inv[m] % p) * inv[n - m] % p;
}
int lucas(int n, int m) // 要为质数
{
if (n < m || m < 0)
return 0;
if (m == 0)
return 1;
return 1ll * C(n % p, m % p) * lucas(n / p, m / p) % p;
}
};
Comb co(N, M);
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(15) << fixed;
int yi[128];
vector<char> tm(62);
for (int i = 0; i < 26; i++)
{
yi[i + 'A'] = i;
tm[i] = i + 'A';
yi[i + 'a'] = i + 26;
tm[i + 26] = i + 'a';
}
for (int i = 0; i < 10; i++)
{
yi[i + '0'] = i + 52;
tm[i + 52] = i + '0';
}
string a;
cin >> a;
int n = a.size();
vector<array<i64, 62>> s(n + 1);
fill(s[0].begin(), s[0].end(), 0);
vector<vector<int>> mp(62);
for (int i = 1; i <= n; i++)
{
s[i] = s[i - 1];
s[i][yi[a[i - 1]]]++;
mp[yi[a[i - 1]]].push_back(i);
}
vector<int> sa(n + 1, 0);
for (int i = 1; i <= n; i++)
{
sa[i] = sa[i - 1];
sa[i] += s[i - 1][yi[a[i - 1]]];
sa[i] %= M;
}
i64 ans = 0;
for (int i = 0; i < 62; i++)
{
if (mp[i].size() <= 1)
continue;
for (int j = 0; j < 62; j++)
{
if (i == j)
continue;
i64 f1 = 0, f2 = 0, dp1 = 0, dp2 = 0, res;
f2 = s[n][j] - s[mp[i].back()][j];
// if (tm[i] == 'f' && tm[j] == 'c')
// {
// cout << f2 << endl;
// }
for (int z = mp[i].size() - 2; z >= 0; z--)
{
dp1 = (dp2 + (s[mp[i][z + 1]][j] - s[mp[i][z]][j]) * f2) % M;
f1 = ((s[n][j] - s[mp[i][z]][j]) + f2) % M;
i64 id = mp[i][z] - 1;
if (id == 1)
continue;
res = co.C(id, 2);
i64 s1 = s[id][j], s2 = s[id][i];
res = (res + M - sa[id] - s1 * s2 % M - (s1 + s2) * (id - s1 - s2) % M) % M;
ans = (ans + res * dp1) % M;
swap(f1, f2), swap(dp1, dp2);
}
}
}
cout << ans << endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 13ms
memory: 18640kb
input:
wohaha
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 13ms
memory: 18632kb
input:
momomo
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 10ms
memory: 18548kb
input:
gshfd1jkhaRaadfglkjerVcvuy0gf
output:
73
result:
ok 1 number(s): "73"
Test #4:
score: 0
Accepted
time: 17ms
memory: 18648kb
input:
retiredMiFaFa0v0
output:
33
result:
ok 1 number(s): "33"
Test #5:
score: -100
Time Limit Exceeded
input:
bcdccccacccabdbdaddcabddbaccaaaaaabaccbbbcbbddabcbccabacdacbcbabccbcbddcdcbcaaadddddccdbabaabcbbcaaadadacdaadbdccbddddabcbaaddbcadadcbcbaaccacabdababaabdccadaddacdcacdaabbadadaddbbcccbcddaccaadbbcaaccccdcacbdbdddbaccaacbcaccaaabccdadddbaabdbcaaccacdcdcbcdddacbcacdbbbdccdddccccabdbacddacbaacbbcaccdcd...