Description

Stan有N个不同的单词,这天,Stan新结交的两个朋友来他这里玩,Stan作为主人,他需要送给他们单词,但由于Stan不能偏心,所以Stan给每个单词一个权值v_i,他需要他这N个单词恰好分配给这两个朋友,这个地方的人很奇怪,他们用来定义自己的喜悦值的方式是把所有得到的单词的权值都位运算and起来的值,所以你需要使得两个朋友的喜悦值是相同的

好学的Stan不满足于求出一种方案,而是想要知道总共有多少种方案数,Stan觉得这个太简单了,所以请你来帮他解决吧。

Input Format

第一行包含一个整数N

第二行包含N个非负整数,表示每个单词的权值

Output Format

输出仅一行,即方案数

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define ll long long
ll f[][][][][],ans=;
int n,a[],mx,bin[];
int b[][],m,num,fa[];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void sbpianfen(){
for (int i=;i<=n;i++){
int now=i&,pre=now^;
if (i==){
f[now][a[i]][][][]=;
f[now][][a[i]][][]=;
continue;
}
for (int j=;j<=mx;j++) for (int k=;k<=mx;k++) for (int l=;l<=;l++) for (int z=;z<=;z++) f[now][j][k][l][z]=;
for (int j=;j<=mx;j++)
for (int k=;k<=mx;k++){
if (k==)
f[now][j&a[i]][k][][]+=f[pre][j][k][][];
f[now][j&a[i]][k][][]+=f[pre][j][k][][];
if (j==)
f[now][j][k&a[i]][][]+=f[pre][j][k][][];
f[now][j][k&a[i]][][]+=f[pre][j][k][][];
}
for (int j=;j<=mx;j++){
f[now][a[i]][j][][]+=f[pre][][j][][];
f[now][j][a[i]][][]+=f[pre][j][][][];
}
}
ans=;
for (int i=;i<=mx;i++)
ans+=f[n&][i][i][][];
printf("%lld\n",ans);
}
int find(int x){
if (fa[x]==x) return x;
else return (fa[x]=find(fa[x]));
}
void dfs(int dep,int delta){
if (dep==m){
ans+=(ll)delta*(((ll)<<num));
return;
}
dfs(dep+,delta);
int tnum=num;
int tmp[];
for (int j=;j<n;j++) tmp[j]=fa[j];
int T;
for (T=;!b[dep][T];T++);
int f=find(T);
for (int j=T+;j<n;j++)
if (b[dep][j]){
int xx=find(j);
if (xx!=f){
fa[xx]=f;
num--;
}
}
dfs(dep+,-delta);
for (int j=;j<n;j++)
fa[j]=tmp[j];
num=tnum;
}
void sxpianfen(){
m=;
for (int i=;i<;i++){
int t=<<i,cnt=;
for (int j=;j<n;j++)
if (a[j]&t) cnt++;
if (cnt==||cnt==n) continue;
for (int j=;j<n;j++)
if (!(a[j]&t)) b[m][j]=;
else b[m][j]=;
m++;
}
for (int i=;i<n;i++)
b[m][i]=,fa[i]=i;
m++;
ans=;
num=n;
dfs(,);
printf("%lld\n",ans);
}
int main(){
n=read();
for (int i=;i<n;i++) a[i]=read();
sxpianfen();
}

CoFun 1612 单词分组(容斥)的更多相关文章

  1. BZOJ 4671 异或图 | 线性基 容斥 DFS

    题面 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中 ...

  2. AcWing 214. Devu和鲜花 (容斥)打卡

    Devu有N个盒子,第i个盒子中有AiAi枝花. 同一个盒子内的花颜色相同,不同盒子内的花颜色不同. Devu要从这些盒子中选出M枝花组成一束,求共有多少种方案. 若两束花每种颜色的花的数量都相同,则 ...

  3. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

  4. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  5. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  6. 【BZOJ-4455】小星星 容斥 + 树形DP

    4455: [Zjoi2016]小星星 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 204  Solved: 137[Submit][Status] ...

  7. cf#305 Mike and Foam(容斥)

    C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. UVa12633 Super Rooks on Chessboard(容斥 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...

  9. PE-1 & 暴模|容斥

    题意: 求1000以下3或5的倍数之和. SOL: 暴模也是兹瓷的啊... 那么就想到了初赛悲催的滚粗...容斥忘了加上多减的数了... 然后对着题...T = 3*333*(1+333)/2 + 5 ...

随机推荐

  1. MVC每层的职责

    MVC模式把应用程序分割成三层:模型.视图和控制器. 模型:模型代表着核心的业务逻辑和数据.模型封装了域实体的属性和行为,并暴露出了实体的属性. 视图: 视图负责转换模型并把它传递给表示层.视图应关注 ...

  2. Delphi - GetUserNameEx(学一下导出Windows API,以及Array Char充当缓冲区的用法,下标必须从零开始)

    (* * Author : http://www.michael-puff.de * Date : 2006-03-29 * License : PUBLIC DOMAIN *) function G ...

  3. 原型模式 - OK

    原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单说来原型模式就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节. 原型模式UML ...

  4. zoj2314 经典 无源汇有上下界最大流 并输出可行流

    ZOJ Problem Set - 2314 Reactor Cooling Time Limit: 5 Seconds      Memory Limit: 32768 KB      Specia ...

  5. Linux企业级项目实践之网络爬虫(24)——定制规则扩展为垂直爬虫

    在垂直搜索的索引建立之前,我们需要到垂直网站上抓取资源并做一定的处理.垂直搜索与通用搜索不同之处在于,通用搜索不需要理会网站哪些资源是需要的,哪些是不需要的,一并抓取并将其文本部分做索引.而垂直搜索里 ...

  6. NOI2010 海拔

    http://www.lydsy.com/JudgeOnline/problem.php?id=2007 平面图网络流. 好吧,其实我只会暴力的网络流,并不会平面图网络流. Orz~ http://w ...

  7. 华为u8800怎样root?

    用SuperOneClick就可以root了 .在手机上面进入设置-应用程序-开发-三个都要勾选.用数据线连接到电脑,确认可正常连接.不行就使用豌豆夹连接,豌豆夹会自动帮你安装手机的驱动.运行Supe ...

  8. chapter 2: Representing and manipulating information

    C allows conversion between unsigned and signed. The rule is that the underlying bit representation ...

  9. android layout属性介绍

    android:id 为控件指定对应的ID android:text 指定控件其中显示的文字,须要注意的是,这里尽量使用strings.xml文件其中的字符串 android:gravity 指定Vi ...

  10. Struts2 页面url请求怎样找action

    1.我们使用最原始的方法去查找action.不同注解. struts.xml文件先配置 <!-- 新闻信息action --> <action name="newsInfo ...