Disbursement on Quarantine Policy(概率、逆元計算期望)

題目描述

There is a train with n rows, and there are m seats per row. All seats are occupied. For some passengers, we know they are being infected with COVID-19 or not. However, for other passengers, we are not sure about their status, and we assume each of them has 1/2 chance being infected with COVID-19, independent from each other.
All infected passengers need to be quarantined for d0 days. All passengers that are not infected,but edge-adjacent to any infected passenger, need to be quarantined for d1 days. All passengers that are not infected, not edge-adjacent to any infected passenger, but corner-adjacent to any infected passenger, need to be quarantined for d2 days.
The passengers need to stay in the hotel during quarantine. According to the regulations, the government needs to pay for the hotel. As an accountant of the government, you are asked to evaluate the expected total number of days the passengers need to be quarantined, which indicates the expected total cost on paying for the hotel.
For example, suppose n = 4 , m = 4 , d0 = 15 , d1 = 7 , d2 = 3 . The third passenger in the third row is infected, and we don’t know whether the second passenger in the first row is infected or not. Other 14 passengers are not infected.
If the second passenger in the first row is infected, then the total number of days of quarantine is 91 :
7 15 7 0
3 7 7 3
0 7 15 7
0 3 7 3
If that passenger is not infected, then the total number of days of quarantine is 55 :
0 0 0 0
0 3 7 3
0 7 15 7
0 3 7 3
So the expected total number of days of quarantine is (91+55)/2 = 73 .

輸入

The first line contains five integers n , m , d0 , d1 and d2 . The following n lines contain m characters each. The j -th character of the i -th line represents the passenger occupying the j-th seat of the i -th row. Each character is one of ‘V’, ‘.’, or ‘?’. ‘V’ means an infected passenger, ‘.’ means a not infected passenger, and ‘?’ means a assenger that has 1/2 chance being infected.

輸出

The expected total number of days the passengers need to be quarantined, modulo?10^9+7 .
It can be proved that the answer can be represented by a rational number p/q where q is not a multiple of 10^9+7?. Then you need to print p × q^{-1}?modulo ?10^9+7?, where q^{-1}?means the multiplicative inverse of q modulo?10^9+7 .
Note: If x × q ≡ 1 mod?10^9+7 , then x is the multiplicative inverse of q modulo 10^9+7?.

樣例輸入

【樣例1】

4 4 15 7 3
.?..
....
..V.
....

【樣例2】

2 2 1 1 1
??
??
樣例輸出

【樣例1】
73
【樣例2】
750000009

提示

Technical Specification
? 1 ≤ n ≤ 100
? 1 ≤ m ≤ 100
? 0 ≤ d2 ≤ d1 ≤ d0 ≤ 100

思路分析

本題最終目的是求所有乘客需要被隔離的期望總天數——可以拆解為求每位乘客需要被隔離的天數,然后再相加。至于怎么求每位乘客需要被隔離的期望天數,需要求取每種情況的概率,乘上該種情況對應的天數,再求和。

在求概率的時候用到了逆元

逆元

利用費馬小定理求乘法逆元

b\cdot b_{inv}\equiv 1(mod\ p)

費馬小定理:若p為質數,則a^{p-1}\equiv 1(mod\ p)

#define ll long long
ll fast_power(ll a,ll b,ll mod){if(a==0){if(b==0)return 1;else return 0;}ll ans=1;while(b){if(b&1){ans=(ans*a)%mod;}a=(a*a)%mod;b>>=1;}return ans;
}
ll get_inv(ll x,ll p){return fast_power(x,p-2,p);
}

利用擴展歐幾里得算法

擴展歐幾里得算法:求ax+by=gcd(a,b)的一組x,y

求a在模p意義下的乘法逆元:a\cdot a_{inv}\equiv 1(mod\ p)

