QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#441975#8340. 3 Sumucup-team045#WA 29ms3636kbC++205.7kb2024-06-15 01:02:562024-06-15 01:02:57

Judging History

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

  • [2024-09-20 10:20:30]
  • hack成功,自动添加数据
  • (/hack/848)
  • [2024-06-15 01:02:57]
  • 评测
  • 测评结果:WA
  • 用时:29ms
  • 内存:3636kb
  • [2024-06-15 01:02:56]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<random>
#include<array>
#include<algorithm>
#include<iomanip>
#include<set>
#include<map>
using namespace std;
using LL = long long;

int qpow(int a, int b, int mod){
    int res = 1;
    while (b){
        if (b & 1) res = 1LL * res * a % mod;
        a = 1LL * a * a % mod;
        b >>= 1;
    }
    return res;
}

const int BASE = 1e9, WIDTH = 9;
using BigInt = vector<int>;

BigInt operator+(const BigInt &a, const BigInt &b){
    BigInt c(max(a.size(), b.size()) + 1);
    for(int i = 0; i < (int)a.size(); i++){
        c[i] += a[i];
    }
    for(int i = 0; i < (int)b.size(); i++){
        c[i] += b[i];
    }
    for(int i = 0; i < (int)c.size(); i++){
        if (c[i] >= BASE){
            c[i + 1] += 1;
            c[i] -= BASE;
        }
    }
    while(c.size() > 1 && c.back() == 0){
        c.pop_back();
    }
    return c;
}

BigInt operator-(const BigInt &a, const BigInt &b){
    BigInt c(max(a.size(), b.size()));
    for(int i = 0; i < (int)a.size(); i++){
        c[i] += a[i];
    }
    for(int i = 0; i < (int)b.size(); i++){
        c[i] -= b[i];
    }
    for(int i = 0; i < (int)c.size(); i++){
        if (c[i] < 0){
            c[i + 1] -= 1;
            c[i] += BASE;
        }
    }
    while(c.size() > 1 && c.back() == 0){
        c.pop_back();
    }
    return c;
}

BigInt operator*(const BigInt &a, int b){
    BigInt c(a.size() + 1);
    long long t = 0;
    for(int i = 0; i < (int)a.size(); i++){
        t += 1LL * a[i] * b;
        c[i] = t % BASE;
        t /= BASE;
    }
    c.back() = t;
    while(c.size() > 1 && c.back() == 0){
        c.pop_back();
    }
    return c;
}

pair<BigInt, long long> operator/(const BigInt &a, int b){
    BigInt c;
    c.reserve(a.size());
    long long r = 0;
    for(int i = (int)a.size() - 1; i >= 0; i--){
        r = r * BASE + a[i];
        c.push_back(r / b);
        r %= b;
    }
    reverse(c.begin(), c.end());
    while(c.size() > 1 && c.back() == 0){
        c.pop_back();
    }
    return {c, r};
}

bool operator<(const BigInt &a, const BigInt &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;
}

BigInt str2int(const string &s){
    BigInt a;
    a.reserve(s.size() / WIDTH + 1);
    int base = 1, val = 0;
    for(int i = (int)s.size() - 1; i >= 0; i--){
        val += (s[i] - '0') * base;
        base *= 10;
        if (base == BASE){
            a.push_back(val);
            base = 1, val = 0;
        }
    }
    if (a.empty() || val) a.push_back(val);
    return a;
}

ostream &operator<<(ostream &os, const BigInt &A) { 
    os << A.back();
    for(int i = (int)A.size() - 2; i >= 0; i--)
        os << setw(WIDTH) << setfill('0') << A[i];
    return os;
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    const int N = 1e9, M = 4;
    mt19937_64 rnd(12412124235);
    array<int, M> p;
    uniform_int_distribution<int> dist(N / 2, N);
    for(int i = 0; i < M; i++){
        p[i] = dist(rnd);
    }

    auto add = [&](const array<int, M> &a, const array<int, M> &b){
        array<int, M> c{};
        for(int i = 0; i < M; i++){
            c[i] = (a[i] + b[i]);
            if (c[i] >= p[i]) c[i] -= p[i];
        }
        return c;
    };

    auto sub = [&](const array<int, M> &a, const array<int, M> &b){
        array<int, M> c{};
        for(int i = 0; i < M; i++){
            c[i] = (a[i] - b[i]);
            if (c[i] < 0) c[i] += p[i];
        }
        return c;
    };

    int n, k;
    cin >> n >> k;
    BigInt MOD = str2int(string(k, '9'));
    array<int, M> target[4];
    int base[M];
    for(int i = 0; i < M; i++){
        base[i] = qpow(10, k, p[i]) - 1;
        if (base[i] < 0) base[i] += p[i];
    }
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < M; j++){
            target[i][j] = 1LL * base[j] * i % p[j];
        }
    }
    vector<array<int, M> > q(n);
    if (k <= 9){
        int t = 1;
        for(int i = 0; i < k; i++) t *= 10;
        t -= 1;
        for(int i = 0; i < n; i++){
            string s;
            cin >> s;
            int a = 0;
            for(auto c : s){
                a = (1LL * a * 10 + c - '0') % t;
            }
            for(int j = 0; j < M; j++){
                q[i][j] = a % p[j];
            }
        } 
    }
    else{
        for(int i = 0; i < n; i++){
            string s;
            cin >> s;
            BigInt a{0};
            for(int j = s.size() - 1; j >= 0; j -= k){
                int r = j, l = max(0, r - k + 1);
                a = a + str2int(s.substr(l, r - l + 1));
                if (!(a < MOD)){
                    a = a - MOD;
                }
            }
            cout << a << '\n';
            for(int j = 0; j < M; j++){
                for(int k = (int)a.size() - 1; k >= 0; k--){
                    q[i][j] = 1LL * q[i][j] * BASE + a[k] % p[j];
                }
            }
        } 
    }
    int ans = 0;
    map<array<int, M>, int> mp;
    for(int i = 0; i < n; i++){
        mp[q[i]] += 1;
        for(int j = i; j <= n; j++){
            for(int k = 0; k < 4; k++){
                auto t = sub(target[k], add(q[i], q[j]));
                if (mp.contains(t)){
                    ans += mp[t];
                }
            }
        }
    }
    cout << ans << '\n';

}

详细

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: -100
Wrong Answer
time: 29ms
memory: 3636kb

input:

500 859
7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...

output:

435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628611995137246394912510656515295435557843205186214040186183306335684565204529861059170847125683930207400446208213553422685006231852021460607734961453840...

result:

wrong output format Expected integer, but "435544161148142093848317807514...1919147678034797899830944685400" found