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

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

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

那么怎么计算cnt1[j]cnt1[j]cnt1[j]呢,假如我们已经计算好了第i−1i-1i1次枚举后的cnt1cnt1cnt1数组,我们先用这个数组,计算好当前枚举的数字iii可以产生的答案,再用当前枚举的iii来更新这个cnt1cnt1cnt1数组,可以得到转移方程
cnt1[i]+=cnt1[i−1]∗ci 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 ;
}
### 关于Codeforces Educational Round 172 Problem D 的解决方案 对于Codeforces Educational Round 172中的D题,虽然未直接提供该题目具体描述以及官方解答[^2],可以基于过往相似难度和类型的题目给出一般性的解决思路。 #### 题目分析 通常情况下,D级别的题目会涉及到较为复杂的算法设计或是数据结构的应用。这类问题往往需要参赛者具备良好的编程基础、逻辑思维能力以及对特定算法的理解掌握程度。针对不同性质的问题(如图论、动态规划、字符串处理等),采取相应的策略来构建模型并求解是最常见的方法之一。 #### 解决方案框架 假设此题属于某种典型问题类别,则可以根据其特点制定如下通用框架: - **输入解析**:仔细阅读题目说明,明确给定条件与目标函数之间的关系。 - **核心概念理解**:深入剖析题目背后所隐藏的关键知识点或技巧点,这可能涉及但不限于贪心算法、二分查找、树形DP等方面的知识。 - **边界情况考虑**:考虑到极端测试用例的存在,在编写程序时要特别注意各种特殊情况下的行为表现,确保代码鲁棒性强。 - **优化空间复杂度/时间效率**:当面对大数据集时,应尽可能寻找更高效的实现方式减少不必要的计算开销;比如利用哈希表加速查询速度,通过位运算代替常规算术操作提高性能等等。 ```cpp // 假设这是一个简化版的伪代码示例 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; // 输入参数 vector<int> data(n); for(auto& d : data){ cin>>d; } // 主体逻辑部分省略... cout << "Result"; return 0; } ``` 由于缺乏具体的题目细节,上述内容仅作为参考模板展示如何着手准备类似的竞赛挑战。为了获得更加精准的帮助建议查阅官方题解文档或者参与社区论坛交流获取更多信息资源。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值