首页 > 其他分享 >CF213E Two Permutations

CF213E Two Permutations

时间:2023-11-04 17:00:32浏览次数:24  
标签:CF213E int Permutations Two pos 哈希

CF213E Two Permutations 题解

下文的 \(a+x\) 表示 \(a_1+x,a_2+x,...a_n+x\) 这个序列。

发现 \(n,m\) 不大,所以可以枚举 \(x\),然后快速判断是否合法。

考虑如何快速判断一个 \(x\) 是否合法。

注意到 \(a,b\) 都是排列

\(a_1+x,a_2+x,...a_n+x\) 的数在 \([1+x,n+x]\) 范围内。

想象一下,把 \(b\) 中不在 \([1+x,n+x]\) 范围内的数都锁起来不管,只留下 \([1+x,n+x]\) 范围内的数,设这时的 \(b\) 数组中没有被锁的数组成的子序列为 \(b’\)。\(b’\) 和 \(a+x\) 相等,那么这个 \(x\) 就是合法的,这里快速判断两个数组相等显然可以想到哈希


怎么求 \(b’\) 的哈希值?

从小到大枚举 \(x\),在上一个 \(x\) 时,\(b’\) 中的数范围为 \([x,n+x-1]\),所以我们只需要在之前的 \(b’\) 的基础上删去 \(x\) 这个数,然后再加上 \(n+x\) 这个数。

设 \(pos[i]\) 为 \(i\) 在 \(b\) 数组中的位置。
在 \(b\) 数组上的操作就是:锁住 \(b_{pos_{x}}\),解锁 \(b_{pos_{n+x}}\)

所以,用线段树维护 \(b’\) 的哈希值。仅需要修改操作即可(具体看代码)


怎么快速求出 \(a+x\) 的哈希值?

自己写一下哈希的式子就知道了。具体见代码。


\(Code\):

