zoukankan      html  css  js  c++  java
  • 题解 CF914G 【Sum the Fibonacci】

    经典板子题?

    (i = s_a&s_b, j = s_c, k = s_d oplus s_e)

    (sum_{p}sum_{i&j&k = 2^p} f_i imes f_j imes f_k imes (sum_{s_a|s_b = i, s_a &s_b = 0}1) imes (sum_{s_c = j}1) imes (sum_{s_d oplus s_e = k}1))

    (A_i = (sum_{s_a|s_b = i, s_a &s_b = 0}1))

    (B_j = (sum_{s_c = j}1))

    (C_k = (sum_{s_d oplus s_e = k}1))

    (A_i) 可以通过子集卷积来做,复杂度 (N log^2 N)

    (B_j) 显然。

    (C_k) 可以直接 FWTXOR 来做,复杂度 (N log N)

    最后 FWTAND 卷一下三个就行了。

    #include <bits/stdc++.h>
    const int BL = 2 << 20;
    struct streambufcin {
    	std::streambuf *in;
    	streambufcin() {
    		std::ios::sync_with_stdio(false);
    		std::cin.tie(NULL);
    		in = std::cin.rdbuf();
    		bg = ed = NULL;
    #ifdef LOCAL
    		freopen("in.in", "r", stdin);
    #endif
    	}
    	char buf[BL], *bg, *ed, ch;
    	char gc() { return bg == ed ? ed = buf + in -> sgetn(bg = buf, BL), (bg == ed ? -1 : *bg++) : *bg++; }
    	template<class T>
    	streambufcin &operator >> (T &x) {
    		bool neg = false;
    		for (ch = gc(); !isdigit(ch); ch = gc());
    		if (ch == '-')
    			neg = true;
    		for (x = 0; isdigit(ch); ch = gc())
    			x = x * 10 + ch - 48;
    		if (neg)
    			x = -x;
    		return *this;
    	}
    	streambufcin &operator >> (char &c) {
    		for (c = gc(); isspace(c); c = gc());
    		return *this;
    	}
    } cin;
    
    struct streambufcout {
    	std::streambuf *out = std::cout.rdbuf();
    	char buf[BL], *optr = buf;
    	streambufcout() { out = std::cout.rdbuf(); }
    	~streambufcout() { flush(); }
    	void flush() { out -> sputn(buf, optr - buf); optr = buf; }
    	void pc(char c) {
    		if (optr == buf + BL)
    			flush();
    		*optr++ = c;
    	}
    	template<class T>
    	streambufcout &operator << (T x) {
    		static int st[233], top;
    		if (x < 0) {
    			x = -x;
    			pc('-');
    		}
    		do {
    			st[++top] = x % 10;
    			x /= 10;
    		} while (x);
    		while (top)
    			pc(st[top--] ^ 48);
    		return *this;
    	}
    	streambufcout &operator << (char c) { pc(c); return *this; }
    } cout;
    
    int n;
    const int N = 262144;
    const int mod = 1e9 + 7;
    const int inv2 = mod + 1 >> 1;
    int s[N], fib[N], cnt[N];
    int A[N], B[N], C[N], F[19][N], h[N], answer[N];
    
    int add(int x, int y) {
    	if (x + y >= mod)
    		return x + y - mod;
    	else
    		return x + y;
    }
    int dec(int x, int y) {
    	if (x - y < 0)
    		return x - y + mod;
    	else
    		return x - y;
    }
    
    void mul(int &x, int y) { x = 1ll * x * y % mod; }
    void _ad(int &x, int y) { x += y; if (x >= mod) x -= mod; }
    void _de(int &x, int y) { x -= y; if (x < 0) x += mod; }
    
    void FWTOR(int *f, int inv) {
    	for (int len = 1; len < N; len <<= 1) {
    		for (int i = 0; i < N; i += len << 1) {
    			for (int j = 0; j < len; j++) {
    				if (inv > 0)
    					_ad(f[i + j + len], f[i + j]);
    				else
    					_de(f[i + j + len], f[i + j]);
    			}
    		}
    	}
    }
    
    void FWTAND(int *f, int inv) {
    	for (int len = 1; len < N; len <<= 1) {
    		for (int i = 0; i < N; i += len << 1) {
    			for (int j = 0; j < len; j++) {
    				if (inv > 0)
    					_ad(f[i + j], f[i + j + len]);
    				else
    					_de(f[i + j], f[i + j + len]);
    			}
    		}
    	}
    }
    
    void FWTXOR(int *f, int inv) {
    	for (int len = 1; len < N; len <<= 1) {
    		for (int i = 0; i < N; i += len << 1) {
    			for (int j = 0; j < len; j++) {
    				int x = f[i + j], y = f[i + j + len];
    				f[i + j] = add(x, y), f[i + j + len] = dec(x, y);
    				if (inv < 0) 
    					mul(f[i + j], inv2), mul(f[i + j + len], inv2);
    			}
    		}
    	}
    }
    
    int main() {
    	for (int i = 1; i < 262144; i++)
    		cnt[i] = cnt[i >> 1] + (i & 1);
    	fib[0] = 0; fib[1] = 1;
    	for (int i = 2; i < 262144; i++)
    		fib[i] = add(fib[i - 1], fib[i - 2]);
    	cin >> n;
    	for (int i = 1; i <= n; i++) { int x; cin >> x; A[x]++; B[x]++; C[x]++; }
    	for (int i = 0; i < 262144; i++) 
    		F[cnt[i]][i] = A[i], A[i] = 0;
    	for (int i = 0; i < 19; i++)
    		FWTOR(F[i], 1);
    	for (int i = 0; i < 19; i++) {
    		for (int j = 0; j < 262144; j++)
    			h[j] = 0;
    		for (int j = 0; j <= i; j++)
    			for (int k = 0; k < 262144; k++)
    				_ad(h[k], 1ll * F[j][k] * F[i - j][k] % mod);
    		FWTOR(h, -1);
    		for (int j = 0; j < 262144; j++)
    			if (cnt[j] == i)
    				_ad(A[j], h[j]);
    	}
    	FWTXOR(C, 1);
    	for (int i = 0; i < 262144; i++)
    		mul(C[i], C[i]);
    	FWTXOR(C, -1);
    	for (int i = 0; i < 262144; i++)
    		mul(A[i], fib[i]), mul(B[i], fib[i]), mul(C[i], fib[i]);
    	FWTAND(A, 1), FWTAND(B, 1), FWTAND(C, 1);
    	for (int i = 0; i < 262144; i++)
    		answer[i] = 1ll * A[i] * B[i] % mod * C[i] % mod;
    	FWTAND(answer, -1);
    	int Answer = 0;
    	for (int i = 1; i < 262144; i <<= 1)
    		_ad(Answer, answer[i]);
    	cout << Answer << '
    ';
    	return 0;
    }
    
  • 相关阅读:
    mysql find_int_set
    PHPSTROM8.0 注册码(7.1也可用)
    gym 101657 D
    gym101657 C
    poj 3525
    poj1279
    poj3335
    poj 1228
    poj 1873
    poj 2074
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/13502747.html
Copyright © 2011-2022 走看看