Extremely Dangerous Virus

Sort by

recency

|

9 Discussions

|

  • + 0 comments

    Python 3 solution for cases where a+b is even (all test cases):

    def solve(a, b, t):
        # Write your code here
        return int(pow(int((a+b)/2), t, 10**9 + 7))
    
  • + 0 comments

    static int solve(int a, int b, long t) {

        if(t == 1)return (int) ((a + b) / 2);
    
        return (int)pow((a+b)/2,t);
    }
    static long pow(long avg, long t) {
        long x = 1, n = avg;
        while(t > 0) {
            if(t%2 == 1) {
                x=(x * n)%1000000007;
            }
            n = (n * n)%1000000007;
            t /= 2;
        }
        return x;
    }
    
  • + 0 comments

    `

    GIven probability is 0.5 ( 1/2 ) . Factors are a and b . Initially cell is 1

    Every time ,it grows according expected value using factor given a & b

    fIrst time virus cell will be (a*(1/2)) + (b*(1/2)) = (a+b)/2;

    Now cells are (a+b)/2; Now this will be growing into again by factor a & b with probability (1/2) .Let's see ....................

    a*( ( a + b ) / 2 ) * ( 1 / 2 ) + b* ( ( a + b ) / 2 ) * ( 1 / 2 )

    = ( ( a + b ) / 2 ) * (( a + b ) / 2 ) ) , when time is 2 milisecond .

    So from this pattern , we can say ( ( a + b ) / 2 ) ^ t ....... %mod .

    ` My code

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef unsigned long long ll;
    
    const ll mod=1e9+7;
    
    ll power(ll a,ll b)
    {
        ll ans=1;
        while(b)
        {
            if(b&1)
                ans=(ans*a)%mod;
    
                a=(a*a)%mod;
                b=b>>1;
        }
        return ans;
    }
    
    int main()
    {
        ll n,a,b,c,time;
        cin>>a>>b>>time;
        ll exp=(a+b)/2;
        ll result= power(exp,time)%mod;
        cout<<result<<endl;
    }
    
  • + 0 comments

    Initially there is a single virus who replicates into (a+b)/2 expected number of viruses at time=1 ms. These (a+b)/2 new viruses further replicate into (a+b)/2 new viruses each at time=2 ms.
    Let us call (a+b)/2 as mean.
    Total number of viruses at time=2ms:
    =mean*mean
    =mean^2
    After time=t ms, mean^t;
    A simple pow(mean,t) should work if value of t is small. However, in this case, we need to use fast exponentiation.

    long calc(int n, long long pow)      //fast exponentiation 
    {
        if(pow==0)
            return 1;
        if(pow==1)
            return n;
        long p=calc(n,pow/2);
        p=(p*p)%mod;
        if(pow%2!=0)
            p=(p*n)%mod;
        return p;
    
    }
    int main() {
       long long t;
        int a,b;
        cin>>a>>b>>t;
        int mean=(a+b)/2;
        long p=calc(mean,t);
        cout<<p;
        return 0;
    }
    
    
  • + 1 comment

    There seems to be something funny going on with the math here.

    According to my "correct" answer; If there is a 50% probability that the number of cells doubles every millisecond, and you carry out the experiment for 1,000,000 milliseconds, the expected number of cells is still 1.

    Surely this can't be correct?

    (My answer is pretty much the same as Kunal07's)