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 ...
随机推荐
- HYNB Contest 7:2017 Asia HCMC Vietnam National Programming Contest
A. Another Query on Array Problem B. Board Covering C. Cumulative Sums 题意 \(A_1=1,A_i=A_{i-1}+sod(A_ ...
- VMware Workstation 10 简体中文安装教程
分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 分享到 一键分享 QQ ...
- selenium基础(控制浏览器)
python基础(控制浏览器) 控制浏览器 控制浏览器窗口大小 设置浏览器屏幕大小方法:set_window_size() 浏览器最大化:maximize_window() 浏览器最小化:minimi ...
- Shuffle过程详解
- Echart使用过的属性总结
改变坐标轴颜色与粗细: axisLine: { lineStyle: {//设置轴的颜色 color: '#CD0000', width: 1,//轴的宽度 } } 改变坐标轴上刻度的间隔与倾斜方向: ...
- [sonata admin] argument "$code" of method
2. CREATING AN ADMIN 按照这里,在 AppBundle中的Controller创建了 CategoryAdmin 类,当运行 php bin/console server:star ...
- mysql出现ERROR 1366 (HY000):的解决办法
今天向新建的表中添加内容,出现以下错误: mysql> INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_sho ...
- SQFREE - Square-free integers
SQFREE - Square-free integers 求n以内,约数中不包含任意一个平方数的个数,\(n≤10^{14}\). 解 显然为约数计数问题,于是想办法转换为代数问题,不难列出 \[a ...
- 今天给各位介绍Linux touch命令详述
触摸命令 touch命令有两个功能:一个是将现有文件的时间戳更新为系统的当前时间(默认模式),它们的数据将保持不变;第二个是创建一个新的空文件. 语法 触摸(可选) 最常见的用法:touch file ...
- 廖雪峰Java12maven基础-2maven进阶-1使用插件
1.maven的Lifecycle,Phase和Goal: 使用maven构建项目就是执行Lifecycle 执行Lifecycle就是按顺序执行一系列Phase 每执行一个Phase,都会执行该Ph ...