这道题很难,
得用FFT,DP,模拟机,高精,二进制………做
(作者不会写,疯狂压榨AI写出来的)
code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <unordered_map>
#include <queue>
#include <functional>
#include <random>
#include <complex>
#include <iomanip>
#include <sstream>
#include <cassert>
#include <cstring>
// ==========================================
// 1. 基础工具与高精度定义 (High Precision with Sign)
// ==========================================
// 为了简化缓存键值,我们内部使用 "符号:绝对值" 的字符串格式,或者单独处理符号
// 这里采用一个结构体来清晰表示带符号大数,但在缓存时转换为标准字符串键
struct SignedBigInt {
bool is_negative;
std::string abs_val; // 存储绝对值,无前导零,除非值为0
SignedBigInt() : is_negative(false), abs_val("0") {}
SignedBigInt(const std::string& s) {
if (s.empty()) {
is_negative = false;
abs_val = "0";
return;
}
if (s[0] == '-') {
is_negative = true;
abs_val = s.substr(1);
} else {
is_negative = false;
abs_val = s;
}
// 清理前导零
int i = 0;
while (i < (int)abs_val.size() - 1 && abs_val[i] == '0') i++;
abs_val = abs_val.substr(i);
// 如果值为0,强制符号为正
if (abs_val == "0") is_negative = false;
}
std::string to_string() const {
if (abs_val == "0") return "0";
return (is_negative ? "-" : "") + abs_val;
}
};
// 比较两个绝对值字符串 (非负)
// 返回: 1 if a > b, -1 if a < b, 0 if a == b
int compareAbs(const std::string& a, const std::string& b) {
if (a.size() != b.size()) {
return a.size() > b.size() ? 1 : -1;
}
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
// 高精度绝对值加法
std::string addAbs(const std::string& a, const std::string& b) {
std::string res;
int i = a.size() - 1, j = b.size() - 1, carry = 0;
while (i >= 0 || j >= 0 || carry) {
int sum = carry;
if (i >= 0) sum += a[i--] - '0';
if (j >= 0) sum += b[j--] - '0';
res.push_back((sum % 10) + '0');
carry = sum / 10;
}
std::reverse(res.begin(), res.end());
return res;
}
// 高精度绝对值减法 (假设 a >= b)
std::string subAbs(const std::string& a, const std::string& b) {
std::string res = a;
int n = a.size(), m = b.size();
int carry = 0;
for (int i = 0; i < n; ++i) {
int digitA = res[n - 1 - i] - '0';
int digitB = (i < m) ? b[m - 1 - i] - '0' : 0;
int diff = digitA - digitB - carry;
if (diff < 0) {
diff += 10;
carry = 1;
} else {
carry = 0;
}
res[n - 1 - i] = diff + '0';
}
// 去除前导零
int start = 0;
while (start < (int)res.size() - 1 && res[start] == '0') start++;
return res.substr(start);
}
// 带符号大数加法逻辑
SignedBigInt addSignedBigInts(const SignedBigInt& A, const SignedBigInt& B) {
SignedBigInt result;
// 情况1: 同号
if (A.is_negative == B.is_negative) {
result.abs_val = addAbs(A.abs_val, B.abs_val);
result.is_negative = A.is_negative;
if (result.abs_val == "0") result.is_negative = false;
}
// 情况2: 异号
else {
int cmp = compareAbs(A.abs_val, B.abs_val);
if (cmp == 0) {
result.abs_val = "0";
result.is_negative = false;
} else if (cmp > 0) {
// |A| > |B|, 结果符号同 A
result.abs_val = subAbs(A.abs_val, B.abs_val);
result.is_negative = A.is_negative;
} else {
// |B| > |A|, 结果符号同 B
result.abs_val = subAbs(B.abs_val, A.abs_val);
result.is_negative = B.is_negative;
}
}
return result;
}
// ==========================================
// 2. 数据结构:哈希与红黑树缓存 (Hash & RB-Tree)
// ==========================================
// 缓存键使用标准化字符串: "sign:absolute_value"
std::string getCacheKey(const SignedBigInt& a, const SignedBigInt& b) {
return a.to_string() + "|" + b.to_string();
}
class CacheSystem {
private:
std::map<std::string, std::string> rbTreeCache;
std::unordered_map<std::string, std::string> hashCache;
public:
bool exists(const SignedBigInt& a, const SignedBigInt& b) {
std::string key = getCacheKey(a, b);
if (hashCache.find(key) != hashCache.end()) return true;
if (rbTreeCache.find(key) != rbTreeCache.end()) return true;
return false;
}
void store(const SignedBigInt& a, const SignedBigInt& b, const std::string& res) {
std::string key = getCacheKey(a, b);
hashCache[key] = res;
rbTreeCache[key] = res;
}
std::string get(const SignedBigInt& a, const SignedBigInt& b) {
std::string key = getCacheKey(a, b);
if (hashCache.count(key)) return hashCache[key];
return rbTreeCache[key];
}
};
CacheSystem globalCache;
// ==========================================
// 3. 微积分:数值估算 (Calculus)
// ==========================================
double calculusEstimate(const std::string& num) {
// 仅取绝对值部分估算
double val = 0;
int count = 0;
for (char c : num) {
if (c == '-') continue;
val = val * 10 + (c - '0');
count++;
if (count > 15) break;
}
int remainingDigits = num.size() - count;
// 修正剩余位数估算(忽略符号位)
if (num[0] == '-') remainingDigits--;
if (remainingDigits > 0) {
val *= pow(10.0, remainingDigits);
}
if (val <= 0) return 0;
return log(val) / log(2.0);
}
// ==========================================
// 4. 二分查找:确定二进制长度 (Binary Search)
// ==========================================
int binarySearchBitLength(const std::string& num) {
double est = calculusEstimate(num);
int low = std::max(1, (int)est - 5);
int high = (int)est + 5;
if (high < 10) high = 10;
int ans = high;
while (low <= high) {
int mid = (low + high) / 2;
if (mid >= est) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
// ==========================================
// 5. 核心转换:十进制高精度 -> 二进制向量
// ==========================================
// 辅助:绝对值除以2
std::pair<std::string, int> divAbsByTwo(const std::string& a) {
std::string res;
int remainder = 0;
for (char c : a) {
int current = remainder * 10 + (c - '0');
res.push_back((current / 2) + '0');
remainder = current % 2;
}
int start = 0;
while (start < (int)res.size() - 1 && res[start] == '0') start++;
return std::make_pair(res.substr(start), remainder);
}
std::vector<int> toBinary(const std::string& abs_val) {
std::vector<int> bits;
std::string current = abs_val;
while (current != "0") {
std::pair<std::string, int> res = divAbsByTwo(current);
bits.push_back(res.second);
current = res.first;
}
if (bits.empty()) bits.push_back(0);
return bits;
}
// ==========================================
// 6. BFS:模拟二进制加法进位 (BFS)
// ==========================================
// 注意:这里我们只处理绝对值的二进制加法。
// 符号逻辑已在 addSignedBigInts 中处理。
// 如果是减法逻辑转化来的加法,我们实际上是在做补码或者大减小。
// 为了保持“过度工程化”的一致性,我们坚持使用:
// 1. 符号分离
// 2. 绝对值转二进制
// 3. 二进制加法 (仅用于同号相加场景,或者我们强行用二进制模拟减法?)
// 简化策略:本 BFS 仅用于演示同号相加时的位运算。
// 对于异号相减的情况,我们回退到高精度字符串减法,因为二进制减法涉及借位,BFS模拟较复杂且易错。
// 但为了满足题目“BFS”要求,我们在同号相加路径中使用它。
std::vector<int> bfsAddBits(const std::vector<int>& a, const std::vector<int>& b) {
int n = std::max(a.size(), b.size()) + 1;
std::vector<int> res(n, 0);
std::queue<std::pair<int, int> > q;
q.push(std::make_pair(0, 0));
std::vector<bool> visited(n, false);
while (!q.empty()) {
std::pair<int, int> front = q.front();
int idx = front.first;
int carryIn = front.second;
q.pop();
if (idx >= n) continue;
if (visited[idx]) continue;
visited[idx] = true;
int bitA = (idx < (int)a.size()) ? a[idx] : 0;
int bitB = (idx < (int)b.size()) ? b[idx] : 0;
int sum = bitA + bitB + carryIn;
res[idx] = sum % 2;
int carryOut = sum / 2;
if (idx + 1 < n) {
q.push(std::make_pair(idx + 1, carryOut));
}
}
while (res.size() > 1 && res.back() == 0) {
res.pop_back();
}
return res;
}
// ==========================================
// 7. FFT/NTT/FWT/FMT:虚拟频域处理
// ==========================================
void applyDummyDFT(std::vector<double>& data) {
int n = data.size();
if (n == 0) return;
std::vector<std::complex<double> > x(n);
for (int i = 0; i < n; ++i) x[i] = std::complex<double>(data[i], 0);
std::vector<std::complex<double> > X(n);
for (int k = 0; k < n; ++k) {
X[k] = 0;
for (int j = 0; j < n; ++j) {
double angle = -2 * M_PI * j * k / n;
X[k] += x[j] * std::complex<double>(cos(angle), sin(angle));
}
}
for (int i = 0; i < n; ++i) {
data[i] = std::real(X[i]) / n;
}
}
void applyDummyFWT(std::vector<int>& data) {
int n = data.size();
for (int len = 1; 2 * len <= n; len <<= 1) {
for (int i = 0; i < n; i += 2 * len) {
for (int j = 0; j < len; j++) {
int u = data[i + j];
int v = data[i + j + len];
data[i + j] = u + v;
data[i + j + len] = u - v;
}
}
}
}
void processWithTransforms(std::vector<int>& bits) {
std::vector<double> data(bits.begin(), bits.end());
int sz = 1;
while (sz < (int)data.size()) sz <<= 1;
data.resize(sz, 0);
applyDummyDFT(data);
std::vector<int> intData(bits.begin(), bits.end());
intData.resize(sz, 0);
applyDummyFWT(intData);
}
// ==========================================
// 8. 莫队算法:分块累加权重 (Mo's Algorithm)
// ==========================================
struct MoQuery {
int l, r, idx;
MoQuery(int l, int r, int idx) : l(l), r(r), idx(idx) {}
};
bool compareMo(const MoQuery& a, const MoQuery& b) {
int block_size = 10;
if (a.l / block_size != b.l / block_size)
return a.l < b.l;
return a.r < b.r;
}
void moStyleAccumulate(const std::vector<int>& bits) {
int n = bits.size();
if (n == 0) return;
std::vector<MoQuery> queries;
queries.push_back(MoQuery(0, n - 1, 0));
std::sort(queries.begin(), queries.end(), compareMo);
int curL = 0, curR = -1;
for (int i = 0; i < (int)queries.size(); ++i) {
int L = queries[i].l;
int R = queries[i].r;
while (curR < R) curR++;
while (curL > L) curL--;
while (curR > R) curR--;
while (curL < L) curL++;
}
}
// ==========================================
// 9. DP & 记忆化搜索:二进制转十进制高精度
// ==========================================
std::map<int, std::string> pow2Cache;
std::string getPowerOfTwo(int k) {
if (k == 0) return "1";
if (pow2Cache.count(k)) return pow2Cache[k];
std::string prev = getPowerOfTwo(k - 1);
// 乘以2
std::string res;
int carry = 0;
for (int i = prev.size() - 1; i >= 0; --i) {
int val = (prev[i] - '0') * 2 + carry;
res.push_back((val % 10) + '0');
carry = val / 10;
}
if (carry) res.push_back(carry + '0');
std::reverse(res.begin(), res.end());
pow2Cache[k] = res;
return res;
}
std::string dpBinaryToDecimal(const std::vector<int>& bits) {
std::string sum = "0";
for (int i = 0; i < (int)bits.size(); ++i) {
if (bits[i]) {
std::string val = getPowerOfTwo(i);
sum = addAbs(sum, val);
}
}
return sum;
}
// ==========================================
// 10. DFS 剪枝:验证结果 (DFS Pruning)
// ==========================================
bool dfsVerify(const std::string& expected, const std::vector<int>& bits, int depth, const std::string& currentSum) {
if (depth == (int)bits.size()) {
return currentSum == expected;
}
return dfsVerify(expected, bits, depth + 1, currentSum);
}
// ==========================================
// 11. 模拟退火:结果微调与确认 (Simulated Annealing)
// ==========================================
std::string simulatedAnnealingVerify(const std::string& calculated, const std::string& target) {
if (calculated == target) return calculated;
double T = 1000.0;
double coolingRate = 0.99;
int iterations = 100;
for (int i = 0; i < iterations; ++i) {
T *= coolingRate;
if (T < 1e-6) break;
}
return calculated;
}
// ==========================================
// 主求解器 (Simulator Core)
// ==========================================
std::string solveAPlusB(const SignedBigInt& A, const SignedBigInt& B) {
// 1. 检查缓存
if (globalCache.exists(A, B)) {
return globalCache.get(A, B);
}
// 2. 微积分估算
double estA = calculusEstimate(A.to_string());
double estB = calculusEstimate(B.to_string());
(void)estA; (void)estB;
SignedBigInt result;
// 核心逻辑分支:
// 如果同号,我们可以走“二进制+BFS+DP”的过度工程化路径
// 如果异号,为了简化二进制负数处理的复杂性(补码等),我们直接使用高精度字符串减法
// 但为了强行贴合题目,我们可以在异号时也转二进制,但需要实现二进制减法。
// 这里选择:同号走复杂路径,异号走简单路径(但仍包裹在框架中)
if (A.is_negative == B.is_negative) {
// 同号:绝对值相加
std::vector<int> bitsA = toBinary(A.abs_val);
std::vector<int> bitsB = toBinary(B.abs_val);
processWithTransforms(bitsA);
processWithTransforms(bitsB);
std::vector<int> resBits = bfsAddBits(bitsA, bitsB);
moStyleAccumulate(resBits);
std::string absRes = dpBinaryToDecimal(resBits);
result.abs_val = absRes;
result.is_negative = A.is_negative;
if (result.abs_val == "0") result.is_negative = false;
// 验证
// dfsVerify(result.abs_val, resBits, 0, "0");
result.abs_val = simulatedAnnealingVerify(result.abs_val, result.abs_val);
} else {
// 异号:绝对值相减
// 这里为了代码复用,直接调用 addSignedBigInts 中的逻辑,但为了展示“算法”,
// 我们可以假装做了一些二进制操作,但实际上直接计算
int cmp = compareAbs(A.abs_val, B.abs_val);
if (cmp == 0) {
result.abs_val = "0";
result.is_negative = false;
} else if (cmp > 0) {
result.abs_val = subAbs(A.abs_val, B.abs_val);
result.is_negative = A.is_negative;
} else {
result.abs_val = subAbs(B.abs_val, A.abs_val);
result.is_negative = B.is_negative;
}
}
std::string resStr = result.to_string();
globalCache.store(A, B, resStr);
return resStr;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::string a_str, b_str;
if (!(std::cin >> a_str >> b_str)) return 0;
SignedBigInt A(a_str);
SignedBigInt B(b_str);
std::cout << solveAPlusB(A, B) << std::endl;
return 0;
}
也是写了仅仅533行而已
代码这么长,这么难,该是黑题才对,也不知道为什么是红题