排列数+时间戳+逆元取模

前言:这个题目是真的难,不会做,看了题解才发现是咋回事


题目地址

在这里插入图片描述
在这里插入图片描述

最主要的就是为啥是除以3,c之前需要完成a 和 b,d 和 e 对我们的答案没有影响,所以我们要除以 A(3,3) ,但是 a 和 b 的排列没有要求,所以乘以 A( 2 , 2 ) 抵消得到 3

#include<bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;

const i64 mod = 1e9 + 7;

i64 ksm(i64 a, i64 b) {
    i64 res = 1;
    while(b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }

    return res;
}

void solve() {
    i64 n;
    std::cin >> n;
    std::string s;
    std::cin >> s;
    s = ' ' + s;

    std::stack<i64> st;
    std::vector<i64> cnt(n * 4 + 1);
    i64 cnt0 = 0, cnt1 = 0, num = 0;
    bool flag = 0;
    for (i64 i = 1; i <= 4 * n; i++) {
        st.push(i);
        if (s[i] == '0') {
            cnt0++;
        }
        else {
            cnt1++;
            if (cnt1 + cnt0 >= 4) {
                std::vector<i64> v;
                for (i64 j = 1; j <= 4; j++) {
                    v.push_back(st.top());
                    st.pop();
                }

                if (s[v[0]] == '1' && s[v[1]] == '1' && s[v[2]] == '0' && s[v[3]] == '0') {
                    cnt1 -= 2;
                    cnt0 -= 2;
                    num++;
                    cnt[v[0]] = num;
                    cnt[v[1]] = num;
                    cnt[v[2]] = num;
                    cnt[v[3]] = num;
                }
                else {
                    for (i64 j = 3; j >= 0; j--) {
                        st.push(v[j]);
                    }
                }
            }
        }
    }
    if (st.size()) {
        std::cout << 0 << '\n';
        return;
    }
    i64 res = 1;
    std::vector<i64> mp(n + 1);
    std::set<i64> stt;
    for (i64 i = 1; i <= n; i++) {
        stt.insert(i);
    }
    for (i64 i = 1; i <= 4 * n; i++) {
        if (!mp[cnt[i]]) {
            i64 q = *stt.begin();
            res = res * (cnt[i] - q + 1) % mod;
            stt.erase(cnt[i]);
            mp[cnt[i]] = 1;
        }
    }

    i64 ans = 1;
    for (i64 i = 1; i <= n; i++) {
        ans = ans * i % mod;
    }
    // std::cout << ans << ' ' << res << '\n';
    std::cout << ans * ksm(res, mod - 2) % mod << '\n';
} 

signed main() {
    std::ios::sync_with_stdio(0);
    std::cout.tie(0);
    std::cin.tie(0);

    i64 t = 1; 
    std::cin >> t;
    while (t--) {
        solve();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wniuniu_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值