组合计数 ---- 732 Div2 D. AquaMoon and Chess

题目链接


题目大意:

就是给你一个 1 × n 1\times n 1×n的棋盘,上面初始状态下有若干个棋子,对于每个棋子有可以有两种移动的条件(假设第 i i i个位置上有一枚棋子)

  1. 棋子可以移动到 i + 2 i+2 i+2的位置上,如果 i + 1 i+1 i+1的位置上有棋子,第 i + 2 i+2 i+2上没有棋子,并且 i + 2 i+2 i+2没有超过 n n n
  2. 棋子可以移动到 i − 2 i-2 i2的位置上,如果 i − 1 i-1 i1的位置上有棋子,第 i − 2 i-2 i2上没有棋子,并且 i − 2 i-2 i2没有低过 0 0 0

现在给你棋盘的初始状态,问你棋盘有多少种可达的状态?


解题思路:

首先我们发现对于某个1,我们都是两个两个成为一组的移动的,因为每个棋子都是一样的,那么我们发现,对于上面的1和2的操作实际上就是把这两个棋子左右平移一个单位而已。对于单个的棋子是无法移动的
我们手动模拟一下:
eg.
01110 分组 0 11 1 0 ,现在可以分成4块
cg = 1:是可移动的块的数量
c0 = 2:0的数量
c1 = 1:单个1的数量

那么这个棋盘可达的状态有 a n s = C ( c g + c 0 , c g ) ans=C(cg+c0,cg) ans=C(cg+c0,cg);

因为我们知道对于单个1的位置是不用考虑的因为,如果在所有的可移动的棋子固定下来后,单个1的位置也是唯一确实的!!

那么上面就相当于从3块中选出1块放 11 11 11的方案数


#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 998244353;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
   x = 0;char ch = getchar();ll f = 1;
   while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
   while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
   read(first);
   read(args...);
}
int n;
char str[maxn];
ll inv[maxn], rf[maxn];
int main() {
    IOS;
    int T;
    cin >> T;
    inv[1] = 1;
    rf[1] = 1;
    for(int i = 2; i < maxn; ++ i) inv[i] = 1ll*(mod-mod/i)*inv[mod%i]%mod;
    for(int i = 2; i < maxn; ++ i) inv[i] = inv[i] * inv[i-1] % mod;//处理阶乘的逆元
    for(int i = 2; i < maxn; ++ i) rf[i] = 1ll * i * rf[i - 1] % mod;//处理阶乘

    while(T--) {
        cin >> n;
        cin >> str;
        int c0 = 0, cg = 0;
        for(int i = 0;i < n; ++ i) {
           if(str[i] == '1') {
              int ep = 0;
              while(i + ep < n && str[i+ep] == '1') ep ++;
              i = i + ep - 1;
              cg = cg + ep / 2;
           } else c0 ++;
        }
        ll ans = rf[cg + c0] * inv[cg] % mod * inv[c0] % mod;
        cout << ((ans == 0) ? 1 : ans) << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值