【牛客練習賽137 C】題解

比賽鏈接

C. 變化的數組(Easy Version)

題目大意

一個長度為 n n n 的非負數組 a a a,要求執行 k k k 次操作,每次操作如下:

  • 1 2 \frac{1}{2} 21? 的概率令 a i ← a i + ( a i ? m ) + x , ? i ∈ [ 1 , n ] a_i \leftarrow a_i + (a_i \otimes m) + x, \ \forall i \in [1, n] ai?ai?+(ai??m)+x,??i[1,n]
  • 1 2 \frac{1}{2} 21? 的概率保持 ? a i \forall a_i ?ai? 不變。

∑ i = 1 n a i \sum\limits_{i = 1}^{n}a_i i=1n?ai? 的期望,答案對 998244353 998244353 998244353 取模。

其中 ? \otimes ? 表示按位與,例如 ( 10 ) 2 ? ( 11 ) 2 = ( 10 ) 2 , ( 01 ) 2 ? ( 10 ) 2 = 0 (10)_2 \otimes (11)_2 = (10)_2, \ (01)_2 \otimes (10)_2 = 0 (10)2??(11)2?=(10)2?,?(01)2??(10)2?=0

數據范圍

  • 1 ≤ n ≤ 1 0 6 , 1 \leq n \leq 10^6, 1n106,
  • 1 ≤ m , k ≤ 5 ? 1 0 3 , 1 \leq m, k \leq 5 \cdot 10^3, 1m,k5?103,
  • 0 ≤ x ≤ 1 0 5 , 0 \leq x \leq 10^5, 0x105,
  • 0 ≤ a i ≤ 1 0 9 . 0 \leq a_i \leq 10^9. 0ai?109.

Solution

我們觀察 a i a_i ai? 的增量 ( a i ? m ) + x (a_i \otimes m) + x (ai??m)+x,發現除了給定的 x x x ( a i ? m ) (a_i \otimes m) (ai??m) 只與后 ? log ? 2 m ? \lfloor \log_2 m \rfloor ?log2?m? 位有關,于是記 M = 2 ? log ? 2 m ? + 1 , M = 2^{\lfloor \log_2 m \rfloor + 1}, M=2?log2?m?+1,

這樣一來我們只需要知道 a i ? ( M ? 1 ) a_i \otimes (M - 1) ai??(M?1) 就能知道 a i a_i ai? 的增量。

于是我們把每個 a i a_i ai? 劃分為兩部分,分別是 ? a i M ? \lfloor \frac{a_i}{M} \rfloor ?Mai??? a i ? ( M ? 1 ) a_i \otimes (M - 1) ai??(M?1),我們稱其為高位和低位。

接下來我們就分別求高位和低位的期望 h i s \rm{his} his l o s \rm{los} los,最終的答案就是 h i s × M + l o s \rm{his \times M + los} his×M+los

對于低位來說,我們可以構造一個轉換表 s u f \rm{suf} suf,其中 s u f [ v ] = ( v + ( v ? m ) + x ) ? ( M ? 1 ) , v ∈ [ 0 , M ) . \rm{suf[v]} = (v + (v \otimes m) + x) \otimes (M - 1), \ v \in [0, M). suf[v]=(v+(v?m)+x)?(M?1),?v[0,M).

這樣就可以求出低位和的期望 l o s \rm{los} los

  • 假設有 j j j 次操作讓 a i a_i ai? 發生改變,現在已經求出 j ? 1 j - 1 j?1 次改變時每個低位值的個數,記為 c n t j ? 1 [ v ] cnt_{j - 1}[v] cntj?1?[v],其中 v ∈ [ 0 , M ) v \in [0, M) v[0,M),那么只要對 ? v ∈ [ 0 , M ) \forall v \in [0, M) ?v[0,M) 做一次 s u f \rm{suf} suf 變換就可以得到新的 v ′ v' v 以及 c n t j [ v ′ ] cnt_j[v'] cntj?[v] 了,具體來說就是 c n t j [ s u f [ v ] ] : = c n t j [ s u f [ v ] ] + c n t j ? 1 [ v ] . \rm{cnt_j[suf[v]]} := cnt_j[suf[v]] + cnt_{j - 1}[v]. cntj?[suf[v]]:=cntj?[suf[v]]+cntj?1?[v]. 再對每個 c n t j [ v ′ ] × v ′ cnt_j[v'] \times v' cntj?[v]×v 乘上 j j j 次改變的概率 ( 1 2 ) k ( k j ) , \left(\frac{1}{2}\right)^k{k \choose j}, (21?)k(jk?), 最后求和就是期望。
  • 初始值 c n t 0 [ v ] cnt_0[v] cnt0?[v] 只要對 ? a i \forall a_i ?ai? 記錄 a i ? ( M ? 1 ) a_i \otimes (M - 1) ai??(M?1) 的數量即可。

高位就稍微復雜一些。

我們模仿低位,構造一個高位映射表 p r e \rm{pre} pre,其中 p r e [ v ] = ? ( v + ( v ? m ) + x ) M ? , v ∈ [ 0 , M ) . \rm{pre[v]} = \lfloor \frac{(v + (v \otimes m) + x)}{M} \rfloor, \ v \in [0, M). pre[v]=?M(v+(v?m)+x)??,?v[0,M).

對于高位來說,我們不用期望的原始公式 E ( X ) = ∑ i = 1 N p i X i , E(X) = \sum\limits_{i = 1}^{N}p_i X_i, E(X)=i=1N?pi?Xi?, 而是選擇一個基準 B B B,對其變形得到 E ( X ) = ∑ i = 1 N p i ( X i ? B + B ) = B + ∑ i = 1 N p i ( X i ? B ) . E(X) = \sum\limits_{i = 1}^{N}p_i (X_i - B + B) = B + \sum\limits_{i = 1}^{N}p_i (X_i - B). E(X)=i=1N?pi?(Xi??B+B)=B+i=1N?pi?(Xi??B). 其中 ( X i ? B ) (X_i - B) (Xi??B) 是每個隨機變量取值 X i X_i Xi? 相對于 B B B 的增量。

在高位上我們選擇的基準 B = ∑ i = 1 n ? ( a i + ( a i ? m ) + x ) M ? . B = \sum\limits_{i = 1}^{n}\lfloor \frac{(a_i + (a_i \otimes m) + x)}{M} \rfloor. B=i=1n??M(ai?+(ai??m)+x)??.

接下來就是算高位 增量 的期望了。

我們先給出求和式。

∑ j = 0 k ( 1 2 ) k ( k j ) ∑ i = 0 j ? 1 ∑ v = 0 M ? 1 c n t i [ v ] × p r e [ v ] , \sum\limits_{j = 0}^{k}\left( \frac{1}{2} \right)^k {k \choose j} \sum\limits_{i = 0}^{j - 1}\sum\limits_{v = 0}^{M - 1}cnt_i[v] \times pre[v], j=0k?(21?)k(jk?)i=0j?1?v=0M?1?cnti?[v]×pre[v],

上式中 ( 1 2 ) k ( k j ) \left( \frac{1}{2} \right)^k {k \choose j} (21?)k(jk?) 表示對數組 a a a 做了 j j j 次改變的概率,后面的兩重循環是求從開始到改變 j j j 次的增量和。

對于 j j j,之所以我們要遍歷 i ∈ [ 0 , j ) i \in [0, j) i[0,j),是因為 c n t i [ v ] cnt_i[v] cnti?[v] 是不斷變化的;而低位不需要這樣遍歷則是因為它不用求增量,可以直接獲得值。

但是這個三重循環的復雜度我們無法接受,所以考慮交換求和次序。

∑ j = 0 k ( 1 2 ) k ( k j ) ∑ i = 0 j ? 1 ∑ v = 0 M ? 1 c n t i [ v ] × p r e [ v ] = ∑ i = 0 k ∑ j = i + 1 k ∑ v = 0 M ? 1 ( 1 2 ) k ( k j ) × c n t i [ v ] × p r e [ v ] = ∑ i = 0 k ∑ v = 0 M ? 1 c n t i [ v ] × p r e [ v ] ∑ j = i + 1 k ( 1 2 ) k ( k j ) = ∑ i = 0 k ∑ v = 0 M ? 1 c n t i [ v ] × p r e [ v ] × s [ j ] . \begin{align*} &\sum\limits_{j = 0}^{k}\left( \frac{1}{2} \right)^k {k \choose j} \sum\limits_{i = 0}^{j - 1}\sum\limits_{v = 0}^{M - 1}cnt_i[v] \times pre[v] \\ &= \sum\limits_{i = 0}^{k}\sum\limits_{j = i + 1}^{k}\sum\limits_{v = 0}^{M - 1}\left( \frac{1}{2} \right)^k {k \choose j} \times cnt_i[v] \times pre[v] \\ &= \sum\limits_{i = 0}^{k}\sum\limits_{v = 0}^{M - 1}cnt_i[v] \times pre[v] \sum\limits_{j = i + 1}^{k}\left( \frac{1}{2} \right)^k {k \choose j} \\ &= \sum\limits_{i = 0}^{k}\sum\limits_{v = 0}^{M - 1}cnt_i[v] \times pre[v] \times s[j]. \end{align*} ?j=0k?(21?)k(jk?)i=0j?1?v=0M?1?cnti?[v]×pre[v]=i=0k?j=i+1k?v=0M?1?(21?)k(jk?)×cnti?[v]×pre[v]=i=0k?v=0M?1?cnti?[v]×pre[v]j=i+1k?(21?)k(jk?)=i=0k?v=0M?1?cnti?[v]×pre[v]×s[j].?

其中 s [ j ] = ∑ j = i + 1 k ( 1 2 ) k ( k j ) . s[j] = \sum\limits_{j = i + 1}^{k}\left( \frac{1}{2} \right)^k {k \choose j}. s[j]=j=i+1k?(21?)k(jk?).

這樣就把復雜度降到 O ( M k ) O(Mk) O(Mk) 了。

時間復雜度 O ( m k ) O(mk) O(mk)

  • 雖然說 M = 2 ? log ? 2 m ? + 1 M = 2^{\lfloor \log_2 m\rfloor} + 1 M=2?log2?m?+1,但是量級上最多是 2 m 2m 2m 2 2 2 可以看作常數。

C++ Code

#include <bits/stdc++.h>using u32 = unsigned;
using i64 = long long;
using u64 = unsigned 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;
}
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 += 1) {_fac[i] = _fac[i - 1] * i;}_invfac[m] = _fac[m].inv();for (int i = m; i > n; i -= 1) {_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);}Z Lucas(i64 n, i64 m, int p) {if (n < p and m < p) {return binom(n, m);}return Lucas(n / p, m / p, p) * binom(n % p, m % p);}Z Lucas(i64 n, i64 m) {if (n < Z::getMod() and m < Z::getMod()) {return binom(n, m);}return Lucas(n / Z::getMod(), m / Z::getMod()) * binom(n % Z::getMod(), m % Z::getMod());   }Z perm(int n, int m) {if (n < m or m < 0) {return 0;}return fac(n) * invfac(n - m);}
} comb;template<class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {for (auto &x: v) {is >> x;}return is;
}int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);int n, x, m, k;std::cin >> n >> x >> m >> k;std::vector<int> a(n);std::cin >> a;int lm = std::__lg(m) + 1;int M = 1 << lm;std::vector<int> pre(M);std::vector<int> suf(M);for (int i = 0; i < M; i++) {int v = i + (i & m) + x;pre[i] = v >> lm;suf[i] = v & (M - 1);}Z hi0 = 0;std::vector<int> cnt(M);for (int ai: a) {cnt[ai & (M - 1)]++;hi0 += ai >> lm;}std::vector<Z> binom(k + 1);for (int i = 0; i <= k; i++) {binom[i] = comb.binom(k, i) / power(Z(2), k);}std::vector<Z> s(k + 2);for (int i = k; i >= 0; i--) {s[i] = s[i + 1] + binom[i];}Z los = 0;Z his = hi0;for (int i = 0; i <= k; i++) {Z lo = 0;Z hi = 0;for (int j = 0; j < M; j++) {lo += Z(cnt[j]) * j;hi += Z(cnt[j]) * pre[j];}los += binom[i] * lo;his += s[i + 1] * hi;std::vector<int> ncnt(M);for (int j = 0; j < M; j++) {ncnt[suf[j]] += cnt[j];}cnt = std::move(ncnt);}Z ans = his * M + los;std::cout << ans << "\n";return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/79830.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/79830.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/79830.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Redis適用場景

Redis適用場景 一、加速緩存二、會話管理三、排行榜和計數器四、消息隊列五、實時分析六、分布式鎖七、地理位置數據八、限流九、數據共享十、簽到 一、加速緩存 Redis最常見的應用之一是作為緩存層&#xff0c;用于存儲頻繁訪問的數據&#xff0c;從而減輕數據庫的負載。 通過…

【LangChain4j快速入門】5分鐘用Java接入AI大模型,Spring Boot整合實戰!| 附源碼

【LangChain4j快速入門】5分鐘用Java接入AI大模型&#xff0c;Spring Boot整合實戰&#xff01; 前言&#xff1a;當Java遇上大模型 在AI浪潮席卷全球的今天&#xff0c;Java開發者如何快速擁抱大語言模型&#xff1f;LangChain4j作為專為Java打造的AI開發框架&#xff0c;以…

2025第十七屆“華中杯”大學生數學建模挑戰賽題目B 題 校園共享單車的調度與維護問題完整成品正文33頁(不含附錄)文章思路 模型 代碼 結果分享

校園共享單車運營優化與調度模型研究 摘 要 本研究聚焦校園共享單車點位布局、供需平衡、運營效率及故障車輛回收四大核心問題&#xff0c;通過構建一系列數學模型&#xff0c;系統分析與優化共享單車的運維體系。 針對問題一&#xff0c;我們建立了基于多時段觀測的庫存估算…

