We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3fromfunctoolsimportlru_cacheimportmathimportosimportrandomimportreimportsys## Complete the 'strangeCounter' function below.## The function is expected to return a LONG_INTEGER.# The function accepts LONG_INTEGER t as parameter.#classCycle:def__init__(self,start_time,initial_value,length):self.start_time=start_timeself.initial_value=initial_valueself.length=lengthself.end_time=start_time+length-1def__repr__(self):returnf"Cycle(start_time={self.start_time}, initial_value={self.initial_value}, length={self.length}, end_time={self.end_time})"defgenerate_cycles(limit):cycles=[]start_time=1initial_value=3whilestart_time<=limit:cycle_length=initial_valuecycle=Cycle(start_time,initial_value,cycle_length)cycles.append(cycle)start_time+=cycle_lengthinitial_value*=2returncycles@lru_cache(None)deffind_cycle(t,cycles):forcycleincycles:ifcycle.start_time<=t<=cycle.end_time:returncyclereturnNonedefstrangeCounter(t,cycles):cycle=find_cycle(t,tuple(cycles))ifcycle:returncycle.initial_value-(t-cycle.start_time)return-1cycles=generate_cycles(1000000000000)if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')t=int(input().strip())result=strangeCounter(t,cycles)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strange Counter
You are viewing a single comment's thread. Return to all comments →
My solution using Python.