QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#578654#8340. 3 SumPoetryTL 151ms28216kbC++236.7kb2024-09-20 20:39:442024-09-20 20:39:44

Judging History

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

  • [2024-09-20 20:39:44]
  • 评测
  • 测评结果:TL
  • 用时:151ms
  • 内存:28216kb
  • [2024-09-20 20:39:44]
  • 提交

answer

#include <bits/stdc++.h>
#include <bits/extc++.h>

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

struct B : std::vector<u64> {
    static const short bit = 8;
    static const u64 base = 1E8;

    B (u64 n = 0) {
        (*this) = n;
    }
    B (const std::string &str) {
        (*this) = str;
    }
    B & format() {
        while (!empty() && !back()) pop_back();
        if (empty()) return (*this);
        for (int i = 1; i < size(); ++i) {
            (*this)[i] += (*this)[i - 1] / base;
            (*this)[i - 1] %= base;
        }
        while (back() >= base) {
            emplace_back(back() / base);
            (*this)[size() - 2] %= base;
        }
        while (size() > 1 && back() == 0) pop_back();
        return (*this);
    }
    B operator = (u64 n) {
        clear();
        emplace_back(n);
        format();
        return (*this);
    }
    B operator = (const std::string &str) {
        clear();
        if (str.empty()) emplace_back(0);
        for (int i = (int)str.length() - 1; i >= 0; i -= bit) {
            u64 tmp = 0;
            for (int j = std::max(i - bit + 1, 0); j <= i; ++j)
                tmp = (tmp << 3) + (tmp << 1) + (str[j] & 15);
            emplace_back(tmp);
        }
        return (*this);
    }
    friend std::istream &operator>>(std::istream &is, B &tmp) {
        std::string str;
        if (!(is >> str)) return is;
        tmp = str;
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const B & tmp) {
        if (tmp.empty()) os << 0;
        else os << tmp[tmp.size() - 1];
        for (int i = (int)tmp.size() - 2; i >= 0; --i)
            os << std::setfill('0') << std::setw(bit) << tmp[i];
        return os;
    }
    friend bool operator != (const B &lhs, const B &rhs) {
        if (lhs.size() != rhs.size()) return true;
        for (int i = 0; i < lhs.size(); ++i) {
            if (lhs[i] != rhs[i]) {
                return true;
            }
        }
        return false;
    }
    friend bool operator == (const B &lhs, const B &rhs) {
        return !(lhs != rhs);
    }
    friend bool operator < (const B &lhs, const B &rhs) {
        if (lhs.size() != rhs.size()) return lhs.size() < rhs.size();
        for (int i = (int)lhs.size() - 1; i >= 0; --i)
            if (lhs[i] != rhs[i]) return lhs[i] < rhs[i];
        return false;
    }
    friend bool operator >= (const B &lhs, const B &rhs) {
        return !(lhs < rhs);
    }
    friend bool operator > (const B &lhs, const B &rhs) {
        return rhs < lhs;
    }
    friend bool operator <= (const B &lhs, const B &rhs) {
        return !(lhs > rhs);
    }
    friend B & operator += (B &lhs, const B &rhs) {
        if (lhs.size() < rhs.size()) lhs.resize(rhs.size());
        for (int i = 0; i < rhs.size(); ++i) {
            lhs[i] += rhs[i];
        }
        return lhs.format();
    }
    friend B operator + (B lhs, const B &rhs) {
        return lhs += rhs;
    }
    friend B & operator++ (B &res) {
        return res += 1;
    }
    friend B operator++ (B &res, int) {
        B old = res;
        ++res;
        return old;
    }
    friend B & operator -= (B &lhs, B rhs) { // abs(lhs - rhs)
        if (lhs < rhs) {
            B t = lhs;
            lhs = rhs;
            rhs = t;
        }
        for (int i = 0; i < rhs.size(); lhs[i] -= rhs[i], ++i) {
            if (lhs[i] < rhs[i]) {
                int j = i + 1;
                while (!lhs[j]) ++j;
                while (j > i) {
                    lhs[j--]--;
                    lhs[j] += base;
                }
            }
        }
        return lhs.format();
    }
    friend B operator - (B lhs, const B &rhs) {
        return lhs -= rhs;
    }
    friend B & operator -- (B &res) {
        return res -= 1;
    }
    friend B operator -- (B &res, int) {
        B old = res;
        --res;
        return old;
    }
    friend B operator * (const B &lhs, const B &rhs) {
        B res;
        res.assign(lhs.size() + rhs.size() - 1, 0);
        for (int i = 0; i < lhs.size(); ++i) {
            for (int j = 0; j < rhs.size(); ++j)
                res[i + j] += lhs[i] * rhs[j];
        }
        return res.format();
    }
    friend B & operator *= (B &lhs, const B &rhs) {
        return lhs = lhs * rhs;
    }
    friend B operator / (B &lhs, const int &rhs) {
        B ans;
        ans.resize(lhs.size());
        u64 t = 0;
        for (int i = (int)lhs.size() - 1; ~i; --i) {
            t = t * lhs.base + lhs[i];
            ans[i] = t / rhs;
            t %= rhs;
        }
        return ans.format();
    }
    friend u64 operator % (B &lhs, const int &rhs) {
        u64 t = 0;
        for (int i = (int)lhs.size() - 1; ~i; --i) {
            t = t * lhs.base + lhs[i];
            t %= rhs;
        }
        return t;
    }
    friend B operator /= (B &lhs, const int &rhs) {
        lhs = lhs / rhs;
        return lhs;
    }
} ;

constexpr u64 base = 13333331;

u64 hash(const B &x) {
    if (x.empty()) {
        return 0;
    }

    u64 res = 0;
    for (int i = x.size() - 1; i >= 0; --i) {
        res = base * res + x[i];
    }
    return res;
}

int main() {
    // freopen("manzanita.in", "r", stdin);
    // freopen("manzanita.out", "w", stdout);

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;
    int k;
    std::cin >> k;

    // k = std::min(k, (int)2E3 + 2);

    B M = 1;
    for (int i = 1; i <= k; ++i) {
        M = M * 10;
    }
    M--;

    std::vector<B> a(n);
    for (int i = 0; i < n; ++i) {
        std::string s;
        std::cin >> s;

        int len = s.size();
        do {
            int pre = len;
            len = std::max(0, len - k);
            a[i] += s.substr(len, pre - len);
        } while (len > 0);

        while (a[i] >= M) {
            a[i] -= M;
        }
    }

    sort(a.begin(), a.end());

    std::vector<__gnu_pbds::gp_hash_table<u64, int>> vis(n);
    for (int i = n - 1; i >= 0; --i) {
        if (i + 1 < n) {
            vis[i] = vis[i + 1];
        }
        ++vis[i][hash(a[i])];
    }

    B now = 0;
    int ans = 0;
    for (int x = 0; x < 3; ++x) {
        for (int i = 0; i < n; ++i) {
            if (a[i] + a[i] > now) {
                break;
            }

            for (int j = i; j < n; ++j) {
                B sum = a[i] + a[j];
                if (sum > now) {
                    break;
                }
                ans += vis[j][hash(now - sum)];
            }
        }
        now += M;
    }

    std::cout << ans << "\n";

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3524kb

input:

4 1
0
1
10
17

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 151ms
memory: 28216kb

input:

500 859
7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Time Limit Exceeded

input:

500 17336
11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...

output:


result: