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.
frommathimportceildefgcd(a,b):ifa>b:returngcd(b,a)whilea!=0:a,b=b%a,areturnbdefproduct(lst):prod=1forninlst:prod=(prod*n)%(10**9+7)returnprod# Have a global variable denote the bounds# Have the function denote how much to divide the bounds bybounds=Nonecache=Nonedefcoprime_count(divisor):ifdivisor>bounds[-1]// 2:return1ifdivisorincache:returncache[divisor]# bounds are assumed to be sortedscaled_bounds=[n// divisor for n in bounds]total=product(scaled_bounds)cap=scaled_bounds[0]# total = sum d from 1 to lowest bound coprime_count(d) (inclusive)rest=sum(coprime_count(divisor*d)fordinrange(2,cap+1))cache[divisor]=(total-rest)%(10**9+7)returncache[divisor]t=int(input())for_inrange(t):k=int(input())bounds=list(map(int,input().split()))bounds.sort()cache=dict()total=0foriinrange(1,bounds[0]+1):total=(total+i*coprime_count(i))%(10**9+7)print(total)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hyperrectangle GCD
You are viewing a single comment's thread. Return to all comments →
Python3 :-)