跳到正文

题解

题解:P13388 [GCJ 2010 Qualification] Fair Warning

WYxiaolan约 21 分钟
原文
文章信息与版本

文章信息

发布
2026年8月18日
收录
2026年9月10日
更新
2026年11月3日
篇幅
约 21 分钟
题目
P13388

版本

  1. v1当前

呃,看了一楼二楼之后发现,太可恶了,竟然用python和java。 嗯对,这是我之间的代码:

C++
#include <bits/stdc++.h>
using namespace std;
string s;
int a[110], b[110], c[110];
int len;
void sub(int a[], int b[]) {
    int len = max(a[0], b[0]);
    for (int i = 1; i <= len; i++) {
        a[i] -= b[i];
        if (a[i] < 0) {
            a[i] += 10;
            a[i + 1]--;
        }
    }
    while (len > 1 && a[len] == 0) len--;
    a[0] = len;
}
int cmp(int a[], int b[]) {
    if (a[0] != b[0]) return a[0] > b[0];
    for (int i = a[0]; i >= 1; i--) {
        if (a[i] != b[i]) return a[i] > b[i];
    }
    return 1;
}
void mod(int a[], int b[]) {
    int tmp[110] = {0};
    while (cmp(a, b)) {
        int len = a[0] - b[0] + 1;
        memset(tmp, 0, sizeof(tmp));
        for (int i = 1; i <= b[0]; i++) tmp[i + len - 1] = b[i];
        tmp[0] = b[0] + len - 1;
        sub(a, tmp);
    }
}
void gcd(int a[], int b[]) {
    while (b[0] > 1 || b[1] > 0) {
        mod(a, b);
        if (a[0] == 1 && a[1] == 0) break;
        for (int i = 0; i <= max(a[0], b[0]); i++) swap(a[i], b[i]);
    }
}
void str_to_arr(string &s, int arr[]) {
    arr[0] = s.length();
    for (int i = 1; i <= arr[0]; i++) {
        arr[i] = s[arr[0] - i] - '0';
    }
}
void print_arr(int arr[]) {
    for (int i = arr[0]; i >= 1; i--) cout << arr[i];
}
int main() {
    int C;
    cin >> C;
    for (int cas = 1; cas <= C; cas++) {
        int n;
        cin >> n;
        vector<string> vs(n);
        for (int i = 0; i < n; i++) cin >> vs[i];
        int base[110] = {0};
        str_to_arr(vs[0], base);
        int g[110] = {0};
        int first = 1;
        for (int i = 1; i < n; i++) {
            int cur[110] = {0};
            str_to_arr(vs[i], cur);
            int diff[110] = {0};
            if (cmp(cur, base)) {
                for (int j = 0; j <= cur[0]; j++) diff[j] = cur[j];
                sub(diff, base);
            } else {
                for (int j = 0; j <= base[0]; j++) diff[j] = base[j];
                sub(diff, cur);
            }
            if (diff[0] == 1 && diff[1] == 0) continue;
            if (first) {
                for (int j = 0; j <= diff[0]; j++) g[j] = diff[j];
                first = 0;
            } else {
                gcd(g, diff);
            }
        }
        int T[110] = {0};
        for (int i = 0; i <= g[0]; i++) T[i] = g[i];
        // r = base % T
        int r[110] = {0};
        for (int i = 0; i <= base[0]; i++) r[i] = base[i];
        mod(r, T);
        int y[110] = {0};
        if (r[0] == 1 && r[1] == 0) {
            y[0] = 1; y[1] = 0;
        } else {
            int tmp[110] = {0};
            for (int i = 0; i <= T[0]; i++) tmp[i] = T[i];
            sub(tmp, r);
            for (int i = 0; i <= tmp[0]; i++) y[i] = tmp[i];
        }
        cout << "Case #" << cas << ": ";
        print_arr(y);
        cout << endl;
    }
    return 0;
}

两个测试点直接全TLE了,足足3.2s。

搞了半天发现,原来是mod函数效率太低了。

废话不多说,以下就是代码。

