Sort by

recency

|

983 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/Plsx1i8dqiI

    long strangeCounter(long t) {
        long last = 3, step = 3;
        while(t > last){
            step *= 2;
            last += step;
        }
        return last - t + 1;
    }
    
  • + 0 comments

    C++ solution:

    long strangeCounter(long t) {
        return 6*pow(2,floor(log2((1+(t-1)/3))))-2-t;
    }
    
  • + 0 comments

    Java solution:

    public static long cycleLength(long t){
        long sum = 0;
        long len = 3;
        while(true){
            sum = sum +len;
            if(sum >= t) return len;
            len = len * 2;
            }      
        }
        
      public static long strangeCounter(long t) {
        return 2 * cycleLength(t) - t - 2;
        }  
    
  • + 0 comments

    def strangeCounter(t): sm = 0 for i in range(10**12): sm += 2**i if 3*sm >= t: return 3*sm-t+1

  • + 0 comments

    This is the full steps of calculation:

    public static long Run(long time)
    {
    // a[k] = a[k-1] * q
    // a[n] = a[m] * q^(n-m)
    // S = a[1] * (1 - q^n) / (1 - q)
    
    var S = new Dictionary<long, long>();
    var a = new Dictionary<long, long>();
    
    a[1] = 3;
    
    var q = 2;
    
    var n = (long)Math.Ceiling(Math.Log(1 - time * (1 - q) / (double)a[1]) / Math.Log(q));
    
    S[n - 1] = (long)(a[1] * (1 - Math.Pow(2, n - 1)) / (1 - q));
    a[n] = a[1] * (long)Math.Pow(q, n - 1);
    
    var remain = time - S[n - 1];
    var val = a[n] - remain + 1;
    
    return val;
    }