• CF1392H ZS Shuffles Cards 题解


    CF1392H ZS Shuffles Cards

    题意:有 (n+m) 张牌,其中前 (n) 张牌上分别标着 (1,2,cdots,n) 的数字,后 (m) 张牌是鬼牌。现在我们打乱这些牌,然后开始抽牌游戏,每一轮你可以抽一张牌:

    • 如果抽到了一张标有数字 (x) 的牌,就移除这张牌,并将 (x) 加入一个集合 (S)
    • 如果抽到了鬼牌,就把移除的牌重新加入牌堆,再次打乱所有牌的顺序,重新开始抽牌。如果你抽到了鬼牌,并且集合 (S) 已经包括了 ([1,n]) 中全部 (n) 个数,那么抽牌游戏结束。

    询问抽牌游戏结束的期望轮数。

    题解:我们设每次抽到一张鬼牌并重排牌序之前的抽卡流程为一次迭代,我们先求出这一次迭代的期望轮数,显然可以列出式子:

    [egin{aligned} E(x)&=1 imesfrac{m}{n+m}+2 imesfrac{n}{n+m}frac{m}{n+m-1}+3 imesfrac{n}{n+m}frac{n-1}{n+m-1}frac{m}{n+m-2}+cdots \ &=sum^{n+1}_{i=1}i imesfrac{m}{n+m+1-i}prod^{i-2}_{j=0}frac{n-j}{n+m-j} end{aligned} ]

    直接计算这个式子的复杂度是 (O(n^2log n)) ,显然不行,但是观察一下不难发现后面的连乘式子的重复率非常高,设 (w_k=prod^{k-2}_{j=0}frac{n-j}{n+m-j}) ,那么有递推式:

    [egin{aligned} w_{k+1}=frac{n-k+1}{n+m-k+1}w_k end{aligned} ]

    因此我们可以 (O(nlog n)) 计算出这个期望:

    [E(x)=sum^{n+1}_{i=1}frac{im}{n+m+1-i}w_i ]

    然后我们考虑一共需要迭代几次才能使得集合中包含 (1)(n) 中的所有数。我们设 (f_k) 表示还有 (k) 个不存在于集合中的数时,结束游戏还需要的期望迭代次数。由于我们有 (frac{m}{m+k}) 的概率抽到鬼牌结束这一次迭代;也有可能是 (frac{k}{m+k}) 抽到一张需要的牌,然后跳转到 (k-1) 的子问题,因此不难推出以下转移式:

    [egin{aligned} f_k&=frac{m}{m+k}(f_k+1)+frac{k}{m+k}f_{k-1} \ Leftrightarrow f_k&=f_{k-1}+frac{m}{k} end{aligned} ]

    差分一下不难得到:(f_n=f_1+sum^n_{i=2}frac{m}{i}) ,因此我们现在只需要求出 (f_1) 的值,这实际上就是一个几何分布,直接求出期望为 (frac{1}{frac{1}{m+1}}=m+1) 。因此总共的期望迭代次数 (f_n=m+1+sum^n_{i=2}frac{m}{i}=1+msum^n_{i=1}frac{1}{i})

    本题答案即每次迭代的期望轮数乘以总共的期望迭代次数:

    [egin{aligned} E_{total}=E(x)(1+msum^n_{i=1}frac{1}{i}) end{aligned} ]

    #include <bits/stdc++.h>
    using namespace std;
    
    // 直接声明 Mint 类即可调用
    template <typename T>
    T inverse(T a, T m) { // a % m 的逆元
    	T u = 0, v = 1;
    	while (a != 0) {
    		T t = m / a;
    		m -= t * a; swap(a, m);
    		u -= t * v; swap(u, v);
    	}
    	assert(m == 1);
    	return u;
    }
    
    template <typename T>
    class Modular {
    public:
    	using Type = typename decay<decltype(T::value)>::type;
    
    	constexpr Modular() : value() {}
    	template <typename U>
    	Modular(const U& x) {
    		value = normalize(x);
    	}
    
    	template <typename U>
    	static Type normalize(const U& x) {
    		Type v;
    		if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
    		else v = static_cast<Type>(x % mod());
    		if (v < 0) v += mod();
    		return v;
    	}
    
    	const Type& operator()() const { return value; }
    	template <typename U>
    	explicit operator U() const { return static_cast<U>(value); }
    	constexpr static Type mod() { return T::value; }
    
    	Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
    	Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
    	template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
    	template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
    	Modular& operator++() { return *this += 1; }
    	Modular& operator--() { return *this -= 1; }
    	Modular operator++(int) { Modular result(*this); *this += 1; return result; }
    	Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
    	Modular operator-() const { return Modular(-value); }
    
    	template <typename U = T>
    	typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { // 适配 32 位机需要添加以下代码
    //#ifdef _WIN32
    //		uint64_t x = static_cast<int64_t>(value)* static_cast<int64_t>(rhs.value);
    //		uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
    //		asm(
    //			"divl %4; 
    	"
    //			: "=a" (d), "=d" (m)
    //			: "d" (xh), "a" (xl), "r" (mod())
    //		);
    //		value = m;
    //#else
    		value = normalize(static_cast<int64_t>(value)* static_cast<int64_t>(rhs.value));
    		//#endif
    		return *this;
    	}
    	template <typename U = T>
    	typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
    		int64_t q = static_cast<int64_t>(static_cast<long double>(value)* rhs.value / mod());
    		value = normalize(value * rhs.value - q * mod());
    		return *this;
    	}
    	template <typename U = T>
    	typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
    		value = normalize(value * rhs.value);
    		return *this;
    	}
    
    	Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
    
    	template <typename U>
    	friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
    
    	template <typename U>
    	friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
    
    	template <typename U>
    	friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
    
    private:
    	Type value;
    };
    
    template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
    template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
    template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
    
    template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
    template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
    template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
    
    template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
    
    template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
    template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
    template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
    
    template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
    template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
    template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
    
    template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
    template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
    template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
    
    template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
    template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
    template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
    
    template<typename T, typename U>
    Modular<T> powmod(const Modular<T>& a, const U& b) {
    	assert(b >= 0);
    	Modular<T> x = a, res = 1;
    	U p = b;
    	while (p > 0) {
    		if (p & 1) res *= x;
    		x *= x;
    		p >>= 1;
    	}
    	return res;
    }
    
    template <typename T>
    string to_string(const Modular<T>& number) {
    	return to_string(number());
    }
    
    template <typename T>
    std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
    	return stream << number();
    }
    
    template <typename T>
    std::istream& operator>>(std::istream& stream, Modular<T>& number) {
    	typename common_type<typename Modular<T>::Type, int64_t>::type x;
    	stream >> x;
    	number.value = Modular<T>::normalize(x);
    	return stream;
    }
    
    /*
    using ModType = int;
    
    struct VarMod { static ModType value; };
    ModType VarMod::value;
    ModType& md = VarMod::value;
    using Mint = Modular<VarMod>;
    */
    
    constexpr int md = 998244353; // Mint类模数
    using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
    // 多模数的情况可以声明多个 Mint 例如 using Mint1, Mint2, ...
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr);
    	cout.tie(nullptr);
    	Mint m, n;
    	cin >> n >> m;
    	Mint res = 0, pre = 1;
    	for (int i = 1; i <= (int)n + 1; i++) {
    		Mint tmp = i / (n + m - i + 1);
    		if (i > 1) pre *= (n - i + 2) / (n + m - i + 2);
    		tmp *= pre;
    		res += tmp;
    	}
    	res *= m;
    	Mint h = 0;
    	for (Mint i = 1; i != (n + 1); i++)
    		h += 1 / i;
    	h = h * m + 1;
    	cout << res * h;
    	return 0;
    }
    

    实际上本题的第一部分期望还可以优化:

    [egin{aligned} E(x)&=1 imesfrac{m}{n+m}+2 imesfrac{n}{n+m}frac{m}{n+m-1}+3 imesfrac{n}{n+m}frac{n-1}{n+m-1}frac{m}{n+m-2}+cdots \ &=sum^{n+1}_{i=1}i imesfrac{m}{n+m+1-i}prod^{i-2}_{j=0}frac{n-j}{n+m-j} \ &=frac{n}{m+1}+1 end{aligned} ]

    简单解释:你可以直接变换出来。事实上,我们考虑对于任意一张数字牌而言,拿出它的概率都是 (frac{1}{m+1}) ,根据期望线性性质直接累加得到 (frac{n}{m+1}) ,最后再花费一回合抽出一张鬼牌,即期望为 (frac{n}{m+1}+1)

    #include <bits/stdc++.h>
    using namespace std;
    // Mint类太长省略了
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr);
    	cout.tie(nullptr);
    	Mint m, n;
    	cin >> n >> m;
    	Mint ans = (n + m + 1) / (m + 1);
    	Mint h = 0;
    	for (Mint i = 1; i != (n + 1); i++)
    		h += Mint(1) / i;
    	h = h * m + 1;
    	cout << ans * h;
    	return 0;
    }
    
  • 相关阅读:
    011 Go语言基础之map
    009 Go语言基础之切片
    计算机组成原理 — GPU 图形处理器
    js 实现各浏览器全屏
    js 实现各浏览器全屏
    IIS-真正解决ASP.NET每一个页面首次访问超级慢的问题
    IIS-真正解决ASP.NET每一个页面首次访问超级慢的问题
    首次启动速度慢的问题记录IIS性能优化
    首次启动速度慢的问题记录IIS性能优化
    C#-执行cmd命令,获取结果
  • 原文地址:https://www.cnblogs.com/st1vdy/p/13518667.html
Copyright © 2020-2023  润新知