CF1546D.AquaMoon and Chess(组合数学)

传送门

思路:

考虑简单的情况(0001100)
这样移动后有(0011000)(0000110)情况,可以发现(11)始终是在一起的,也就是说他们的移动位置可以看成是把(1)放到(0)上。
对于连续的多个(1)来说,有效的移动是成对的(11),单独多出来的那个是无法移动的。
问题就传化成了有(cnt0)(0),(cnt1)(11),有几种方案数。
(c[cnt0+cnt1][cnt0])

代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll 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 * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)


const int inf = 0x3f3f3f3f;
#define PI acos(-1)

const int maxn=2e5+100;

const int N = 300010;
const int mod = 998244353;
ll fact[N];//阶乘 
ll infact[N];//逆元 
ll ksm(ll a,ll b,ll p){
	ll res=1;
	while(b){
//&运算当相应位上的数都是1时,该位取1,否则该为0。
		if(b&1)
			res=1ll*res*a%p;//转换为ll型
		a=1ll*a*a%p;
		b>>=1;//十进制下每除10整数位就退一位 
	}
	return res;
}
void init(){
	fact[0]=1;
	infact[0]=1;
	fact[1]=infact[1]=1;
	for(int i=2;i<2e5;i++){
		fact[i]=fact[i-1]*i%mod;
		infact[i]=infact[i-1]*ksm(i,mod-2,mod)%mod;
	}
}

ll cul(ll a,ll b){
	return fact[a]%mod*infact[b]%mod*infact[a-b]%mod;
}


void solve(){
	string s;
	ll n=read;cin>>s;
	ll cnt0=0,cnt1=0,tmp=0;
	rep(i,0,n-1){
		if(s[i]=='1') tmp++;
		else cnt0++,cnt1+=tmp/2,tmp=0;
	}
	cnt1+=tmp/2;
	printf("%lld
",cul(cnt0+cnt1,cnt0));
}

int main(){
	init();
	int _=read;
	while(_--){
		solve();
	}
	
	return 0;
}
/*
10001111110110111000
*/
原文地址:https://www.cnblogs.com/OvOq/p/15001550.html