QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#468633#8340. 3 Sumreal_sigma_team#TL 428ms45544kbC++232.6kb2024-07-08 21:54:272024-07-08 21:54:28

Judging History

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

  • [2024-09-20 10:20:30]
  • hack成功,自动添加数据
  • (/hack/848)
  • [2024-07-08 21:54:28]
  • 评测
  • 测评结果:TL
  • 用时:428ms
  • 内存:45544kb
  • [2024-07-08 21:54:27]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

using ll = long long;

vector<int> operator+(vector<int> a, vector<int> b) {
    int d = 0;
    int sz = max(a.size(), b.size());
    a.resize(sz, 0);
    b.resize(sz, 0);
    for (int i = 0; i < sz; ++i) {
        a[i] += b[i] + d;
        d = a[i] / 10;
        a[i] %= 10;
    }
    if (d) a.push_back(d);
    while (a.size() && a.back() == 0) a.pop_back();
    return a;
}
uint64_t mod = (1ll << 61) - 1;
uint64_t base = 37;

uint64_t add(uint64_t a, uint64_t b) {
    return a + b >= mod ? a + b - mod : a + b;
}

uint64_t sub(uint64_t a, uint64_t b) {
    return a >= b ? a - b : a + mod - b;
}

uint64_t mul(uint64_t a, uint64_t b) {
    return (__int128)a * b % mod;
}

bool lss(vector<int> a, vector<int> b) {
    if (a.size() != b.size()) return a.size() < b.size();
    for (int i = (int)a.size() - 1; i >= 0; --i) {
        if (a[i] != b[i]) return a[i] < b[i];
    }
    return false;
}

uint64_t nhash(vector<int> a) {
    uint64_t pw = 1;
    uint64_t hsh = 0;
    for (auto i : a) {
        hsh = add(hsh, mul(i, pw));
        pw = mul(pw, base);
    }
    return hsh;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n, K;
    cin >> n >> K;
    vector<vector<int>> num(n);
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        reverse(s.begin(), s.end());
        vector<int> a(s.size());
        for (int j = 0; j < s.size(); ++j) a[j] = s[j] - '0';
        while (a.size() > K) {
            vector<int> b;
            for (int j = 0; j < a.size(); j += K) {
                b = b + vector(a.begin() + j, a.begin() + min((int)a.size(), j + K));
            }
            a = b;
        }
        if (*min_element(a.begin(), a.end()) == 9) a = {};
        while (a.size() && a.back() == 0) a.pop_back();
        num[i] = a;
    }
    vector<int> MOD(K, 9);
    auto hmod = nhash(MOD);
    sort(num.begin(), num.end(), lss);
    vector<int> ha(n);
    for (int i = 0; i < n; ++i) ha[i] = nhash(num[i]);
    map<uint64_t, int> cnt;
    int ret = 0;
    for (int i = 0; i < n; ++i) {
        auto hha = ha[i];
        if (num[i].size()) hha = sub(hmod, hha);
        cnt[hha]++;
        int l = i - 1, r = n;
        while (r - l > 1) {
            int m = (l + r) / 2;
            if (lss(num[i] + num[m], MOD)) l = m;
            else r = m;
        }
        for (int j = i; j < n; ++j) {
            uint64_t hh = add(ha[i], ha[j]);
            if (j > l) hh = sub(hh, hmod);
            ret += cnt[hh];
        }
    }
    cout << ret;
}

詳細信息

Test #1:

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

input:

4 1
0
1
10
17

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 53ms
memory: 13248kb

input:

500 859
7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 428ms
memory: 45544kb

input:

500 17336
11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: -100
Time Limit Exceeded

input:

500 1
751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...

output:

2327631

result: