题解 CF1545B AquaMoon and Chess

· · 题解

题目链接

题目描述

给一个长为 n 格的棋盘,棋盘上每格放或不放棋子,分别记为 1 和 0 。棋子可以越过与之相邻的一个棋子跳到另一侧的空格中,求棋盘上棋子的布局可能数。

题解

可以注意到一个非常明显的特点:关于一对相邻的 1 ,即 11 ,可以在棋盘上自由移动,不需要也不可能分开。

因此我们可以将棋盘上所有的 1 分别看作自由移动的 11 和无法 自由移动 的 1 。

而对于相邻的奇数个 1 ,在他移动时,可以看做 11 向前跳到下一个 0 并将 1 向后挤一格,如:

由此可见,在棋盘上, 11 只会跳到与之相邻或只隔1个棋子的 0 ,而 1 在棋盘上的移动是被动的,因此影响棋盘布局的只有 11 和 0 ,我们可以忽略 1 的影响。

设 11 的数量为 a , 0 的数量为 b 。则答案就是两者的组合顺序,即 C^a_{a+b} 。

实现

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
using namespace std;
bool Handsome;
const int M=1e5+5,P=998244353;
int n,F[M],INV[M];
char s[M];
int FAST(int x,int y){
    int res=1;
    for(;y;y>>=1){
        if(y&1)res=1ll*res*x%P;
        x=1ll*x*x%P;
    }
    return res;
}
void init(){
    F[0]=INV[0]=1;
    rep(i,1,1e5)F[i]=1ll*F[i-1]*i%P;
    INV[100000]=FAST(F[100000],P-2);
    drep(i,99999,1)INV[i]=1ll*INV[i+1]*(i+1)%P;
}
int C(int x,int y){return 1ll*F[x]*INV[y]%P*INV[x-y]%P;}
void solve(){
    scanf("%d%s",&n,s+1);
    int p=1,a=0,b=0;
    for(;p<=n;++p){
        if(s[p]=='1' && s[p+1]=='1'){
            ++a;++p;
        }
        else if(s[p]=='0')++b;
    }
    printf("%d\n",C(a+b,b));
}
bool Most;
int main(){
//  printf("%.2lfMB\n",(&Most-&Handsome)/1024.0/1024.0);
    init();
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}
\mathcal{By}\quad\mathcal{Most}\ \mathcal{Handsome} \mathcal{2021.07.20}