Codeforces Round #191 (Div. 2) E题

时间:2021-11-27 23:36:51

状态压缩DP,算sum,本来是枚举的,结果TLE了。。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cstdlib>
using namespace std;
#define MOD 1000000007
int p[];
int hash[<<];
int sum[<<];
int k[];
int lowbit(int t)
{
return t&(-t);
}
int main()
{
int n,m,i,j;
scanf("%d",&n);
for(i = ; i < n; i ++)
{
scanf("%d",&p[i]);
}
scanf("%d",&m);
for(i = ; i <= m; i ++)
{
scanf("%d",&k[i]);
}
if(m == ) k[] = k[];
hash[] = ;
for(i = ;i < n;i ++)
sum[<<i] = p[i];
for(i = ; i < (<<n); i ++)
{
sum[i] = sum[i-lowbit(i)] + sum[lowbit(i)];
if(sum[i] == k[]||sum[i] == k[])
continue;
for(j = i; j > ; j -= lowbit(j))
{
hash[i] += hash[i-lowbit(j)];
if(hash[i] > MOD)
hash[i] -= MOD;
}
}
printf("%d\n",hash[(<<n)-]);
return ;
}