版本比较
U694652的题解
正文
增加 43 行删除 22 行
28 行未改动29303132333334353536373839404142434420 行未改动656667686566697071726869707172737475767374757677787980818283848586878889909192938283848586949596979899100101102909110310410510610710810993941101111121131141151161171181194 行未改动28 行未改动所以 $ n $ 是奇数时, $ I=0 $ 。
当 $ n $ 是偶数时,只有 $ \cos(ky) $ 在 $ k=0 $ 时对结果有贡献,贡献为 $ 2\pi $ 。
所以我们计算多项式 $ F,G $ 后再求 $ F \times G $ , 结果即为 $ F \times G $ 中的常数项或者说零次项。所以我们计算多项式 $ 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); vector<int> Fp, Fn, G; Fp.reserve(32768); Fn.reserve(32768); 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; 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); 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; 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 (F.size() > G.size()) for (auto& g : G) { if (F.count(-g.first)) ans += F[-g.first] * g.second; 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 for (auto& f : F) { if (G.count(-f.first)) ans += f.second * G[-f.first]; 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 行未改动