#include<bits/stdc++.h>
using namespace std;
int read(){
    int x=0;char c=getchar();bool f=0;
    while(c>'9'||c<'0'){f|=(c=='-');c=getchar();}
    while(c<='9'&&c>='0'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return f?-x:x;
}
typedef long long ll;
const ll mod=1e9+7;
const ll base=31;
const int MAXN=2e5+5;
int a[MAXN],b[MAXN],pos[MAXN];
int n,m;
#define lch (u<<1)
#define rch (u<<1|1)
ll sum[MAXN<<4],cnt[MAXN<<4];
ll pw[MAXN],S;
void up(int u){
    cnt[u]=cnt[lch]+cnt[rch];
    sum[u]=(sum[lch]*pw[cnt[rch]]%mod+sum[rch])%mod;
}
void add(int u,int l,int r,int p,int val){
    if(l==r){
        cnt[u]+=val;
        sum[u]=cnt[u]*b[p]%mod;
        return;
    }
    int mid=l+r>>1;
    if(p<=mid)   add(lch,l,mid,p,val);
    else    add(rch,mid+1,r,p,val);
    up(u);
}
int main(){
    n=read(),m=read();
    ll A=0;pw[0]=1ll;ll S=0;
    for(int i=1;i<=n;i++){
        a[i]=read();
        A=(A*base%mod+a[i])%mod;
        pw[i]=pw[i-1]*base%mod;
        S=(S+pw[i-1])%mod;
    }
    for(int i=1;i<=m;i++){b[i]=read();pos[b[i]]=i;}
    int ans=0;
    //初始所有数都被锁住
    for(int i=1;i<=m;i++){//b数组中出现 [i-n+1,i]
        add(1,1,m,pos[i],1);//解锁 i
        if(i>n) add(1,1,m,pos[i-n],-1);//锁住 i-n
        if(i>=n){
            int x=i-n;
            ll C=(A+x*S%mod)%mod,B=sum[1];//C就是a+x的hash值
            if(C==B)    ans++;
        }
    }
    printf("%d",ans);
    return 0;
}

标签:CF213E,int,Permutations,Two,pos,哈希
From: https://www.cnblogs.com/bwartist/p/17809547.html

相关文章

  • Distilling Knowledge from Graph Convolutional Networks
    目录概符号说明DistillGCNLocalStructurePreserving代码YangY.,QiuJ.,SongM.,TaoD.andWangX.Distillingknowledgefromgraphconvolutionalnetworks.CVPR,2020.概蒸馏表征间的结构关系,教师必须是图网络结构?符号说明\(\mathcal{G}=(\mathcal{V},\m......
  • highcharts network 网络图
    highchartsnetwork网络图要在边上加上箭头,十分困难?Re:HighChartsNetworkGraphArrowLinksWedJul15,20209:47amHi!Welcometoourforumandthanksforcontactinguswithyourquestion!FromtheAPI,thisoptionisnotpossible.Toachievethis,youhavetoext......
  • How to determine the correct number of epoch during neural network training? 如
     Thenumberofepochsisnotthatsignificant.Moreimportantisthethevalidationandtrainingerror.Aslongasitkeepsdroppingtrainingshouldcontinue.Forinstance,ifthevalidationerrorstartsincreasingthatmightbeaindicationofoverfittin......
  • network提示use --host to expose
    项目运行之后,想要通过局域网ip访问项目,无法访问:查了一下问题,没有配置netWork,在vite.config.ts如下配置,就可以了server:{host:'0.0.0.0'} 有问题欢迎交流!!! ......
  • Convolutional neural network (CNN)–extreme learning machine (ELM)
    1.介绍论文:(2020)Neuralnetworksforfacialageestimation:asurveyonrecentadvances.地址:http://link.springer.com/article/10.1007/s10462-019-09765-w针对问题:软生物识别技术在现实世界中的应用日益增多,已成为研究人员感兴趣的一个新领域。它包括对年龄、性别、伤疤、......
  • 题解 [ARC149B] Two LIS Sum
    题解[ARC149B]TwoLISSum大胆猜结论,按照\(a\)数组为关键字进行排序,求更改后\(b\)的\(LIS\)。证明:每次移动,都有\(a\)中增加一个长度,\(b\)中贡献可能为\(\{-1,0,1\}\),总体贡献为\(\{0,1,2\}\),具体为:\(b\)中排序后大数变到小数前方。\(b\)中排序后小数仍然......
  • Graph Neural Networks with Adaptive Residual
    目录概符号说明AirGNN代码LiuX.,DingJ.,JinW.,XuH.,MaY.,LiuZ.andTangJ.Graphneuralnetworkswithadaptiveresidual.NIPS,2021.概基于UGNN框架的一个更加鲁棒的改进.符号说明\(\mathbf{A}\in\mathbb{R}^{n\timesn}\),邻接矩阵;\(\mathbf{D}......
  • Is Homophily a Necessity for Graph Neural Networks?
    目录概MaY.,LiuX.,ShahN.andTangJ.Ishomophilyanecessityforgraphneuralnetworks?ICLR,2022.概探究Homophily假设(即相互连接的结点相似)对于GCN发挥效果是否是必须的.结论是如果图中的同一类的结点具有相似的邻居的分布,则Homophily不是必须的......
  • 单细胞测序 基因调控网络 Gene regulatory networks
    单细胞测序基因调控网络Generegulatorynetworks基因不是独立发挥作用的。相反,基因的表达水平是由与其他基因和小分子之间的复杂调控决定的。揭示这些调控作用是基因调控网络(GRN)推断方法的目标(SCENIC|从单细胞数据推断基因调控网络和细胞类型)。基因调控网络推断是基于对基因共......
  • 【CVPR2023】Learning A Sparse Transformer Network for Effective Image Deraining
    论文:https://readpaper.com/paper/4736105248993591297代码:https://github.com/cschenxiang/DRSformerTransformer模型通常使用标准的QKV三件套进行计算,但是部分来自K的token与来自Q的token并不相关,如果仍然对这些token进行特征聚合计算会影响图像修复的性能。......