CF472G Increase the Constraints
Increase the Constraints
定义两个等长的01字符串的汉明距离为它们字符不同的对应位置的个数。
给你两个01串S,T,现在有q个询问,每次指定S,T中两个定长的子串询问它们的汉明距离。
1≤|S|,|T|≤200000,1≤q≤400000
cz_xuyixuan的题解
字符不同=长度-字符相同。考虑到两个字符串的匹配问题可以用FFT处理,于是往FFT方面考虑。
分块FFT,令分块大小为B,进行O(\(\frac{n}{B}\))次FFT,处理出O(\(\frac{n}{B}\))个T的后缀与S的每个后缀能够匹配的位数。询问时容斥一下并加上边角暴力就好了。
这样的时间复杂度是O(\(\frac{n^2\log n}{B}\)+qB),取B=n\(\sqrt{\frac{\log n}{q}}\)=1,327.013205时,可以获得渐进意义下最优复杂度O(n\(\sqrt{q log n}\))。
有一种高妙的做法来解决01匹配问题。我们令0为1,1为-1,然后FFT。那么两个字符如果匹配,得数为1,否则为-1。我们给1和-1的总和加上长度,那么就变成了匹配得2,不匹配得0.
由于NTT常数大,所以程序取B=7200。
CO int N=524288;
int rev[N],omg[N];
void NTT(int a[],int lim,int dir){
for(int i=0;i<lim;++i)
if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int i=1;i<lim;i<<=1)
for(int j=0;j<lim;j+=i<<1)
for(int k=0;k<i;++k){
int t=mul(omg[lim/(i<<1)*k],a[j+i+k]);
a[j+i+k]=add(a[j+k],mod-t),a[j+k]=add(a[j+k],t);
}
if(dir==-1){
int ilim=fpow(lim,mod-2);
for(int i=0;i<lim;++i) a[i]=mul(a[i],ilim);
}
}
CO int B=7200;
char s[N],t[N];int ls,lt;
int index[N],l[N],r[N],tot;
int a[N],b[N],ans[N/B][N];
int query(int ps,int pt){
int ans=0;
if(ps+B>=ls or pt+B>=lt){
for(;ps<ls and pt<lt;++ps,++pt) ans+=s[ps]==t[pt];
return ans;
}
for(;index[pt]==index[pt-1];++ps,++pt) ans+=s[ps]==t[pt];
ans+=::ans[index[pt]][ps];
return ans;
}
int main(){
scanf("%s%s",s,t);
ls=strlen(s),lt=strlen(t);
for(int i=0;i<lt;++i){
if(i%B==0) l[++tot]=i;
index[i]=tot,r[tot]=i;
}
for(int p=1;p<=tot;++p){
memset(a,0,sizeof a);
for(int i=0;i<ls;++i) a[i]=s[i]=='0'?1:mod-1;
memset(b,0,sizeof b);
for(int i=l[p];i<lt;++i) b[lt-1-i]=t[i]=='0'?1:mod-1;
int n=lt-l[p]-1;
int len=ceil(log2(ls+n)),lim=1<<len;
for(int i=0;i<lim;++i) rev[i]=rev[i>>1]>>1|(i&1)<<(len-1);
omg[0]=1,omg[1]=fpow(3,(mod-1)/lim);
for(int i=2;i<lim;++i) omg[i]=mul(omg[i-1],omg[1]);
NTT(a,lim,1),NTT(b,lim,1);
for(int i=0;i<lim;++i) a[i]=mul(a[i],b[i]);
omg[1]=fpow(omg[1],mod-2);
for(int i=2;i<lim;++i) omg[i]=mul(omg[i-1],omg[1]);
NTT(a,lim,-1);
for(int i=0;i<ls;++i){
ans[p][i]=add(a[i+n],add(n+1,mod-max(0,i+n-ls+1))); // edit 1
ans[p][i]=mul(ans[p][i],i2);
}
}
for(int q=read<int>();q--;){
int ps=read<int>(),pt=read<int>(),n=read<int>();
printf("%d\n",n-query(ps,pt)+query(ps+n,pt+n));
}
return 0;
}
处理ans数组的时候还是要放到模意义下,因为1和-1的总和可能为负数。
CF472G Increase the Constraints的更多相关文章
- 【CF472G】Design Tutorial: Increase the Constraints
Description 给出两个01序列\(A\)和\(B\) 要求回答\(q\)个询问每次询问\(A\)和\(B\)中两个长度为\(len\)的子串的哈明距离 哈明距离的值即有多少个位置不相等 ...
- cf 472G Design Tutorial: Increase the Constraints 分块+压位/FFT
题目大意 给出两个\(01\)序列\(A\)和\(B\) 哈明距离定义为两个长度相同的序列中,有多少个对应位置上的数字不一样 "00111" 和 "10101" ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- Propagation of Visual Entity Properties Under Bandwidth Constraints
1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...
- iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制
iOS Programming Auto Layout: Programmatic Constraints 1. However, if your views are created in co ...
- States of Integrity Constraints
States of Integrity Constraints As part of constraint definition, you can specify how and when Oracl ...
- Go build constraints
Go语言有一个不(奇)错(葩)的设计,就是build constraints(构建约束).可以在源码中通过注释的方式指定编译选项,比如只允许在linux下,或者在386的平台上编译啊之类的:还可以通过 ...
- Unable to simultaneously satisfy constraints.
在进行版本的迭代更新时,新功能需求需要对主页面的UI进行重新的布局,但是,报了错误,出了好多约束方面的问题: Unable to simultaneously satisfy constraints. ...
- Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement
Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...
随机推荐
- 二分法构造AVL树
public class ConvertSortedArrayToBinarySearchTree { public static TreeNode sortedArrayToBST(int[] nu ...
- ssh服务器安装测试
ssh服务器搭建 作用:用于远程登录到服务器 (1)服务器端 安装ssh: $ sudo apt-get install openssh-server 查看ssh是否已经安装: $ sudo apti ...
- C#实现ActiveMQ消息队列
本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...
- [转帖]在 Kubernetes 离线部署 KubeSphere
在 Kubernetes 离线部署 KubeSphere https://kubesphere.io/docs/v2.0/zh-CN/installation/install-ks-offline/ ...
- Sitecore安全:访问权限
由于Sitecore使用Core数据库中的项来定义其用户界面,因此您可以对该数据库中的项应用访问权限,以控制对CMS功能的访问.最常见的是,将用户置于预定义的Sitecore客户端角色中 Siteco ...
- 常用Java API之Ramdom--用代码模拟猜数小游戏
常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...
- sqlserver分布式事务
启动服务中的Distributed Transaction Coodinator后 创建链接服务器ender-pc\subx 设定连接服务器RPC OUT 以及RPC属性为True 实验一下代码 创建 ...
- App.config 分开文件
App.config 分开文件 分开文件里配置: <appSettings configSource="App_TestMachine.config"> App_Tes ...
- 详细的Hadoop的入门教程-伪分布模式Pseudo-Distributed Operation
一. 伪分布模式Pseudo-Distributed Operation 这里关于VM虚拟机的安装就不再介绍了,详细请看<VMware虚拟机的三种网络管理模式>一章介绍.这章只介绍hado ...
- w3c网站案例
w3c网站 reset操作 body { background-color: #eee; } html, body, h1, h2, h3, h4, h5, h6, ul, p { margin: 0 ...