题目描述

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;但是既然是实验…