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.
This problem may be one of my favorites, because it permits so much completely different approaches. (I'm none of these ICPC wizards who hop on a problem and code and next problem...).
Possible approaches:
Direct solution (formula) -> dont know if possible, looks cumbersome
Brute force on 2 numbers, estimate bounds -> done, see notes
Number theory: solve modulo some numbers and apply CRT -> done, see notes
Approximation (eg. Newton) in the real numbers -> difficult, see notes
-> Editorial: Brute force on 1 number + formula for other 2 numbers
Beforehand one can observe:
the difficulty lies in finding a,b,c, the series are trivial
the order of a,b,c don't matter
there is only one positive solution (up to order)
Now some notes to the approaches:
(2) Brute force on c and b: the growth rate of f2..f3..f4 is dominated by the biggest term = c, that is one can get upper and lower bounds for c from f4 and from f4/f2 (and similar bounds for b). However that leaves to many combinations to probe, so I did precompute valid triples (g2,g3,g4) where gi where possible sums of 2 numbers (^2,^3,^4) mod something, and used that to filter c
(3) Number theory: Maybe the cleanest and fastest approach: precompute possible solutions - dictionary of (a,b,c) with key (f2,f3,f4) - modulo some primes, I used 23,29,31. From f2,f3,f4 you can lookup candidates (a,b,c mod primes =(CRT)=> original a,b,c) amongst them is the solution.
(3) Approximation: It turns out that there is a strong dependency from a good starting point. I tried to tackle this by using a grid of starting points, but was much to slow, and even then failed to converge for some test cases.
## - - - - - - -# brute force with estimation of rangevalidx2=Nonedefsolve_bf(f2,f3,f4):'''Bruteforcewithestimationofbiggestnumberbasedonxi^4/xi^2:f<=c^2<=gamma*fwithf=f4/f2andgamma=(sqrt(3)+1)/2g<=b^2<=beta*gwithg=(f4-c^4)/(f2-c^2)andbeta=(sqrt(2)+1)/2'''globalvalidx2ifnotvalidx2:validx2={}forkin[81,32,125,49]:validx2[k]=set(tuple((a**i+b**i)%kforiin(2,3,4))forainrange(k)forbinrange(k))found=[]f=f4/f2gamma_f=(math.sqrt(3)+1)/2*fc2max=min(14_997**2,f2-5,math.sqrt(f4-17),gamma_f+EPS)count=0forcinrange(int(math.sqrt(f-EPS)),int(math.sqrt(c2max))+1):c2,c3,c4=f2-c*c,f3-c**3,f4-c**4ifany((c2%k,c3%k,c4%k)notinvalidx2[k]forkin[81,32,125,49]):continueg=(f4-c**4)/(f2-c*c)beta_g=(math.sqrt(2)+1)/2*gb2max=min((c-1)**2,f2-c*c-1,beta_g+EPS)forbinrange(int(math.sqrt(g-EPS)),int(math.sqrt(b2max))+1):count+=1# check valid aa2=f2-c*c-b*bifa2<b*banda2*a2==f4-c**4-b**4:a=math.isqrt(a2)ifa*a==a2anda**3+b**3+c**3==f3:found.append((a,b,c))# print('count:',count)found.sort()returnfound[0]## - - - - - - -# number theoretical approach - solve modulo and use CRTfromcollectionsimportdefaultdictfromitertoolsimportcombinations_with_replacementascombwr,permutations,productvalidx3=Nonedefsolve_nt(f2,f3,f4):'''Numbertheory:lookuptablefora,b,c(modsomeprimes)anduseCRT.'''globalvalidx3mods=[23,29,31]ifvalidx3isNone:validx3={}forpinmods:pmap=defaultdict(set)fora,b,cincombwr(range(p),3):pmap[tuple((a**i+b**i+c**i)%pforiin[2,3,4])].add((a,b,c))validx3[p]=pmapall_remainders=[pmap[(f2%p,f3%p,f4%p)]forp,pmapinvalidx3.items()]forcomboinproduct(*all_remainders):c23=combo[0]forc29inpermutations(combo[1]):forc31inpermutations(combo[2]):a=[multicrt(((23,c23[i]),(29,c29[i]),(31,c31[i])))foriinrange(3)]ifall(sum(aa**kforaaina)==fkfork,fkin((2,f2),(3,f3),(4,f4))):print('...',sorted(a))returnsorted(a,reverse=True)returnNonedefmulticrt(mrs):#multiremainders:reducebyapplyingCRTsucessivelymrs=list(mrs)m,r=mrs.pop()whilemrs:m1,r1=mrs.pop()r=crt(m,r,m1,r1)m*=m1returnrdefcrt(m1,r1,m2,r2):g,b1,b2=xgcd(m1,m2)#seemslikethisfactorsarecalledBezoutcoefficientsifg!=1:#b1*m1+b2*m2=1raiseValueError('Modulinotrelativeprime')return(r2*b1*m1+r1*b2*m2)%(m1*m2)defxgcd(a,b):x0,x1,y0,y1=0,1,1,0whilea!=0:q,b,a=b//a,a,b%ay0,y1=y1,y0-q*y1x0,x1=x1,x0-q*x1returnb,x0,y0
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Birthday Triplets
You are viewing a single comment's thread. Return to all comments →
This problem may be one of my favorites, because it permits so much completely different approaches. (I'm none of these ICPC wizards who hop on a problem and code and next problem...).
Possible approaches:
Beforehand one can observe:
Now some notes to the approaches:
(2) Brute force on c and b: the growth rate of f2..f3..f4 is dominated by the biggest term = c, that is one can get upper and lower bounds for c from f4 and from f4/f2 (and similar bounds for b). However that leaves to many combinations to probe, so I did precompute valid triples (g2,g3,g4) where gi where possible sums of 2 numbers (^2,^3,^4) mod something, and used that to filter c
(3) Number theory: Maybe the cleanest and fastest approach: precompute possible solutions - dictionary of (a,b,c) with key (f2,f3,f4) - modulo some primes, I used 23,29,31. From f2,f3,f4 you can lookup candidates (a,b,c mod primes =(CRT)=> original a,b,c) amongst them is the solution.
(3) Approximation: It turns out that there is a strong dependency from a good starting point. I tried to tackle this by using a grid of starting points, but was much to slow, and even then failed to converge for some test cases.