Codeforces div2 936

E

  1. 我们发现合法的序列必定是 \(a1[0] = 1 , a1[m1 -1] = a2[0] , a2[m2 - 1] = n\)
  2. \(a1[m - 1]\) 的下标分段,两部分是独立的,可以分别计算。
  3. 我们发现 \(a1[0] \rightarrow a1[m1 - 1]\) 就是一段数的排布 \(\tbinom{n - 1}{a1[m - 1] - 1}\) (因为 \(a1[m - 1] = n\) 已经确定了), 这样子 \(a2[0] \rightarrow a2[m2 - 1]\) 是哪些数的排布也确定了。
  4. 那么给定一段数,它有几种排布的方式呢?对于 \([a[i] , a[i + 1] - 1]\) , 如果 \(a[i] + 1 , a[i + 1] - 1\) 已经挑选,那么 \(a[i]\) 就能确定。\(a[i] + 1 , a[i + 1] - 1\) 用组合数选即可。
  5. 没想出来的原因,在于认为 \(a[i]\) 最大值的确定是需要状态的转移的,但通过 \(a1[m - 1] = n\) 的确定就能逆推。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1E9 + 7;

template<const int T>
struct ModInt {
    const static int mod = T;
    int x;
    ModInt(int x = 0) : x(x % mod) {}
    ModInt(long long x) : x(int(x % mod)) {
        if (x < 0){
            int t = 0;
        }
    } 
    int val() { return x; }
    ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
    ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
    bool operator == (const ModInt &a) const { return x == a.x; };
    bool operator != (const ModInt &a) const { return x != a.x; };
    void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
    void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
    void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
    void operator /= (const ModInt &a) { *this = *this / a; }
    friend ModInt operator + (int y, const ModInt &a){ int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    friend ModInt operator - (int y, const ModInt &a){ int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    friend ModInt operator * (int y, const ModInt &a){ return ModInt(1LL * y * a.x % mod);}
    friend ModInt operator / (int y, const ModInt &a){ return ModInt(y) / a;}
    friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x;}
    friend std::istream &operator>>(std::istream &is, ModInt &t){return is >> t.x;}

    ModInt pow(int64_t n) const {
        ModInt res(1), mul(x);
        while(n){
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    
    ModInt inv() const {
        int a = x, b = mod, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        if (u < 0) u += mod;
        return u;
    }
    
};
using mint = ModInt<mod>;

const int N = 2E5 + 5;
mint fac[N] , invfac[N];

void init() {
	fac[0] = invfac[0] = 1;
	for (int i = 1 ; i < N ; ++i) {
		fac[i] = fac[i - 1] * i;
	}
	invfac[N - 1] = fac[N - 1].pow(mod - 2);
	for (int i = N - 2 ; i >= 0 ; --i) {
		invfac[i] = invfac[i + 1] * (i + 1);
	}
}
inline mint C(int n,int m) {
	if (n < 0 || m < 0 || n < m) return 0;
	return fac[n] * invfac[m] * invfac[n - m];
}

void solve() {
	
	int n,m1,m2;
	cin >> n >> m1 >> m2;
	
	std::vector<int> a1(m1),a2(m2);
	for (int i = 0 ; i < m1 ; ++i) {
		cin >> a1[i];
	}
	for (int i = 0 ; i < m2 ; ++i) {
		cin >> a2[i];
	}

	if (a1[0] != 1 || a2[m2 - 1] != n) {
		std::cout << 0 << '\n';
		return;
	}
	if (a1[m1 - 1] != a2[0]) {
		cout << 0 << '\n';
		return;
	}

	int r = a1[m1 - 1];
	int remain = r - 1;

	mint n1 = 1 , n2 = 1;
	for (int i = m1 - 1 ; i >= 1 ; --i) {
		int cnt = a1[i] - a1[i - 1] - 1;
		n1 = n1 * C(remain - 1 , cnt) * fac[cnt];
		remain -= (cnt + 1);
	}
	remain = n - r;
	for (int i = 0 ; i + 1 < m2 ; ++i) {
		int cnt = a2[i + 1] - a2[i] - 1;
		n2 = n2 * C(remain - 1 , cnt) * fac[cnt];
		remain -= (cnt + 1);
	}

	cout << n1 * n2 * C(n - 1 , r - 1) << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	init();

	int T = 1;
	std::cin >> T;
	while (T--) {
		solve();
	}

	return 0;
}	
posted @ 2024-03-25 23:56  xqy2003  阅读(1)  评论(0编辑  收藏  举报