Educational Codeforces Round 65 (Rated for Div. 2)D. Bicolored RBS

文章介绍了在满足特定条件下的序列计数问题,利用递推关系、古典概型和模运算解决,涉及MLong和MInt模板的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

考虑有效解的数量/总数,因为要求从小往大结尾是相等才对,所以考虑从小往大枚举 i i i,对每一个 i i i,我们考虑结尾是 i , i i,i i,i的解的个数,因为我们用的是古典概型的枚举法,所以解的形状是 … … i , i … … ……i,i…… ……i,i……,前缀 … … i , i ……i,i ……i,i是合法解,那么怎么算这样的序列个数呢,首先前面是不相等且递增且每个数都小于 i i i的序列,后面在确定前面用去多少数之后,直接乱排就行,我们可以枚举 i , i i,i i,i前面序列的个数 j j j
j ∈ [ 1 , c n t 2 ] j\in[1,cnt2] j[1,cnt2]
c n t 1 [ j ] cnt1[j] cnt1[j]是对应 i , i i,i ii前使用 j j j个数的个数, c i ci ci表示当前枚举的数字 i i i的数量
a n s + = c n t 1 [ j ] ∗ c i ∗ ( c i − 1 ) ∗ A ( n − j − 2 , n − j − 2 ) ans+=cnt1[j]*ci*(ci - 1)*A(n-j-2,n-j-2) ans+=cnt1[j]ci(ci1)A(nj2,nj2)

那么怎么计算 c n t 1 [ j ] cnt1[j] cnt1[j]呢,假如我们已经计算好了第 i − 1 i-1 i1次枚举后的 c n t 1 cnt1 cnt1数组,我们先用这个数组,计算好当前枚举的数字 i i i可以产生的答案,再用当前枚举的 i i i来更新这个 c n t 1 cnt1 cnt1数组,可以得到转移方程
c n t 1 [ i ] + = c n t 1 [ i − 1 ] ∗ c i cnt1 [ i ] += cnt1 [ i - 1 ] * ci cnt1[i]+=cnt1[i1]ci

(总数处理直接 A ( n , n ) A(n,n) A(n,n)上个逆元就是了不会有人不会算逆元吧,逃

这里使用了 jiangly sama 的模与逆元模版

ac 代码

# include  <bits/stdc++.h>
using  namespace  std ;

namespace predefine {
using ll = long long ;
# define  IOS  iostream::sync_with_stdio ( 0 ) ; cin.tie ( 0 ) ;

# define af( i , a , b ) for ( ll i = a ; i <= b ; ++ i )
# define df( i , a , b ) for ( ll i = a ; i >= b ; -- i )
}
using namespace predefine ;

using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
}
template<i64 P>
struct MLong {
    i64 x;
    constexpr MLong() : x{} {}
    constexpr MLong(i64 x) : x{norm(x % getMod())} {}
    
    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) {
        Mod = Mod_;
    }
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const {
        return x;
    }
    explicit constexpr operator i64() const {
        return x;
    }
    constexpr MLong operator-() const {
        MLong res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MLong inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MLong &operator*=(MLong rhs) & {
        x = mul(x, rhs.x, getMod());
        return *this;
    }
    constexpr MLong &operator+=(MLong rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MLong &operator-=(MLong rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MLong &operator/=(MLong rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MLong operator*(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MLong operator+(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MLong operator-(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MLong operator/(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
        i64 v;
        is >> v;
        a = MLong(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MLong lhs, MLong rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MLong lhs, MLong rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
i64 MLong<0LL>::Mod = i64(1E18) + 9;

template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}
    
    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
int MInt<0>::Mod = 998244353;

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;

struct Comb {
    int n;
    std::vector<Z> _fac;
    std::vector<Z> _invfac;
    std::vector<Z> _inv;
    
    Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
    Comb(int n) : Comb() {
        init(n);
    }
    
    void init(int m) {
        m = std::min(m, Z::getMod() - 1);
        if (m <= n) return;
        _fac.resize(m + 1);
        _invfac.resize(m + 1);
        _inv.resize(m + 1);
        
        for (int i = n + 1; i <= m; i++) {
            _fac[i] = _fac[i - 1] * i;
        }
        _invfac[m] = _fac[m].inv();
        for (int i = m; i > n; i--) {
            _invfac[i - 1] = _invfac[i] * i;
            _inv[i] = _invfac[i] * _fac[i - 1];
        }
        n = m;
    }
    
    Z fac(int m) {
        if (m > n) init(2 * m);
        return _fac[m];
    }
    Z invfac(int m) {
        if (m > n) init(2 * m);
        return _invfac[m];
    }
    Z inv(int m) {
        if (m > n) init(2 * m);
        return _inv[m];
    }
    Z binom(int n, int m) {
        if (n < m || m < 0) return 0;
        return fac(n) * invfac(m) * invfac(n - m);
    }
} comb;

void solve ()
{
    ll n ; cin >> n ;
    vector < ll > v ( n ) ;
    af ( i , 0 , n - 1 ) cin >> v [ i ] ;
    map < ll , ll > mp ;
    af ( i , 0 , n - 1 ) ++ mp [ v [ i ] ] ;

    comb.init ( n ) ;
    
    vector < Z > cnt1 ( n + 1 ) ;

    cnt1 [ 0 ] = 1 ;

    Z ans = 0 ;

    ll cnt2 = 0 ;

    for ( auto [ u , ci ] : mp ) {
        af ( i , 0 , cnt2 ) {
            ans += (Z) ci * ( ci - 1 ) * cnt1 [ i ] * comb.fac ( n - i - 2 ) ;
        }
        ++ cnt2 ;
        df ( i , cnt2 , 1 ) {
            cnt1 [ i ] += cnt1 [ i - 1 ] * ci ;
        }
    }

    cout << ans * comb._invfac [ n ] << endl ;
}

int  main () {
	ll _ = 1 ;
	while ( _ -- ) {
		solve () ;
	} 
	return 0 ;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值