Unity游戲多語言工具包

由于一開始的代碼沒有考慮多語言場景&#xff0c;導致代碼中提示框和UI顯示直接用了中文&#xff0c;最近開始提取代碼的中文&#xff0c;提取起來太麻煩&#xff0c;所以拓展了之前的多語言包&#xff0c;降低了操作復雜度。最后把工具代碼提取出來到單獨項目里面&#xff0c;…

.NET MCP 文檔

MCP 概述 MCP&#xff08;Model Context Protocol&#xff09;是由 Anthropic 推出的一種開放協議&#xff0c;類似 AI 的 USB-C 擴展塢&#xff0c;用于在大模型和數據源之間建立安全的通信&#xff08;授權&#xff09;&#xff0c;讓 AI 應用能夠安全地訪問和操作本地或遠程…

【Linux】vim配置----超詳細

目錄 一、插件管理器準備 二、目錄準備 三、安裝插件 一、插件管理器準備 Vim-plug 是一個Vim插件管理器&#xff0c;利用異步并行可以快速地安裝、更新和卸載插件。它的安裝和配置都非常簡單&#xff0c;而且在操作過程中會給出很多易讀的反饋信息&#xff0c;是一個自由、…

PHP實現圖片自動添加水印效果

<?php // 設置原始圖片路徑和水印圖片路徑 $original_image original.jpg; $watermark_image watermark.png;// 創建圖片資源 $original imagecreatefromjpeg($original_image); $watermark imagecreatefrompng($watermark_image);// 獲取圖片尺寸 $original_width im…

檢查新接手LINUX服務器應用的部署情況和正在運行的服務

當接手一臺新的 Linux 服務器時&#xff0c;第一要務就是摸清系統上已經安裝部署了哪些應用和服務。 本文將以 CentOS7為例&#xff0c;詳細介紹如何系統地排查已安裝的應用和服務&#xff0c;包括它們的安裝方式和安裝位置。 1.查看系統基本信息 首先獲取系統整體信息&…

使用注解方式整合ssm時,啟動tomcat掃描不到resource下面的xxxmapper.xml問題,解決方法

解決org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.mapper.方法 在Spring與Mybatis整合時&#xff0c;可能會遇到這樣的報錯 原因&#xff1a; 其原因為mapper路徑的映射錯誤&#xff0c;表示在嘗試執行某個 Mapper 接口的方法時…

C++11特性補充

