Disbursement on Quarantine Policy
InterviewTime limit2sMemory limit1024 MB
Given a seat grid with known infections and 50% uncertain ones, find the expected total quarantine days modulo 1e9+7.
- Level
Medium5 of 10
- Topics
- Probability, Brute force, Math
- Solved
- No attempts yet
Statement
The 2019 novel coronavirus, COVID-19, can be transmitted between humans through water droplets and close contact. The transmission is especially easy and fast in relatively crowded or confined spaces, such as airplanes or trains. If someone is infected with COVID-19, then passengers occupying the adjacent seats will be infected easily.
There is a train with rows, and there are seats per row. All seats are occupied. For some passengers, we know whether they are infected. For the others, we are not sure, so we assume each of them has a chance of being infected, independently of each other.
All infected passengers must be quarantined for days. Passengers who are not infected but are edge-adjacent to an infected passenger must be quarantined for days. Passengers who are not infected, are not edge-adjacent to any infected passenger, but are corner-adjacent to one must be quarantined for days.
Passengers must stay in a hotel during quarantine, and the government pays for the hotel. As an accountant for the government, find the expected total number of quarantine days, which is the expected total hotel cost.
Source: Quarantine Policy, 2023 ICPC Taoyuan Regional Contest, Problem D
Input
The first line contains five integers , , , , and . The next lines each contain characters. The -th character of the -th line describes the passenger in the -th seat of the -th row. Each character is V, ., or ?. V means the passenger is infected, . means the passenger is not infected, and ? means the passenger has a chance of being infected.
Output
Print the expected total number of quarantine days modulo . The answer can be written as an irreducible fraction , where is not a multiple of . Print modulo , where is the multiplicative inverse of modulo . That is, is the multiplicative inverse of if .
Constraints