SPOJTLE - Time Limit Exceeded(高维前缀和)

题意

题目链接

题目的意思是给一个数组C,长度为n,每个数字的范围是2^m,然后要求构造一个数组a,满足

1、a[i] % C[i] !=0 ;

2、a[i] < 2^m ;

3、a[i] & a[i+1] = 0;

Sol

直接dp的话就是先枚举补集的子集,这样的复杂度是\(3^n\)

然后补集的子集可以用高位前缀和优化一下

时间复杂度:\(O(2^n * n)\)

#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 1e5 + 10, mod = 1000000000;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, M, a[MAXN], c[MAXN], f[51][65537], sum[51][65537];
int add(int &x, int y) {
	x = (x + y >= mod ? x + y - mod : x + y);
}
int solve() {
	N = read(); M = read(); int Lim = (1 << M) - 1;
	memset(f, 0, sizeof(f));
	memset(sum, 0, sizeof(sum));
	f[0][0] = 1;
	for(int i = 0; i <= Lim; i++) sum[0][i] = 1;
	for(int i = 1; i <= N; i++) {
		c[i] = read();
		for(int sta = 1; sta <= Lim; sta ++) {
			if(!(sta % c[i])) continue;
			int s = (~sta) & Lim;
			sum[i][sta] = f[i][sta] = sum[i - 1][s];
		}
		for(int j = 0; j < M; j++)//必须先枚举这个 
			for(int sta = 0; sta <= Lim; sta++)
				if(sta & (1 << j)) add(sum[i][sta], sum[i][sta ^ (1 << j)]);
	}
	int ans = 0;
	for(int i = 0; i <= Lim; i++) add(ans, f[N][i]);
	return ans;
}
int main() {
	int T = read();
	while(T--) printf("%d\n", solve());
	return 0;
}
posted @ 2018-11-05 20:32  自为风月马前卒  阅读(314)  评论(0编辑  收藏  举报

Contact with me