题解:CF895C Square Subsets

· · 题解

根据调查题解区没有这个复杂度的做法。

考虑能不能直接 dp,在 [1,70] 里有 V=19 个质数, f_{i,s} 表示考虑前 i 个数,所有质因子的奇偶状态是 s,其实不用 dp,就是递推,选第 i 个或者不选,时间复杂度 O(n2^V),爆了。

考虑进行一些简单的观察,我们可以把 a_i 的唯一分解分成以下三类:

显然这些数对于最终乘积的贡献是独立的,所以可以分开考虑贡献。

总的时间复杂度就是 O(n2^V),其中 V=11,4s 随便过,注意减掉空集。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define test cerr << "Azusunnya!\n"

const int N = 1e5 + 5, M = 1 << 11, mod = 1e9 + 7;
const int p[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
int n, f[2][M + 5], g[N][2]; vector <int> v; map <int, int> mp;

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n; int all = 1 << 11;
    g[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        g[i][0] = (g[i - 1][0] + g[i - 1][1]) % mod;
        g[i][1] = (g[i - 1][1] + g[i - 1][0]) % mod;
    }
    v.push_back(0); int c1 = 0;
    for (int i = 1, x; i <= n; i++) {
        cin >> x; int val = 0; 
        if (x == 1) { c1++; continue; }
        for (int o = 0; o < 11; o++) {
            bool c = 0;
            while (x % p[o] == 0) x /= p[o], c ^= 1;
            val |= (c << o);
        } if (x > 1) mp[x]++; else v.push_back(val);
    } 
    f[0][0] = 1;
    for (int i = 1; i < v.size(); i++) {
        for (int s = 0; s < M; s++) {
            f[i & 1][s] = (f[i - 1 & 1][s] + f[i - 1 & 1][s ^ v[i]]) % mod;
        }
    } 
    int ans = (v.size() ? f[v.size() - 1 & 1][0] : 1);
    for (auto [_, c] : mp) (ans *= g[c][0]) %= mod;
    for (int i = 1; i <= c1; i++) (ans *= 2) %= mod;
    cout << (ans + mod - 1) % mod;
    return 0;
}