目錄 lambda表達式 定義 捕捉的方式 可變模板參數 遞歸函數方式展開參數包 數組展開參數包 移動構造和移動賦值 包裝器 綁定bind 智能指針 RAII auto_ptr unique_ptr shared_ptr 循環引用 weak_ptr 補充 總結 特殊類的設計 不能被拷貝的類 只能在堆上創建…

My SQL 索引

核心目標&#xff1a; 理解 mysql 索引的工作原理、類型、優缺點&#xff0c;并掌握創建、管理和優化索引的方法&#xff0c;以顯著提升數據庫查詢性能。 什么是索引&#xff1f; 索引是一種特殊的數據庫結構&#xff0c;它包含表中一列或多列的值以及指向這些值所在物理行的指…

極狐GitLab 注冊限制如何設置?

極狐GitLab 是 GitLab 在中國的發行版&#xff0c;關于中文參考文檔和資料有&#xff1a; 極狐GitLab 中文文檔極狐GitLab 中文論壇極狐GitLab 官網 注冊限制 (BASIC SELF) 您可以對注冊實施以下限制&#xff1a; 禁用新注冊。新注冊需要管理員批準。需要用戶電子郵件確認。…

10.(vue3.x+vite)div實現tooltip功能(css實現)

1:效果截圖 2:代碼實現 <template><div><div class="tooltip" style="margin-top: 20%; margin-left: 20%; background-color: blueviolet; color: white;

Linux下 文件的查找、復制、移動和解壓縮

1、在/var/log目錄下創建一個hehe.log的文件&#xff0c;其文件內容是&#xff1a; myhostname ghl mydomain localdomain relayhost [smtp.qq.com]:587 smtp_use_tls yes smtp_sasl_auth_enable yes smtp_sasl_security_options noanonymous smtp_sasl_tls_security_opt…

Ubuntu 安裝 Docker 教程(官方推薦方式)

? 步驟 1&#xff1a;卸載舊版本&#xff08;如果有&#xff09; for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done---### ? 步驟 2&#xff1a;更新 APT 索引并安裝依賴項bash sudo a…

計算機視覺與深度學習 | Transformer原理,公式,代碼,應用

Transformer 詳解 Transformer 是 Google 在 2017 年提出的基于自注意力機制的深度學習模型,徹底改變了序列建模的范式,解決了 RNN 和 LSTM 在長距離依賴和并行計算上的局限性。以下是其原理、公式、代碼和應用的詳細解析。 一、原理 核心架構 Transformer 由 編碼器(Encod…

計算機基礎 | 常見進制與單位簡介 / 表示 / 描述

注&#xff1a;本文為 “進制與常見單位應用” 相關文章合輯。 原文為繁體&#xff0c;注意術語描述差異。 略作重排。 進制簡介&#xff08;二進制、八進制、十進制、十六進制&#xff09; 發表于 2017-01-20 鄭中勝 數字系統&#xff08;Numeral system&#xff09;&#…

門面模式與適配器模式

一、門面模式 門面模式&#xff1a;提供統一接口訪問子系統接口 1、包含角色 外觀系統對外的統一接口子系統類的集合&#xff1b;并不知道外觀角色的存在&#xff0c;需要為了配合外觀角色而做特殊處理或修改 2、舉例 原本開關燈要分別操作各個房間的燈&#xff0c;現在設置總…

SpringBoot Actuator指標收集:Micrometer與Prometheus集成

文章目錄 引言一、Spring Boot Actuator基礎二、Micrometer簡介與集成三、基本指標收集與配置四、自定義業務指標實現五、與Prometheus集成六、實戰案例&#xff1a;API性能監控總結 引言 在現代微服務架構中&#xff0c;監控應用程序的健康狀況和性能指標變得至關重要。Sprin…

【Android面試八股文】Android應用進程的啟動流程【二】

應用進程 1.1 Android系統進程的啟動過程&#xff1a; 1、init進程fork出Zygote進程后&#xff0c;Zygote進程會創建一個服務端socket&#xff0c;等待AMS發起socket請求。 同時&#xff0c;由Zygote進程fork出的SystemServer進程會啟動各項系統服務&#xff0c;其中就包含了A…