Vertex Covers(高维前缀和)
Vertex Covers
时间限制: 5 Sec 内存限制: 128 MB
提交: 5 解决: 3
题目描述
Now, Kamilah shows you an undirected graph G without loops or multiple edges, each vertex of which has a weight.She can evaluate a vertex cover S of G by the product of weights of all vertices belonging to S.Here, the product of an empty set (of numbers) is defined as 1.
You are asked to calculate the sum of the evaluations described above for all vertex covers of G.
输入
For each test case, the first line contains three integers n (1≤n≤36) and m (0≤m≤n(n − 1)/2) which are the number of vertices and the number of edges in the graph G, and q (108≤q≤109 ) which is a prime number for the output.
The second line contains n integers, the i-th of which is the weight of the i-th vertices in G. All weights are in the range of 1 to 109 .
Each of the following m lines contains two integers u and v (1≤u, v≤n) describing an edge between the u-th vertex and the v-th one.
We guarantee that no more than 36 test cases satisfy n > 18.
输出
样例输入
2
4 3 998244353
1 1 1 1
1 2
2 3
3 4
4 6 998244353
1 1 1 1
1 2
1 3
1 4
2 3
2 4
3 4
样例输出
Case #1: 8
Case #2: 5
题意:设一个图的点覆盖的贡献是该点覆集中点权的积,求一个图的所有点覆盖的贡献和。
思路(来自题解):
考虑折半,将点集分为大小接近的两部分 L 和 R,那么边集 分为 L 内部的、R 内部的以及 L 和 R 之间的。
枚举 L 的子集 S,检查是否 L 内部所有边都被覆盖。
再枚举 R 的子集 T,检查是否 R 内部所有边都被覆盖,如果是,那么根据 L 和 R 之间的未覆盖边可以知道 L 的一个合法的子集 T′ 必须要覆盖掉当前的未覆盖边,那么可以在 L 内选出所 有包含 T′ 的可行 S,这样 S+T 就是一个 vertex cover。
由于乘法满足分配率,只需要对 S 做一个高维前缀和就能 快速计算答案。
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std; const int N = ;
long long arr[N];
long long pre[<<]={}; unsigned long long Map[N]={}; int check(int x,int l,int r)
{
for(int i=l;i<=r;i++)
{
if(((<<i)&x)==)
{
unsigned long long now=((Map[i]<<(-r))>>(-r));
if((now&x)!=now)return ;
}
}
return ;
} int check2(int x,int l,int r,int mid)
{
for(int i=l;i<=r;i++)
{
if(((<<i)&x)==)
{
long long now=(Map[i+mid]>>mid);
if((now&x)!=now)return ;
}
}
return ;
} int main()
{
int t,Ca=;
scanf("%d",&t); while(t--)
{
int n,m;
long long mod;
scanf("%d %d %lld",&n,&m,&mod); for(int i=;i<n;i++)scanf("%lld",&arr[i]); memset(Map,,sizeof(Map));
while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
u--;
v--;
Map[u]|=1ll<<v;
Map[v]|=1ll<<u;
} int mid=n/; for(int i=(<<mid)-;i>=;i--)pre[i]=; int upper=(<<mid)-; for(int i=;i<=upper;i++)
{
long long sum=;
for(int j=;j<mid;j++)
if((<<j)&i)sum=(sum*arr[j])%mod; if(check(i,,mid-))pre[i]=sum;
} for(int i=;i<mid;i++) //高维后缀和
for(int j=upper;j>=;j--)
if((j&(<<i))==) pre[j]=(pre[j]+pre[j^(<<i)])%mod; upper=(<<(n-mid))-; long long ans=;
for(int i=;i<=upper;i++)
{
long long sum=;
for(int j=;j<n-mid;j++)
if((<<j)&i)sum=(sum*arr[j+mid])%mod; if(check2(i,,n-mid-,mid))
{
long long base=;
for(int x=;x<=mid-;x++)
{
unsigned long long now=(Map[x]>>mid);
if((now&i)!=now)base|=<<x;
}
ans=(ans+sum*pre[base]%mod)%mod;
}
}
printf("Case #%d: %lld\n",Ca++,ans);
}
return ;
}
Vertex Covers(高维前缀和)的更多相关文章
- BZOJ.5092.[Lydsy1711月赛]分割序列(高维前缀和)
题目链接 \(Description\) \(Solution\) 首先处理\(a_i\)的前缀异或和\(s_i\).那么在对于序列\(a_1,...,a_n\),在\(i\)位置处分开的价值为:\( ...
- HDU.5765.Bonds(DP 高维前缀和)
题目链接 \(Description\) 给定一张\(n\)个点\(m\)条边的无向图.定义割集\(E\)为去掉\(E\)后使得图不连通的边集.定义一个bond为一个极小割集(即bond中边的任意一个 ...
- SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)
题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...
- LOJ2542 PKUWC2018 随机游走 min-max容斥、树上高斯消元、高维前缀和、期望
传送门 那么除了D1T3,PKUWC2018就更完了(斗地主这种全场0分的题怎么会做啊) 发现我们要求的是所有点中到达时间的最大值的期望,\(n\)又很小,考虑min-max容斥 那么我们要求从\(x ...
- Luogu3175 HAOI2015 按位或 min-max容斥、高维前缀和、期望
传送门 套路题 看到\(n \leq 20\),又看到我们求的是最后出现的位置出现的时间的期望,也就是集合中最大值的期望,考虑min-max容斥. 由\(E(max(S)) = \sum\limits ...
- BZOJ5092:[Lydsy1711月赛]分割序列(贪心,高维前缀和)
Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b_2 xor...xor b ...
- HihoCoder - 1496:寻找最大值(高维前缀和||手动求子集)
描述 给定N个数A1, A2, A3, ... AN,小Ho想从中找到两个数Ai和Aj(i ≠ j)使得乘积Ai × Aj × (Ai AND Aj)最大.其中AND是按位与操作. 小Ho当然知道怎么 ...
- BZOJ:5092 [Lydsy1711月赛]分割序列(贪心&高维前缀和)
Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b _2 xor...xor ...
- BZOJ4036:按位或 (min_max容斥&高维前缀和)
Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal 的or)操作.选择数字i的概率是p[i].保证0&l ...
随机推荐
- Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出
题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...
- 收藏的链接-Android
我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法 - 欧阳鹏 - CSDN博客 https://blog.csdn.n ...
- python语句结构(if判断语句)
一.python语句结构分类 条件控制语句:if 语句 if....elif语句 if嵌套 循环语句:while语句 for循环 控制语句:break.continue.pass语句 二.pyt ...
- LeetCode 38.报数(Python3)
题目: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1& ...
- 通过ID获取元素 注:获取的元素是一个对象,如想对元素进行操作,我们要通过它的属性或方法。
通过ID获取元素 学过HTML/CSS样式,都知道,网页由标签将信息组织起来,而标签的id属性值是唯一的,就像是每人有一个身份证号一样,只要通过身份证号就可以找到相对应的人.那么在网页中,我们通过id ...
- csp-s模拟测试54x,y,z题解
题面:https://www.cnblogs.com/Juve/articles/11606834.html x: 并差集,把不能分到两个集合里的元素和并到一起,设连通块个数为cnt,则答案为:$2^ ...
- 期望——邮票收集问题lightoj1342
邮票手机问题: 有n种类型的邮票,问将所有的类型的邮票全部收集起来所要的收集次数期望是多少. 设dp[i]为已经收集了i种类型的票,还要收集n-i种的次数的期望. dp[n]=0; 递推式: dp[i ...
- WebService Exceptions
一. Exception in thread "main" java.lang.ExceptionInInitializerError at com.sun.xml.interna ...
- 手势UIGestureRecognizer
UIGestureRecognizer抽象类,六大手势是其子类: UITapGestureRecognizer 点击 UIPinchGestureRecognizer 缩放 ...
- Flask ——路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...