U694652的题解

· · 题解

以下推导虽然基于《复变函数》的欧拉公式,但是使用积化和差公式也可以得到相同结论但较为麻烦:

令 y = 2\pi x :

I = \int_0^1 \prod_{i=1}^n \sin(2\pi a_ix) \prod_{j=1}^m \cos(2\pi b_jx) \mathrm{d}x = \frac{1}{2\pi} \int_0^{2\pi} \prod_{i=1}^n \sin(a_iy) \prod_{j=1}^m \cos(b_jy) \mathrm{d}y

令 l 为虚数单位 ,因为 i,j 已经被用作下标了,使用欧拉公式:

\sin(a_iy) = \frac{e^{la_iy}-e^{-la_iy}}{2l} \quad \cos(b_jy) = \frac{e^{lb_jy}+e^{-lb_jy}}{2}

代入 I :

I = \frac{1}{2\pi} \int_0^{2\pi} \prod_{i=1}^n \frac{e^{la_iy}-e^{-la_iy}}{2l} \prod_{j=1}^m \frac{e^{lb_jy}+e^{-lb_jy}}{2} \mathrm{d}y = \frac{1}{2\pi} \frac{1}{2^{n+m}l^n} \int_0^{2\pi} \prod_{i=1}^n (e^{la_iy}-e^{-la_iy}) \prod_{j=1}^m (e^{lb_jy}+e^{-lb_jy}) \mathrm{d}y

不难发现积分内部最终结果将会是 e^{ly},e^{-ly} 的多项式:

F(e^{ly},e^{-ly}) = \prod_{i=1}^n (e^{la_iy}-e^{-la_iy}) \quad G(e^{ly},e^{-ly}) = \prod_{j=1}^m (e^{lb_jy}+e^{-lb_jy})

再次使用欧拉公式,其中 k 是整系数:

e^{lky} = \cos(ky) + l\sin(ky)

原积分是在实数域上的积分,所以不难发现:

当 n 是奇数时,只有 \sin(ky) 对结果有贡献,但该函数无论 k 是什么,积分结果都是 0 。

所以 n 是奇数时, I=0 。

当 n 是偶数时,只有 \cos(ky) 在 k=0 时对结果有贡献,贡献为 2\pi 。

所以我们计算多项式 F,G 后再求 F \times G 。

结果即为 F \cdot G 中的常数项或者说零次项,如果 n % 4 = 2 再乘以 -1 。

使用折半搜索

注意并不需要算 F \cdot G ,用哈希表统计系数找寻相反项即可;以及不要全程用哈希表

代码如下:

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
const long long MOD = 1000000007;

int main() {
    vector<long long> ipow2(33);
    ipow2[0] = ipow2[1] = 1;
    for (long long a = 2, b = MOD - 2;b;b >>= 1) {
        if (b & 1) (ipow2[1] *= a) %= MOD;
        (a *= a) %= MOD;
    }
    for (int i = 2;i <= 32;i++) ipow2[i] = ipow2[i - 1] * ipow2[1] % MOD;
    short t;
    scanf("%hd", &t);
    for (;t--;) {
        short n, m;
        scanf("%hd%hd", &n, &m);
        if (n % 2) {
            for (int i = 0; i < n; i++) scanf("%*d");
            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);
        }
    }
}