QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#179194#6891. String and GCDPPP#AC ✓12254ms248972kbC++171.9kb2023-09-14 19:24:072023-09-14 19:24:08

Judging History

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

  • [2023-09-14 19:24:08]
  • 评测
  • 测评结果:AC
  • 用时:12254ms
  • 内存:248972kb
  • [2023-09-14 19:24:07]
  • 提交

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