【BZOJ4903】【CTSC2017】吉夫特 [DP]

时间:2023-03-09 02:50:35
【BZOJ4903】【CTSC2017】吉夫特 [DP]

吉夫特

Time Limit: 15 Sec  Memory Limit: 512 MB
[Submit][Status][Discuss]

Description

  【BZOJ4903】【CTSC2017】吉夫特 [DP]

Input

  第一行一个整数n。
  接下来n行,每行一个整数,这n行中的第i行,表示ai。

Output

    一行一个整数表示答案。

Sample Input

  4
  15
  7
  3
  1

Sample Output

  11

HINT

  【BZOJ4903】【CTSC2017】吉夫特 [DP]

Main idea

  给定一个序列,问有多少个子序列满足相邻的数构成的组合数都为奇数。

Solution

  首先我们用Lucas定理推一推可以知道:C(n,m)为奇数当且仅当n&m=m

  有了这个定理就好办了,我们可以显然地想到DP:通过枚举数在二进制下的子集转移,这样保证了可以转移过去。

  由于序列每个数都不同,且最大值为233333,所以效率是O(3^18)的。

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std; const int ONE = ;
const int MOD = 1e9+; int n,x;
int f[ONE];
int Ans; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} int main()
{
n = get();
for(int i=; i<=n; i++)
{
x = get();
int record = (f[x] + ) % MOD;
for(int sub=x; sub; sub=(sub-) & x)
f[sub] = (f[sub] + record) % MOD;
Ans = (Ans + record) % MOD;
}
printf("%d", Ans-n);
}