62272023-11-08 11:59:34Error42Forgó rulettkerékcpp17Wrong answer 35/100873ms81056 KiB
#include <iostream>
#include <map>
#include <vector>

using namespace std;

using ll = long long;

template <ll M>
struct modular {
    ll val;

    // n >= 0
    modular(const ll n) {
        if (n < M)
            val = n;
        else if (n < 2 * M)
            val = n - M;
        else
            val = n % M;
    }

    [[nodiscard]] modular operator +(const modular& rhs) const {
        return val + rhs.val;
    }

    [[nodiscard]] modular operator -(const modular& rhs) const {
        return val + M - rhs.val;
    }

    [[nodiscard]] modular operator *(const modular& rhs) const {
        return val * rhs.val;
    }

    // p >= 0
    [[nodiscard]] modular pow(const ll& p) const {
        if (p == 0)
            return 1;

        if (p % 2 == 0)
            return (*this * *this).pow(p / 2);
        else
            return *this * pow(p - 1);
    }

    [[nodiscard]] modular inv() const {
        return pow(M - 2);
    }

    [[nodiscard]] modular operator /(const modular& rhs) const {
        return *this * rhs.inv();
    }

    modular& operator +=(const modular& rhs) {
        return *this = *this + rhs;
    }

    modular& operator -=(const modular& rhs) {
        return *this = *this - rhs;
    }

    modular& operator *=(const modular& rhs) {
        return *this = *this * rhs;
    }

    modular& operator /=(const modular& rhs) {
        return *this = *this / rhs;
    }

    explicit operator ll() const {
        return val;
    }
};

template <ll M>
ostream& operator<<(ostream& os, const modular<M>& modular) {
    cout << modular.val;
    return os;
}

using mod = modular<1'000'000'007>;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n, m;
    cin >> n >> m;

    mod const fp = mod(31).pow(m - 1);

    map<ll, ll> hashes;
    ll ans = 0;

    for (int w = 0; w < n; w++) {
        string s;
        cin >> s;

        mod cur_hash = 0;

        for (char const& c : s) {
            cur_hash *= 31;
            cur_hash += c - 'a' + 1;
        }

        ans += hashes[ll(cur_hash)];

        hashes[ll(cur_hash)]++;

        for (int i = 0; i < m - 1; i++) {
            cur_hash -= fp * (s[i] - 'a' + 1);
            cur_hash *= 31;
            cur_hash += (s[i] - 'a' + 1);

            hashes[ll(cur_hash)]++;
        }
    }

    cout << ans << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Wrong answer2ms2052 KiB
subtask220/20
3Accepted3ms2280 KiB
4Accepted3ms2492 KiB
5Accepted3ms2628 KiB
6Accepted4ms2924 KiB
7Accepted6ms4160 KiB
8Accepted4ms3444 KiB
9Accepted4ms3776 KiB
subtask315/15
10Accepted54ms5376 KiB
11Accepted68ms7392 KiB
12Accepted103ms12620 KiB
13Accepted61ms7052 KiB
14Accepted57ms7884 KiB
15Accepted212ms37592 KiB
16Accepted280ms49104 KiB
17Accepted244ms43228 KiB
subtask40/65
18Accepted219ms15912 KiB
19Accepted224ms10632 KiB
20Accepted632ms59832 KiB
21Wrong answer638ms54488 KiB
22Accepted331ms32628 KiB
23Accepted229ms11788 KiB
24Accepted499ms48404 KiB
25Accepted351ms25048 KiB
26Accepted150ms6292 KiB
27Wrong answer381ms33100 KiB
28Accepted873ms81056 KiB
29Accepted112ms12232 KiB
30Wrong answer463ms48416 KiB