loj #161 子集卷积
求不相交集合并卷积
sol:
集合并卷积?看我 FWT!
交一发,10 以上的全 T 了
然后经过参考别人代码认真比对后发现我代码里有这么一句话:
rep(s, , MAXSTATE) rep(i, , n) rep(j, , n - i) h[i + j][s] = inc(h[i + j][s], mul(f[i][s], g[j][s]));
把它改成
rep(i, , n) rep(j, , n - i) rep(s, , MAXSTATE) h[i + j][s] = inc(h[i + j][s], mul(f[i][s], g[j][s]));
就过了...
有理有据地分析一波,上面那种写法会访问 $O(2^n)$ 次不连续的空间,下面那种写法只有 $O(n)$ 次
写出来主要还是提醒自己以后数组访问尽量连续吧...
orz
#include <bits/stdc++.h>
#define LL long long
#define rep(i, s, t) for (register int i = (s), i##end = (t); i <= i##end; ++i)
#define dwn(i, s, t) for (register int i = (s), i##end = (t); i >= i##end; --i)
using namespace std;
namespace IO{
const int BS=(<<)+; int Top=;
char Buffer[BS],OT[BS],*OS=OT,*HD,*TL,SS[]; const char *fin=OT+BS-;
char Getchar(){if(HD==TL){TL=(HD=Buffer)+fread(Buffer,,BS,stdin);} return (HD==TL)?EOF:*HD++;}
void flush(){fwrite(OT,,OS-OT,stdout);}
void Putchar(char c){*OS++ =c;if(OS==fin)flush(),OS=OT;}
void write(int x){
if(!x){Putchar('');return;} if(x<) x=-x,Putchar('-');
while(x) SS[++Top]=x%,x/=;
while(Top) Putchar(SS[Top]+''),--Top;
}
int read(){
int nm=,fh=; char cw=Getchar();
for(;!isdigit(cw);cw=Getchar()) if(cw=='-') fh=-fh;
for(;isdigit(cw);cw=Getchar()) nm=nm*+(cw-'');
return nm*fh;
}
}
using namespace IO;
const int mod = 1e9 + , maxn = ( << );
int n;
int f[][maxn], g[][maxn], h[][maxn], bt[maxn];
inline int inc(int x, int y) {
x += y;
if (x >= mod)
x -= mod;
return x;
}
inline int dec(int x, int y) {
x -= y;
if (x < )
x += mod;
return x;
}
inline int mul(int x, int y) { return 1LL * x * y % mod; }
void fwt(int *a, int n, int f) {
for (int i = ; i < n; i <<= ) {
for (int j = ; j < n; j += (i << )) {
for (int k = ; k < i; k++) {
int x = a[j + k], y = a[j + k + i];
if (f == )
a[j + k + i] = inc(x, y);
else
a[j + k + i] = dec(y, x);
}
}
}
}
int main() {
n = read();
int MAXSTATE = ( << n) - ;
rep(s, , MAXSTATE) bt[s] = __builtin_popcount(s);
rep(s, , MAXSTATE) f[bt[s]][s] = read();
rep(s, , MAXSTATE) g[bt[s]][s] = read();
rep(s, , n) fwt(f[s], MAXSTATE + , ), fwt(g[s], MAXSTATE + , );
rep(i, , n) rep(j, , n - i) rep(s, , MAXSTATE) h[i + j][s] = inc(h[i + j][s], mul(f[i][s], g[j][s]));
//rep(s, 0, MAXSTATE) rep(i, 0, n) rep(j, 0, n - i) h[i + j][s] = inc(h[i + j][s], mul(f[i][s], g[j][s]));
rep(s, , n) fwt(h[s], MAXSTATE + , -);
rep(s, , MAXSTATE) write(h[bt[s]][s]), Putchar(' ');
Putchar('\n'); flush();
}
loj #161 子集卷积的更多相关文章
- CF914G Sum the Fibonacci FWT、子集卷积
传送门 一道良心的练习FWT和子集卷积的板子-- 具体来说就是先把所有满足\(s_a \& s_b = 0\)的\(s_a \mid s_b\)的值用子集卷积算出来,将所有\(s_a \opl ...
- CF 914G Sum the Fibonacci——子集卷积
题目:http://codeforces.com/contest/914/problem/G 第一个括号可以子集卷积:第三个括号可以用 FWT 异或卷积:这样算出选两个数组成 x 的方案数:三个部分的 ...
- UOJ 348 【WC2018】州区划分——子集卷积
题目:http://uoj.ac/problem/348 参考:https://www.cnblogs.com/NaVi-Awson/p/9242645.html#%E5%AD%90%E9%9B%86 ...
- hdu 6057 Kanade's convolution(子集卷积)
题解: 然后就是接下来如何fwt 也就是如何处理bit(x) - bit(y) = bit(k)这个条件. 其实就是子集卷积. 把bit(x)和bit(y)划分成两个集合,然后就是子集卷积的形式. 这 ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- UOJ #348 州区划分 —— 状压DP+子集卷积
题目:http://uoj.ac/problem/348 一开始可以 3^n 子集DP,枚举一种状态的最后一个集合是什么来转移: 设 \( f[s] \) 表示 \( s \) 集合内的点都划分好了, ...
- 【学习笔记】fwt&&fmt&&子集卷积
前言:yyb神仙的博客 FWT 基本思路:将多项式变成点值表达,点值相乘之后再逆变换回来得到特定形式的卷积: 多项式的次数界都为\(2^n\)的形式,\(A_0\)定义为前一半多项式(下标二进制第一位 ...
- UOJ348 WC2018 州区划分 状压DP、欧拉回路、子集卷积
传送门 应该都会判欧拉回路吧(雾 考虑状压DP:设\(W_i\)表示集合\(i\)的点的权值和,\(route_i\)表示点集\(i\)的导出子图中是否存在欧拉回路,\(f_i\)表示前若干个城市包含 ...
- Future Failure CodeForces - 838C (博弈论,子集卷积)
大意: 两人轮流操作一个长$n$, 只含前$k$种小写字母的串, 每次操作删除一个字符或者将整个串重排, 每次操作后得到的串不能和之前出现过的串相同, 求多少种串能使先手必胜. 找下规律发现$n$为奇 ...
随机推荐
- python之数据的序列化
参考博客:http://www.cnblogs.com/yyds/p/6563608.html 数据的序列化功能表 json.dumps() 将python数据类型转换为(json)字符串 json. ...
- offsetHeight+scrollHeight+clientHeight
ch 窗口可见区域高度 :ch = padding + height(height不是所有内容的高度,是样式定义的高度) oh border以内的内容高度: oh = border + padding ...
- ArchiMate进行业务架构建模的参考
业务服务视图 业务渠道视图 业务服务实现视图 业务角色协作视图 业务流程协作视图 业务流程视图 业务对象视图 产品化业务服务视图 分层视图 除了以上内容,在TOGAF中完整的推荐视图是 在ArchiM ...
- ETL应用:使用Pro*C写入文件信息入库的方法
ETL处理过程中,经常需要进行文件校验,如文件级校验.记录级校验,需要保存文件的基本信息,文件名.文件大小.数据日期等,使用Pro*C的一种方法如下: #include <stdio.h> ...
- SQL查询语句的执行顺序
- 登陆weblogic后页面控制台卡主
输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...
- cocos2dx打飞机项目笔记二:BulletLayer类
BulletLayer.h 内容如下 class BulletLayer : public cocos2d::CCLayer { public: CC_SYNTHESIZE(bool, m_IsHer ...
- C# 利用TTS实现文本转语音
TSS(Text To Speech),语音朗读文本的技术,在Windows下台下,微软给我们提供了一套API接口(Speech API).金山词霸的朗读功能就是用的这个接口. WindowsXP自带 ...
- vRA7 Business error “Untrusted certificate chain”
报错截图: 服务无法注册 第一步:登录vRB 5480页面,取消到vRA的注册 第二部:SSH登录到VRB中,查看bio-ssl.keystore.password. cat /shared/cata ...
- 关于view里面xib的问题
[[[NSBundle mainBundle] loadNibNamed:@"NetFailView" owner:self options:nil] lastObject]; 会 ...