ARC053D 2 つの山札
题意
给定两个长度为
初始你有一个空序列
求本质不同的
分析
我们考虑将原问题抽象化一下:画出一个
下图给出了一个
图片来源于网络。
设
但是我们发现,当
考虑两条会算重的本质相同的路径,它从
发现了这样一些性质:
- 两条路径一定是同时拐弯,因为
是排列,如果某一个提前转弯一定会导致序列不同。由此也可以推出 围成的矩形是正方形,也就是说, 一定是 。由此也可以推出,两条路径关于该正方形的主对角线对称,知道其中一条即可推演出另一条。 - 如果两条路径要在某一时刻拐弯,那么拐弯后两条路径所在的行和列仍然需要满足
。由于是同时拐弯,所以 这个点仍然在正方形的主对角线上。
我们将
得到转移方程
时间复杂度
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0)
#define OTIE cout.tie(0)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define all(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
inline int rd(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
if(x<0){x=-x;putchar('-');}
int y=0;char z[40];
while(x||!y){z[y++]=x%10+48;x/=10;}
while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=5e3+5,maxm=4e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n;
int a[maxn],b[maxn];
int c[maxn];
int ksm(int x,int y){
int res=1;
for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)res=1ll*res*x%mod;
return res;
}
int f[maxn][maxn];
inline void solve_the_problem(){
n=rd();
rep(i,1,n)a[i]=rd();
rep(i,1,n)b[i]=rd();
c[0]=1;
rep(i,1,n){
int res=1ll*(4*i-2)*ksm(i+1,mod-2)%mod;
c[i]=1ll*c[i-1]*res%mod;
}
rep(i,1,n)f[1][i]=f[i][1]=1;
rep(i,2,n)rep(j,2,n){
f[i][j]=(f[i-1][j]+f[i][j-1])%mod;
if(a[i]==b[j]){
int cnt=0;
for(int k=1;i-k>=1&&j-k>=1;++k){
if(a[i-k]==b[j-k]){
int res=1ll*c[cnt]*f[i-k][j-k]%mod;
f[i][j]=(f[i][j]+mod-res)%mod,++cnt;
}
}
}
}
write(f[n][n]);
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
// fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
*/
分类:
计数
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现