跳到正文

题解

P1001题解

___HAAVK___约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月17日
篇幅
约 10 分钟
题目
P1001

版本

  1. v2当前
  2. v1

看完题,我惊呆了——这道红题居然要考A+B这一世界性难题,这个建议改成黑题

我做为刚刚学习c++的蒟蒻,第一眼就看见这题的数据—— ∣a∣,∣b∣≤109∣a∣,∣b∣≤10^9 这个数据范围实在是太大了,short压根不够用。

为了优化大炮打蚊子的手感,我特地把FV-4005的主炮拆下来并排安了4根来方便进行火力覆盖

好像扯远了……我看了一下发的题解,大部分都在计算中使用了加号所以很不符合大炮打蚊子的宗旨(虽然我的代码在循环变量自增里也用了加号)

接下来上AC代码 (我知道你们就是来看这个的)

C++
#include <iostream>
using namespace std;

const int BITS = 64;

int main() {
    long long a, b;
    cin >> a >> b;

    unsigned long long ua = static_cast<unsigned long long>(a);
    unsigned long long ub = static_cast<unsigned long long>(b);

    int A[BITS] = {0}, B[BITS] = {0};
    for (int i = 0; i < BITS; ++i) {
        A[i] = (ua >> i) & 1;
        B[i] = (ub >> i) & 1;
    }

    int P[BITS], G[BITS];
    for (int i = 0; i < BITS; ++i) {
        P[i] = A[i] ^ B[i];
        G[i] = A[i] & B[i];
    }

    int PP[BITS], GG[BITS];
    for (int i = 0; i < BITS; ++i) {
        PP[i] = P[i];
        GG[i] = G[i];
    }

    for (int k = 0; k < 6; ++k) {
        int shift = 1 << k;
        for (int i = BITS - 1; i >= shift; --i) {
            int newG = GG[i] | (PP[i] & GG[i - shift]);
            int newP = PP[i] & PP[i - shift];
            GG[i] = newG;
            PP[i] = newP;
        }
    }

    int C[BITS] = {0};
    for (int i = 1; i < BITS; ++i) {
        C[i] = GG[i - 1];
    }

    int sum[BITS];
    for (int i = 0; i < BITS; ++i) {
        sum[i] = P[i] ^ C[i];
    }

    unsigned long long result = 0;
    for (int i = 0; i < BITS; ++i) {
        if (sum[i]) {
            result |= (1ULL << i);
        }
    }
防止有人抄代码(但是真的有人会抄黑题难度的吗?)

    cout << static_cast<long long>(result) << endl;

    return 0;
}

我的代码很简短易懂对不对?才61行

算法解释

输入处理:

读取两个整数并转换为无符号64位整数 将整数分解为64位二进制数组(0=LSB, 63=MSB)

生成传播(P)和生成(G)信号:

P[i] = A[i] XOR B[i]:表示该位是否传播进位 G[i] = A[i] AND B[i]:表示该位是否生成进位

Kogge-Stone并行前缀计算:

进行6级迭代(log₂64=6) 每级迭代计算更远距离的进位传播 使用双重缓冲更新传播(PP)和生成(GG)信号

进位计算:

最终进位C[i] = GG[i-1] 第0位没有进位输入(C[0]=0)

求和计算:

每位的和sum[i] = P[i] XOR C[i] 组合64位结果

结果输出:

将二进制和转换为64位有符号整数 处理负数补码表示

至此大家已经完成了这个世界级难题了,这可能是大家完成的第一道黑题难度红题,学完这个你就可以去试试高精度加法这种题目了……

当然我还有更整活的……

C++
#include <iostream>
#include <vector>
#include <complex>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;

const double PI = acos(-1.0);
typedef complex<double> Complex;

// FFT 实现
void fft(vector<Complex>& a, bool invert) {
    int n = a.size();
    for (int i = 1, j = 0; i < n; i++) {
        int bit = n >> 1;
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;
        if (i < j)
            swap(a[i], a[j]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        Complex wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            Complex w(1);
            for (int j = 0; j < len / 2; j++) {
                Complex u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (Complex& x : a)
            x /= n;
    }
}

vector<int> multiply(const vector<int>& a, const vector<int>& b) {
    vector<Complex> fa(a.begin(), a.end()), fb(b.begin(), b.end());
    int n = 1;
    while (n < max(a.size(), b.size()))
        n <<= 1;
    n <<= 1;
    fa.resize(n);
    fb.resize(n);

    fft(fa, false);
    fft(fb, false);
    for (int i = 0; i < n; i++)
        fa[i] += fb[i];  // 关键:频域点值相加实现多项式加法
    fft(fa, true);

    vector<int> result(n);
    for (int i = 0; i < n; i++)
        result[i] = round(fa[i].real());
    return result;
}

// 高精度加法
string bigint_add(string num1, string num2) {
    bool neg1 = (num1[0] == '-');
    bool neg2 = (num2[0] == '-');

    if (neg1) num1 = num1.substr(1);
    if (neg2) num2 = num2.substr(1);

    reverse(num1.begin(), num1.end());
    reverse(num2.begin(), num2.end());

    vector<int> a, b;
    for (char c : num1) a.push_back(c - '0');
    for (char c : num2) b.push_back(c - '0');

    vector<int> res = multiply(a, b);

    // 处理进位
    int carry = 0;
    for (int i = 0; i < res.size(); i++) {
        res[i] += carry;
        carry = res[i] / 10;
        res[i] %= 10;
    }

    // 转换为字符串
    string result;
    for (int i = res.size() - 1; i >= 0; i--) {
        if (result.empty() && res[i] == 0) continue; // 跳过前导零
        result += char(res[i] + '0');
    }

    if (result.empty()) result = "0";

    // 处理符号
    if ((neg1 && !neg2) || (!neg1 && neg2)) {
        if (result != "0") {
            result = (neg1 ? "-" : "") + result;
        }
    } else if (neg1 && neg2) {
        result = "-" + result;
    }

    return result;
}

int main() {
    string a, b;
    cin >> a >> b;
    cout << a + b << endl;//惊喜吗???哈哈哈
    return 0;
}
原文