C++
#include<bits/stdc++.h>
#define Code using
#define by namespace
#define wjb std
Code by wjb;
namespace Bigint {
    static const int WIDTH = 8, BASE = 1e8;
    long long Pow[40]; // Precompute powers of 2 up to sufficient size
    bool inited = false;
    inline void init() {
        if (inited) return;
        Pow[0] = 1;
        for (int i = 1; i < 40; ++i) Pow[i] = Pow[i-1] << 1;
        inited = true;
    }
    struct BigInt {
        vector<long long> a;
        char op;
        BigInt() : op('+') {}
        BigInt(long long num) { *this = num; }
        BigInt(const string &str) { *this = str; }
        inline void swap(BigInt &other) {
            ::swap(a, other.a);
            ::swap(op, other.op);
        }
        inline BigInt &operator=(long long num) {
            if (num < 0) {
                op = '-';
                num = -num;
            } else {
                op = '+';
            }
            a.clear();
            do {
                a.push_back(num % BASE);
                num /= BASE;
            } while (num);
            return *this;
        }
        inline BigInt &operator=(const string &str) {
            int l = 0;
            a.clear();
            if (str[0] == '-') {
                op = '-';
                l = 1;
            } else {
                op = '+';
            }
            int len = (str.size() - l - 1) / WIDTH + 1;
            for (int i = 0; i < len; ++i) {
                int end = str.size() - i * WIDTH;
                int start = max(l, end - WIDTH);
                long long x = 0;
                for (int j = start; j < end; ++j) {
                    x = x * 10 + (str[j] - '0');
                }
                a.push_back(x);
            }
            return *this;
        }
        inline bool operator<(const BigInt &b) const {
            if (op != b.op) {
                return op == '-';
            }
            if (a.size() != b.a.size()) {
                return (op == '+') ? (a.size() < b.a.size()) : (a.size() > b.a.size());
            }
            for (int i = a.size() - 1; i >= 0; --i) {
                if (a[i] != b.a[i]) {
                    return (op == '+') ? (a[i] < b.a[i]) : (a[i] > b.a[i]);
                }
            }
            return false;
        }
        inline bool operator==(const BigInt &b) const {
            return op == b.op && a == b.a;
        }
        inline bool operator!=(const BigInt &b) const {
            return !(*this == b);
        }
        inline bool operator<=(const BigInt &b) const {
            return (*this < b) || (*this == b);
        }
        inline bool operator>(const BigInt &b) const {
            return !(*this <= b);
        }
        inline bool operator>=(const BigInt &b) const {
            return !(*this < b);
        }
        inline BigInt operator+(const BigInt &b) const {
            if (op != b.op) {
                BigInt tmp = b;
                tmp.op = (tmp.op == '-') ? '+' : '-';
                return *this - tmp;
            }
            BigInt res;
            res.op = op;
            res.a.resize(max(a.size(), b.a.size()));
            long long carry = 0;
            for (size_t i = 0; i < res.a.size() || carry; ++i) {
                long long sum = carry;
                if (i < a.size()) sum += a[i];
                if (i < b.a.size()) sum += b.a[i];
                res.a.push_back(sum % BASE);
                carry = sum / BASE;
            }
            while (res.a.size() > 1 && res.a.back() == 0) res.a.pop_back();
            return res;
        }
        inline BigInt operator-(const BigInt &b) const {
            if (op != b.op) {
                BigInt tmp = b;
                tmp.op = (tmp.op == '-') ? '+' : '-';
                return *this + tmp;
            }
            BigInt aa = *this, bb = b;
            aa.op = '+';
            bb.op = '+';
            if (aa < bb) {
                swap(aa, bb);
                aa.op = '-';
            } else {
                aa.op = '+';
            }
            long long borrow = 0;
            for (size_t i = 0; i < aa.a.size(); ++i) {
                long long diff = aa.a[i] - borrow;
                if (i < bb.a.size()) diff -= bb.a[i];
                if (diff < 0) {
                    diff += BASE;
                    borrow = 1;
                } else {
                    borrow = 0;
                }
                aa.a[i] = diff;
            }
            while (aa.a.size() > 1 && aa.a.back() == 0) aa.a.pop_back();
            return aa;
        }
        inline BigInt operator*(const BigInt &b) const {
            BigInt res;
            res.op = (op == b.op) ? '+' : '-';
            res.a.resize(a.size() + b.a.size());
            for (size_t i = 0; i < a.size(); ++i) {
                for (size_t j = 0; j < b.a.size(); ++j) {
                    res.a[i + j] += a[i] * b.a[j];
                }
            }
            for (size_t i = 0; i < res.a.size() - 1; ++i) {
                res.a[i + 1] += res.a[i] / BASE;
                res.a[i] %= BASE;
            }
            while (res.a.size() > 1 && res.a.back() == 0) res.a.pop_back();
            return res;
        }
        inline BigInt operator/(const BigInt &b) const {
            if (b == 0) {
                assert(false); // Avoid division by zero
            }
            BigInt aa = *this, bb = b;
            aa.op = '+';
            bb.op = '+';
            if (aa < bb) return 0;
            char tmp_op = (op != b.op) ? '-' : '+';
            init();
            BigInt res;
            res.a.resize(aa.a.size() - bb.a.size() + 1);
            for (int i = (int)res.a.size() - 1; i >= 0; --i) {
                BigInt current = bb;
                current.a.insert(current.a.begin(), i, 0);
                long long quotient = 0;
                for (int j = 39; j >= 0; --j) {
                    BigInt temp = current * Pow[j];
                    if (aa >= temp) {
                        quotient += Pow[j];
                        aa -= temp;
                    }
                }
                res.a[i] = quotient;
            }
            while (res.a.size() > 1 && res.a.back() == 0) res.a.pop_back();
            res.op = tmp_op;
            return res;
        }
        inline BigInt operator%(const BigInt &b) const {
            return *this - (*this / b) * b;
        }
        inline BigInt &operator+=(const BigInt &b) {
            *this = *this + b;
            return *this;
        }
        inline BigInt &operator-=(const BigInt &b) {
            *this = *this - b;
            return *this;
        }
        inline BigInt &operator*=(const BigInt &b) {
            *this = *this * b;
            return *this;
        }
        inline BigInt &operator/=(const BigInt &b) {
            *this = *this / b;
            return *this;
        }
        inline BigInt &operator%=(const BigInt &b) {
            *this = *this % b;
            return *this;
        }
        inline friend istream &operator>>(istream &input, BigInt &x) {
            string s;
            if (!(input >> s)) return input;
            x = s;
            return input;
        }
        inline friend ostream &operator<<(ostream &output, const BigInt &x) {
            if (x.op == '-' && !(x.a.size() == 1 && x.a[0] == 0)) {
                output << '-';
            }
            output << x.a.back();
            for (int i = x.a.size() - 2; i >= 0; --i) {
                output << setfill('0

不要作弊抄袭,建造良好洛谷!

原文