void exgcd(int a,int b,int&x,int&y){if(b==0){x=1;y=0;}int gcd=exgcd(b,a%b,y,x);y-=(a/b)*x;
}
int get_inv(int a,int p){int x=1,y=0;exgcd(a,p,x,y);return (x%p+p)%p;
}
代碼
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
int n,m,d0,d1,d2;
ll ans=0;
char a[110][110];
ll v[110][110],p1[110][110],p2[110][110];
ll fast_power(ll a,ll b){if(a==0){if(b==0)return 1;else return 0;}ll ans=1;while(b){if(b&1){ans=(ans*a)%mod;}a=(a*a)%mod;b>>=1;}return ans;
}
ll get_p(char c){if(c=='V')return 1;else if(c=='.')return 0;else{return fast_power(2,mod-2);}
}
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>n>>m>>d0>>d1>>d2;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>a[i][j];v[i][j]=get_p(a[i][j]);}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){ll np1=1;np1=np1*(mod+1-v[i-1][j])%mod;np1=np1*(mod+1-v[i][j-1])%mod;np1=np1*(mod+1-v[i+1][j])%mod;np1=np1*(mod+1-v[i][j+1])%mod;p1[i][j]=(mod+1-np1)%mod;ll np2=1;np2=np2*(mod+1-v[i-1][j-1])%mod;np2=np2*(mod+1-v[i+1][j-1])%mod;np2=np2*(mod+1-v[i+1][j+1])%mod;np2=np2*(mod+1-v[i-1][j+1])%mod;p2[i][j]=(mod+1-np2)%mod;}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){ll part1=d0*v[i][j]%mod;ll part2=d1*(mod+1-v[i][j])%mod*p1[i][j]%mod;ll part3=d2*(mod+1-v[i][j])%mod*(mod+1-p1[i][j])%mod*p2[i][j]%mod;ans=(ans+part1+part2+part3)%mod;}}ans=(ans%mod+mod)%mod;cout<<ans;return 0;
}

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

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

相關文章

AI 在金融領域的落地案例

目錄 引言 一、信貸風控&#xff1a;基于 LoRA 的 Qwen-7B 模型微調&#xff08;適配城商行審批場景&#xff09; 場景背景 核心代碼 1. 環境依賴安裝 2. 金融數據集加載與預處理&#xff08;城商行信貸數據&#xff09; 3. LoRA 微調 Qwen-7B 模型 4. 模型推理&#xf…

平衡二叉樹的調整

平衡二叉樹的定義平衡二叉樹&#xff08;balanced binary tree&#xff09;&#xff0c;又稱AVL樹(Adelson-Velskii and Landis)。 一棵平衡二叉樹或者是空樹&#xff0c;或者是具有下列性質的二叉排序樹&#xff1a;① 左子樹與右子樹的高度之差的絕對值小于等于1&#xff1b;…

深入解析:如何設計靈活且可維護的自定義消息機制

深入解析&#xff1a;如何設計靈活且可維護的自定義消息機制 引言 在現代軟件開發中&#xff0c;組件間的通信機制至關重要。無論是前端框架中的組件交互&#xff0c;還是后端服務間的消息傳遞&#xff0c;一個良好的消息機制能顯著提升代碼的可維護性和擴展性。本文將深入探討…

PostgreSQL——用戶管理

PostgreSQL用戶管理一、組角色管理1.1、創建組角色1.2、查看和修改組角色1.3、刪除組角色二、角色的各種權限2.1、LOGIN&#xff08;登錄&#xff09;2.2、SUPERUSER&#xff08;超級用戶&#xff09;3.3、CREATEDB&#xff08;創建數據庫&#xff09;3.4、CREATEROLE&#xff…

東軟8位MCU使用問題總結

簡介用的單片機為ES7P7021&#xff0c;采用8位RISC內核&#xff0c;2KB的FLASH&#xff0c;128bit的RAM。編譯器使用東軟提供的iDesigner&#xff0c;開發過程中編譯器和單片機有一些地方使用時需要注意下。1.RAMclear()函數注意問題/****************************************…

深度學習在訂單簿分析與短期價格預測中的應用探索

一、訂單簿數據特性及預處理 1.1 訂單簿數據結構解析 在金融交易領域&#xff0c;訂單簿是市場微觀結構的集中體現&#xff0c;它記錄了不同價格水平的買賣訂單信息。一個典型的訂單簿由多個層級組成&#xff0c;每個層級包含特定價格上的買單和賣單數量。例如&#xff0c;在某…

Hashmap源碼

目錄 HashMap底層原理 JDK1.8及以后底層結構為&#xff1a;數組鏈表紅黑樹 默認參數 擴容機制 數組 鏈表 紅黑樹 HashMap為什么用紅黑樹不用B樹 HashMap什么時候擴容 HashMap的長度為什么是 2的 N 次方 HashMap底層原理 JDK1.8及以后底層結構為&#xff1a;數組鏈表紅…

【JAVA 字符串常量池、new String的存儲機制、==與equals的區別,以及字符串重新賦值時的指向變化】

系列文章目錄 提示&#xff1a;這里可以添加系列文章的所有文章的目錄&#xff0c;目錄需要自己手動添加 提示&#xff1a;寫完文章后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄系列文章目錄代碼原理解錯誤邏輯理解理解與修正&#xff1a…

博客項目 Spring + Redis + Mysql

基礎模塊1. 郵箱發送功能最初設計的接口 &#xff08;雛形&#xff09;public interface EmailService {/*** 發送驗證碼郵件** param email 目標郵箱* return 發送的code* throws RuntimeException 如果發送郵件失敗&#xff0c;將拋出異常*/String sendVerificationCode(Stri…

