QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#856811#9970. Looping RPSIllusionaryDominance#WA 121ms54324kbC++203.1kb2025-01-14 17:01:202025-01-14 17:01:21

Judging History

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

  • [2025-01-14 17:01:21]
  • 评测
  • 测评结果:WA
  • 用时:121ms
  • 内存:54324kb
  • [2025-01-14 17:01:20]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef __int128_t i128;

const int MAX_M = 1000000 + 5;
const int Base = 5;
const int P = 1e9 + 9;

int pw[MAX_M], pw2[MAX_M], ipw[MAX_M];

inline ll qpow(ll n) {return 1ll * pw2[n / (MAX_M - 1)] * pw[n % (MAX_M - 1)];}

int N, cnt[MAX_M], tot;
string str[MAX_M];
struct TrieNode{
    int sz, sum, son[3];
}node[MAX_M];
struct Hash{
    int n, ha[MAX_M], temp[MAX_M];
    
    void init(const string &s) {
        n = s.size();
        for (int i = 1; i <= n; i ++) {
            ha[i] = (1ll * ha[i - 1] * Base + s[i - 1]) % P;
            temp[i] = 1ll * ha[i] * ipw[i] % P;
        }
    }
    
    ll query(int pref, ll len) {
        ll k = len / pref, r = len % pref;
        // pw[r] * (k times pref) + ha[r]
        // k times pref : ha[pref] * (pw[k*pref] - 1) * ipw[pref]
        return ((qpow(k * pref) - 1) % P * temp[pref] % P * pw[r] + ha[r]) % P;
    }
}ha;
map <pair <ll, int>, int> status;
ll ans;

void ins(const string &s) {
    int u = 0, len = 0;
    for (auto x : s) {
        int y = x != 1, z = 3 - x - y;
        ans += 1ll * node[node[u].son[y]].sum * node[node[u].son[z]].sum;
        status[pair((ll)len, y)] = node[node[u].son[y]].sum;
        status[pair((ll)len, z)] = node[node[u].son[z]].sum;
        int &v = node[u].son[x]; v = v ? v : ++ tot;
        cnt[++ len] = node[v].sz; node[u = v].sum ++;
    }
    node[u].sz ++;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    static int Map[128];
    Map['K'] = 0;
    Map['N'] = 1;
    Map['P'] = 2;
    pw[0] = 1; pw2[0] = 1;
    auto power = [&](int a, int n) -> int {
        int ans = 1;
        while (n) {
            if (n & 1) ans = 1ll * ans * a % P;
            a = 1ll * a * a % P; n >>= 1;
        }
        return ans;
    };
    for (int i = 1; i < MAX_M; i ++) {
        pw[i] = pw[i - 1] * Base % P;
        ipw[i] = power(pw[i] - 1, P - 2);
    }
    for (int i = 1; i < MAX_M; i ++) {
        pw2[i] = 1ll * pw2[i - 1] * pw[MAX_M - 1] % P;
    }
    
    cin >> N;
    for (int i = 1; i <= N; i ++) {
        cin >> str[i];
        for (auto &x : str[i]) x = Map[x];
    }
    sort(str + 1, str + N + 1, [&](const string &i, const string &j) -> bool {return i.size() < j.size();});
    for (int i = 1; i <= N; i ++) {
        status.clear();
        ins(str[i]);
        ha.init(str[i]);
        for (int j = 1; j < str[i].size(); j ++) {
            ll lim = 1ll * j * str[i].size() / __gcd(j, (int)str[i].size());
            ll l = j + 1, r = lim + 1;
            while (l < r) {
                ll mid = l + r >> 1;
                if (ha.query(j, mid) == ha.query(str[i].size(), mid)) l = mid + 1;
                else r = mid;
            }
            l --;
            if (l >= lim) continue;
            int x = str[i][l % str[i].size()], y = str[i][l % j], z = 3 - x - y;
            ans += 1ll * status[pair(l, z)] * cnt[j];
            status[pair(l, y)] += cnt[j];
        }
    }
    cout << ans << '\n';
    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 120ms
memory: 54324kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: -100
Wrong Answer
time: 121ms
memory: 51024kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

2

result:

wrong answer 1st numbers differ - expected: '3', found: '2'