P3713 [BJOI2017] 机动训练
题意
给一张 \(n\times m\) 的网格图,每个点有一个字符。
对于一条路径,如果每一步离终点的曼哈顿距离都变小了,那么祂是合法的。
对于两条路径,如果把祂们经过的点的上的字符拼起来得到的字符串相同,那么称祂们是同一类的。
求出每个类的路径数量的平方的和,对 \(10^9+7\) 取模。
\(n,m\le30\)。
思路
考虑两个人走,统计走出来的合法且两条路径属于同一类的方案数。容易发现,这个东西和每个类的路径数量的平方的和是相等的。
观察合法的路径,发现只有一下几种组合:
- 上,右上,右。
- 上,左上,左。
- 下,右下,右。
- 下,左下,左。
对两个人分别枚举一个方向然后跑记忆化搜索即可。
但是这样会算重。比如一个人一直向左走,那么祂的路径会在 \(2\) 和 \(4\) 两个方向中被统计。所以需要容斥,把其中一个人只走上,左,下,右四个方向中的一个的方案数减掉,再把两个人都只走上,左,下,右四个方向中的一个的方案数加上。
代码
/*
Luogu P3713 [BJOI2017] 机动训练
2026-04-17
*/
#include<bits/stdc++.h>
using namespace std;
namespace IO{
template<typename T>
inline void read(T&x){
x=0;char c=getchar();bool f=0;
while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
f?x=-x:0;
}
template<typename T>
inline void write(T x){
if(x==0){putchar('0');return ;}
x<0?x=-x,putchar('-'):0;short st[50],top=0;
while(x) st[++top]=x%10,x/=10;
while(top) putchar(st[top--]+'0');
}
inline void read(char&c){c=getchar();while(isspace(c)) c=getchar();}
inline void write(char c){putchar(c);}
inline void read(string&s){s.clear();char c;read(c);while(!isspace(c)&&~c) s+=c,c=getchar();}
inline void write(string s){for(int i=0,len=s.size();i<len;i++) putchar(s[i]);}
template<typename T>inline void write(T*x){while(*x) putchar(*(x++));}
template<typename T,typename...T2> inline void read(T&x,T2&...y){read(x),read(y...);}
template<typename T,typename...T2> inline void write(const T x,const T2...y){write(x),putchar(' '),write(y...),sizeof...(y)==1?putchar('\n'):0;}
}using namespace IO;
template<int mod>struct Modint{
int z;
Modint(){z=0;}
Modint(int x){x%=mod;z=x<0?x+mod:x;}
Modint(long long x){x%=mod;z=x<0?x+mod:x;}
Modint(short x){x%=mod;z=x<0?x+mod:x;}
Modint(char x){x%=mod;z=x<0?x+mod:x;}
Modint(bool x){x%=mod;z=x<0?x+mod:x;}
friend Modint operator+(Modint t,Modint t2){Modint ans;ans.z=(t.z+t2.z)%mod;return ans;}
friend Modint operator*(Modint t,Modint t2){Modint ans;ans.z=1ll*t.z*t2.z%mod;return ans;}
friend Modint operator-(Modint t,Modint t2){Modint ans;ans.z=(t.z-t2.z)%mod;return ans;}
Modint operator<<(const int t)const{Modint ans;ans.z=(z<<t)%mod;return ans;}
Modint operator>>(const int t)const{Modint ans;ans.z=(z>>t)%mod;return ans;}
Modint&operator+=(const Modint t){z=(z+t.z)%mod;return *this;}
Modint&operator*=(const Modint t){z=1ll*z*t.z%mod;return *this;}
Modint&operator-=(const Modint t){z=(z-t.z)%mod;return *this;}
Modint&operator<<=(const int t){z=(z<<t)%mod;return *this;}
Modint&operator>>=(const int t){z=(z>>t)%mod;return *this;}
Modint&operator++(){z++,z%=mod;return *this;}
Modint&operator--(){z--,z%=mod;return *this;}
Modint operator++(int){Modint ls=*this;z++,z%=mod;return ls;}
Modint operator--(int){Modint ls=*this;z--,z%=mod;return ls;}
friend Modint ksm(Modint a,int b){
Modint ans=1;
while(b){if(b&1) ans=ans*a;a=a*a,b>>=1;}
return ans;
}
friend void read(Modint&z){
int x=0;char c=getchar();bool f=0;
while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
while(isdigit(c)) x=(x*10ll+c-'0')%mod,c=getchar();
f?x=-x:0;
z.z=x;
}
friend void write(Modint x){x.z<0?x.z+=mod:0;write(x.z);}
};
int fx[10][10]={
{-1,-1,0},
{-1,-1,0},
{0,1,1},
{1,1,0},
{-1},
{0},
{1},
{0},
};
int fy[10][10]={
{0,-1,-1},
{0,1,1},
{1,1,0},
{0,-1,-1},
{0},
{1},
{0},
{-1},
};
int sz[10]={3,3,3,3,1,1,1,1};
const int maxn=50,mod=1000000009;
#define M Modint<mod>
int n,m;
M f[maxn][maxn][maxn][maxn],ans;
bool vis[maxn][maxn][maxn][maxn];
char c[maxn][maxn];
M dfs(int x,int y,int xx,int yy,int f1,int f2){
if(x<1||x>n||y<1||y>m) return 0;
if(xx<1||xx>n||yy<1||yy>m) return 0;
if(c[x][y]!=c[xx][yy]) return 0;
if(vis[x][y][xx][yy]) return f[x][y][xx][yy];
M&ans=f[x][y][xx][yy];ans=1;vis[x][y][xx][yy]=1;
for(int i=0;i<sz[f1];i++) for(int j=0;j<sz[f2];j++){
int nxt_x=x+fx[f1][i],nxt_y=y+fy[f1][i];
int nxt_xx=xx+fx[f2][j],nxt_yy=yy+fy[f2][j];
ans+=dfs(nxt_x,nxt_y,nxt_xx,nxt_yy,f1,f2);
}
return ans;
}
signed main(){
read(n,m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) read(c[i][j]);
M ans;
for(int i=0;i<8;i++) for(int j=0;j<8;j++){
memset(vis,0,sizeof(vis));
M sum=0;
for(int x=1;x<=n;x++) for(int y=1;y<=m;y++) for(int xx=1;xx<=n;xx++) for(int yy=1;yy<=m;yy++)
sum+=dfs(x,y,xx,yy,i,j);
if(i<4&&j<4) ans+=sum;
else if(i>=4&&j>=4) ans+=sum;
else ans-=sum;
}
write(ans);
return 0;
}

浙公网安备 33010602011771号