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.
importmathimportsysn=int(sys.stdin.readline())numbers=map(int,sys.stdin.readline().split())witnesses=[2,3,5,7,11,13,17,19,23,29]Primes=[]IsPrime=[True]*100001forpinrange(2,100000):ifnot(IsPrime[p]):continuePrimes.append(p)mult=p*pwhilemult<100001:IsPrime[mult]=Falsemult+=pdefGetRidOf2(n):whilen%2==0:n//= 2returnndefIsComposite(n,a,s,d):ifpow(a,d,n)==1:returnFalseforrinrange(s):ifpow(a,(1<<r)*d,n)==n-1:returnFalsereturnTruedefIsPrime(n):s=0d=n-1whiled%2==0:d>>=1s+=1forainwitnesses:ifIsComposite(n,a,s,d):returnFalsereturnTruedefIsNPowerOfPrime(n):ifIsPrime(n):returnTruesqrt=int(math.sqrt(n))ifsqrt*sqrt==nandIsPrime(n):returnTruereturnFalsedefIsNSquare(n):sqrt=int(math.sqrt(n))return(sqrt*sqrt==n)defFactor(n):nbFactors=0sumFactors=0ifn%2==0:nbFactors+=1n=GetRidOf2(n)ifIsNSquare(n):sumFactors=1n=int(math.sqrt(n))forprimeinPrimes:ifn==1:breakifn%prime!=0:continuenbFactors+=1n//= primewhilen%prime==0:n//= prime ifn==1:return(nbFactors,sumFactors)ifIsNPowerOfPrime(n):return(nbFactors+1,sumFactors)return(nbFactors+2,sumFactors)result=[[0,0],[0,0]]forninnumbers:(nb,s)=Factor(n)result[nb%2][s]+=1finalResult=result[0][0]+result[0][1]finalResult=max(finalResult,result[0][0]+result[1][0])finalResult=max(finalResult,result[1][1]+result[1][0])finalResult=max(finalResult,result[1][1]+result[0][1])print(finalResult)
For any fixed N, there exists a set of witnesses such that Miller-Rabin with those fixed bases will correctly identify all composite numbers less than N. This removes the element of randomness from Miller-Rabin, and the set of fixed bases is actually surprisingly small for N = 2^64.
If N^(1/3) < x < N and x is not divisible by any prime < N^(1/3), then x is the product of either one or two primes larger than N^(2/3). In either case, you don't need to factor it completely. If x is a square, how many distinct prime divisors does it have? And how many divisors total? If x is not a square, how many divisors does it have? And how many divisors total?
Python3 solution
Some hints if you are stuck:
For any fixed N, there exists a set of witnesses such that Miller-Rabin with those fixed bases will correctly identify all composite numbers less than N. This removes the element of randomness from Miller-Rabin, and the set of fixed bases is actually surprisingly small for N = 2^64.
If N^(1/3) < x < N and x is not divisible by any prime < N^(1/3), then x is the product of either one or two primes larger than N^(2/3). In either case, you don't need to factor it completely. If x is a square, how many distinct prime divisors does it have? And how many divisors total? If x is not a square, how many divisors does it have? And how many divisors total?
Should duplicate prime factors be counted twice, e.g prime factors of 18 are 2, 3 and 3. Shall we count 3 twice or just once?