loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】

题目链接

loj300

题解

orz litble
膜完题解后,突然有一个简单的想法:
考虑到(2)是质数,考虑Lucas定理:

[{n choose m} = prod_{i = 1} {lfloor frac{n}{2^{i - 1}} floor mod 2^i choose lfloor frac{m}{2^{i - 1}} floor mod 2^i} pmod 2 ]

[{n choose m} = prod_{each.bit.of.n.and.m} {n' choose m'} pmod 2 ]

如果二进制下有任何一位(n)(0)(m)不为(0),那么就会出现(m' > n')的项,结果就为(0)
所以结果不为(0),当且仅当二进制下(m)(n)的子集

所以枚举子集dp即可【(f[i])表示以(A[u] = i)(u)开头的合法子序列个数】
([1,n])枚举子集的复杂度是(O(3^{log(max{a_i})}))

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 250000,maxm = 100005,INF = 1000000000,P = 1e9 + 7;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
int f[maxn],a[maxn],ans,n;
int main(){
	n = read();
	REP(i,n) a[i] = read();
	for (int i = n; i; i--){
		int u = a[i];
		for (int j = u; j; j = (j - 1) & u){
			f[u] = (f[u] + f[j]) % P;
		}
		ans = (ans + f[u]) % P;
		f[u]++;
	}
	printf("%d
",ans);
	return 0;
}

原文地址:https://www.cnblogs.com/Mychael/p/8987024.html