前端處理導出PDF。Vue導出pdf

前言&#xff1a;該篇主要是解決一些簡單的頁面內容導出為PDF1.安裝依賴使用到兩個依賴&#xff0c;項目目錄下運行這兩個//頁面轉換成圖片 npm install --save html2canvas //圖片轉換成pdf npm install jspdf --save 2.創建通用工具類exportPdf.js文件可以保存在工具類目錄下…

【GM3568JHF】FPGA+ARM異構開發板燒錄指南

1. Windows燒錄說明 SDK 提供 Windows 燒寫工具(工具版本需要 V3.31或以上)&#xff0c;工具位于工程根目錄&#xff1a; tools/ ├── windows/RKDevTool 如下圖&#xff0c;編譯生成相應的固件后&#xff0c;設備燒寫需要進入 MASKROM 或 LOADER 燒寫模式&#xff0c;準備…

C++ 多進程編程深度解析【C++進階每日一學】

文章目錄一、引言二、核心概念&#xff1a;進程 (Process)功能與作用三、C 多進程的實現方式四、核心函數詳解1. fork() - 創建子進程函數原型功能說明返回值完整使用格式2. wait() 和 waitpid() - 等待子進程結束函數原型參數與返回值詳解3. exec 系列函數 - 執行新程序函數族…

一周學會Matplotlib3 Python 數據可視化-繪制面積圖(Area)

鋒哥原創的Matplotlib3 Python數據可視化視頻教程&#xff1a; 2026版 Matplotlib3 Python 數據可視化 視頻教程(無廢話版) 玩命更新中~_嗶哩嗶哩_bilibili 課程介紹 本課程講解利用python進行數據可視化 科研繪圖-Matplotlib&#xff0c;學習Matplotlib圖形參數基本設置&…

北京JAVA基礎面試30天打卡11

1.索引創建注意事項 適合的場景 1.頻繁使用where語句查詢的字段 2.關聯字段需要建立索 3.如果不創建索引&#xff0c;那么在連接的過程中&#xff0c;每個值都會進行一次全表掃描 4.分組和排序字段可以建立索引因為索引天生就是有序的&#xff0c;在分組和排序時優勢不言而喻 5…

vscode無法檢測到typescript環境解決辦法

有一個vitereacttypescript項目&#xff0c;在工作電腦上一切正常。但是&#xff0c;在我家里的電腦運行&#xff0c;始終無法檢測到typescript環境。即使出現錯誤的ts語法&#xff0c;也不會有報錯提示&#xff0c;效果如下&#xff1a;我故意將一個string類型&#xff0c;傳入…

【MCP開發】Nodejs+Typescript+pnpm+Studio搭建Mcp服務

MCP服務支持兩種協議&#xff0c;Studio和SSE/HTTP&#xff0c;目前官方提供的SDK有各種語言。 開發方式有以下幾種&#xff1a; 編程語言MCP命令協議發布方式PythonuvxSTUDIOpypiPython遠程調用SSE服務器部署NodejspnpmSTUDIOpnpmNodejs遠程調用SSE服務器部署… 一、初始化項…

vscode使用keil5出現變量跳轉不了和搜索全局不了

vscode使用keil5出現變量跳轉不了&#xff0c;或者未包含文件&#xff0c;或者未全局檢索&#xff1b; 參考如下文章后還會出現&#xff1b; 為什么vscode搜索欄只搜索已經打開的文件_vscode全局搜索只能搜當前文件-CSDN博客 在機緣巧合之下發現如下解決方式&#xff1a; 下載…

命名空間——網絡(net)

命名空間——網絡&#xff08;net&#xff09; 一、網絡命名空間&#xff1a;每個都是獨立的“網絡房間” 想象你的電腦是一棟大樓&#xff0c;每個網絡命名空間就是大樓里的一個“獨立房間”&#xff1a; 每個房間里有自己的“網線接口”&#xff08;網卡&#xff09;、“門牌…

一文讀懂16英寸筆記本的實際尺寸與最佳應用場景

當您搜索"16寸筆記本電腦長寬"時&#xff0c;內心真正在問的是什么&#xff1f;是背包能否容納&#xff1f;桌面空間是否足夠&#xff1f;還是期待屏幕尺寸與便攜性的完美平衡&#xff1f;這個看似簡單的尺寸數字背后&#xff0c;凝結著計算機制造商對用戶體驗的深刻…

Android Studio中創建Git分支

做一些Android項目時&#xff0c;有時候想要做一些實驗性的修改&#xff0c;這個實驗可能需要很多步驟&#xff0c;所以不是一時半會能完成的&#xff0c;這就需要在實驗的過程中不斷修改代碼&#xff0c;且要提交代碼&#xff0c;方便回滾或比較差異&#xff0c;但是既然是實驗…