
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find expected growth of virus after time t in Python
Suppose there is a dangerous virus and that grows rapidly. The probability of number of virus cells growing by a factor x is 0.5 and also the probability of number of virus cells growing by a factor y is 0.5. Now if there was a single cell of virus at beginning, then calculate the expected number of virus cells after t time. If the answer is too large, then mod result by 10^9+7.
So, if the input is like x = 2, y = 4, t = 1, then the output will be 3, because initially, the virus has only one cell. After x time, with probability 0.5, its size is doubled (x2) and, with probability of the other 0.5, its size grows by 4 times. Thus, the expected number of virus cell after time t = 1 is: 0.5*2*1 + 0.5*4*1 = 3.
To solve this, we will follow these steps −
- m = 10^9+7
- factor := floor of (x+y)/2
- res:= 1
- while t > 0, do
- if t is odd, then
- res :=(res*factor) mod m
- factor :=(factor*factor) mod m
- t := floor of t/2
- if t is odd, then
- return res
Example
Let us see the following implementation to get better understanding −
m=10**9+7 def solve(x, y, t): factor=(x+y)//2 res=1 while t > 0: if t % 2: res = (res*factor) % m factor = (factor*factor) % m t = t// 2 return res x = 2 y = 4 t = 1 print(solve(x, y, t))
Input
2, 4, 1
Output
3
- Related Articles
- Program to find expected value of given equation for random numbers in Python
- Python Program to Select the nth Smallest Element from a List in Expected Linear Time
- Python Program to Select the nth Largest Element from a List in Expected Linear Time
- Program to find expected value of maximum occurred frequency values of expression results in Python
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Program to find expected number of shuffle required to sort the elements of an array in Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- C Program to find direction of growth of stack
- Program to find average waiting time in Python
- Program to find final states of rockets after collision in python
- Find the number of spectators standing in the stadium at time t in Python
- Program to find nearest time by reusing same digits of given time in python
- Find the time which is palindromic and comes after the given time in Python
- Program to find state of prison cells after k days in python
- Program to find mean of array after removing some elements in Python
