正文
增加 40 行删除 21 行
32 行未改动3334353637383940414220 行未改动636465666768697071727374757677787980818283848586878889909192939495969798991004 行未改动32 行未改动所以我们计算多项式 $ F,G $ 后再求 $ F \times G $ 。结果即为 $ F \cdot G $ 中的常数项或者说零次项,如果 $ n % 4 = 2 $ 再乘以 $ -1 $ 。使用折半搜索;注意并不需要算 $ F \cdot G $ ,用哈希表统计系数找寻相反项即可。代码如下:```cpp#include <iostream>20 行未改动for (int i = 0; i < m; i++) scanf("%*d");printf("0\n");}else {unordered_map<int, long long> F, G;F.reserve(65536);G.reserve(65536);F[0] = G[0] = 1;for (short i = 0;i < n;i++) {int a;scanf("%d", &a);unordered_map<int, long long> F0 = F;F.clear();for (auto& f : F0) {F[f.first + a] += f.second;F[f.first - a] -= f.second;}}for (short j = 0;j < m;j++) {int b;scanf("%d", &b);unordered_map<int, long long> G0 = G;G.clear();for (auto& g : G0) {G[g.first + b] += g.second;G[g.first - b] += g.second;}}long long ans = 0;if (F.size() > G.size()) for (auto& g : G) {if (F.count(-g.first)) ans += F[-g.first] * g.second;}else for (auto& f : F) {if (G.count(-f.first)) ans += f.second * G[-f.first];}if (ans < 0) ans += MOD;if (n % 4 == 2) ans = MOD - ans;printf("%lld\n", ipow2[n + m] * ans % MOD);4 行未改动32 行未改动33343536373839404142434420 行未改动65666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181194 行未改动32 行未改动所以我们计算多项式 $ F,G $ 后再求 $ F \times G $ 。结果即为 $ F \cdot G $ 中的常数项或者说零次项,如果 $ n % 4 = 2 $ 再乘以 $ -1 $ 。使用折半搜索注意并不需要算 $ F \cdot G $ ,用哈希表统计系数找寻相反项即可;以及不要全程用哈希表代码如下:```cpp#include <iostream>20 行未改动for (int i = 0; i < m; i++) scanf("%*d");printf("0\n");}else {vector<int> Fp, Fn, G;Fp.reserve(32768);Fn.reserve(32768);G.reserve(65536);Fp.push_back(0);G.push_back(0);for (short i = 0; i < n; i++) {int a; scanf("%d", &a);vector<int> Fp0, Fn0;Fp0.reserve(32768);Fn0.reserve(32768);for (auto f : Fp) {Fp0.push_back(f + a);Fn0.push_back(f - a);}for (auto f : Fn) {Fn0.push_back(f + a);Fp0.push_back(f - a);}Fp.swap(Fp0);Fn.swap(Fn0);}for (short j = 0;j < m;j++) {int b;scanf("%d", &b);vector<int> G0;G0.reserve(65536);for (auto& g : G) {G0.push_back(g + b);G0.push_back(g - b);}G.swap(G0);}long long ans = 0;if (n > m) {unordered_map<int, long long> uG;uG.reserve(65536);for (auto& g : G) uG[g]++;for (auto& f : Fp) if (uG.count(-f)) ans += uG[-f];for (auto& f : Fn) if (uG.count(-f)) ans -= uG[-f];}else {unordered_map<int, long long> uF;uF.reserve(65536);for (auto& f : Fp) uF[f]++;for (auto& f : Fn) uF[f]--;for (auto& g : G) if (uF.count(-g)) ans += uF[-g];}if (ans < 0) ans += MOD;if (n % 4 == 2) ans = MOD - ans;printf("%lld\n", ipow2[n + m] * ans % MOD);4 行未改动