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$为奇 ...
随机推荐
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- python 课堂笔记-for语句
for i in range(10): print("----------",i) for j in range(10): print("world",j) i ...
- JAVA 判断对象内容是否含有空值
简单判断对象是否含有NULL值,以及信息描述. package com.sicdt.sicsign.bill.api.util; import java.lang.reflect.Invocation ...
- Bürkert 流体控制系统 (8611 型通用调节器)
Type Description High-Tech Made EasyThe new universal controller eCONTROL Type 8611 brings an essent ...
- centos7环境下zookeeper的搭建步骤之单机伪集群
首先说明:这里是单机版的伪集群搭建 第一步:下载zookeeper:zookeeper的下载地址: http://mirror.bit.edu.cn/apache/zookeeper/ 第二步:安装: ...
- 缩略图悬浮效果的jQuery焦点图
在线演示 本地下载
- iOS上架被拒原因及解决办法
简单的记录一下,近期APP上架所遇到的坑爹事儿吧!! 第一次提交: 第二天给了回复,内容如下: .Guideline - Performance - Software Requirements You ...
- (十三)linux文件系统详解(基于ext2文件系统)【转】
本文转载自:https://blog.csdn.net/FadeFarAway/article/details/53959639 我们知道,一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如 ...
- java深入探究16-mybatis
链接:http://pan.baidu.com/s/1skJ4TNB 密码:koo9 1.引入mybatis jsbc简单易学,上手快,非常灵活构建SQL,效率高但代码繁琐,难以写出高质量的代码 hi ...
- Elasticsearch安装笔记
下载安装包 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.zip 开始执行bin/./el ...