QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#179194 | #6891. String and GCD | PPP# | AC ✓ | 12254ms | 248972kb | C++17 | 1.9kb | 2023-09-14 19:24:07 | 2023-09-14 19:24:08 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> prefix_function (string s) {
int n = (int) s.length();
vector<int> pi (n);
for (int i=1; i<n; ++i) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j]) ++j;
pi[i] = j;
}
return pi;
}
const int maxN = 1e6 + 10;
vector<int> dv[maxN];
int phi[maxN];
vector<int> g[maxN];
string s;
int val[maxN];
ll ans[maxN];
void dfs(int v) {
if (v != 0) {
ans[v] = 0;
for (int to : dv[v]) {
ans[v] += phi[to] * val[to];
}
for (int to : dv[v]) {
val[to]++;
}
}
for (int to : g[v]) {
dfs(to);
}
if (v != 0) {
for (int to : dv[v]) {
val[to]--;
}
}
}
const ll mod = 998244353;
void solve() {
cin >> s;
auto pi = prefix_function(s);
for (int i = 1; i <= s.size(); i++) {
g[i].clear();
}
for (int i = 1; i <= s.size(); i++) {
val[i] = 0;
g[pi[i - 1]].emplace_back(i);
}
dfs(0);
ll coef = 1;
for (int i = 1; i <= s.size(); i++) {
// cout << ans[i] << " " << i << " ??? " << endl;
coef = (1LL * coef * ((ans[i] + 1) % mod)) % mod;
}
cout << coef << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
for (int i = 1; i < maxN; i++) {
phi[i] = i;
}
for (int i = 1; i < maxN; i++) {
dv[i].emplace_back(i);
for (int j = 2 * i; j < maxN; j += i) {
phi[j] -= phi[i];
dv[j].emplace_back(i);
}
}
int tst;
cin >> tst;
while (tst--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 12254ms
memory: 248972kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
348588201 444875250 645157594 69295643 479125431 754799284 363702774 250095339 288773558 250829868
result:
ok 10 lines