⬆️ ⬇️

Parsing tasks 1 round of the school programmers HeadHunter

Passed the first round of selection of participants in the school programmers HeadHunter , the announcement on Habré

Where after filling in the questionnaire it was proposed to solve 5 problems



The questionnaire was asked to fill in the following fields:





Task 1



Condition


For how many n and k, subject to 1 <= n <132, 1 <= k <n, the number of combinations C (n, k)> 1,000,000?

Full print job




Think


What is there to think about? 132 is very small, brute force fits, we implement it on Python.

We take the implementation of the number of combinations from the SciPy package - in many ways this is the open source of Matlab

Decide


from scipy.misc import * #     total = 0 for n in range(1,133): for k in range(1,n): if comb(n,k)>1000000: total=total+1 print "Answer: ",total 


Run, measuring the execution time:

 #:~/hh$ time python 1.py Answer: 7579 real 0m0.530s user 0m0.504s sys 0m0.020s #:~/hh$ 


Found a bug?
constraints from the condition are not transferred to the program





Task 2:



Condition


In the bag is 1 red and 1 blue disk. During the game, the player takes a random disc from the bag for the turn, his color is recorded. After each move, the disc taken is returned to the bag and another red disc is added there.

The player pays 1 euro per game and wins if at the end of the game he has got more blue discs than red ones. If the game lasts 4 turns, the probability of winning is 11/120, so the maximum prize that the host can assign for winning in this game is 10 euros, otherwise he will start to suffer losses.

Please note that this is an integer and it includes the initial payment of participation, so the player actually wins 9 euros.

Find the maximum total amount of the prize that does not make the game unprofitable for the leader in the game of 30 moves?

The same task printskrinom




We understand the condition


During the game, the number of red balls increases, it means that the probability of getting a single blue falls. To win, you need to get more than half of the blue. For the first time, guess the blue probability 1/2 for the second time 1/3, for the n-th time, the probability to reach blue is 1 / (n + 1).

Parse an example from the condition


If the duration of the game is 4 moves, the probabilities are 1/2, 1/3, 1/4, 1/5. To win, you can make a mistake only 1 time. And in which of the attempts we made a mistake it does not matter. Let's calculate what the probability of success is: 1/60 + 1/40 + 1/30 + 1/24 + 1/120 = 15/120

Think


The factorial 30 is a small number, again using complete brute force. We take the generator of the number of combinations from the itertools package built into the python .

Decide


 import itertools game=30 comb=[] resb=1 for t in range(2,game+2): comb.append(t) resb=resb*t #    print comb resa=0 for q in range(game/2+1,game+1): #      print q,resa,resb #   for t in itertools.combinations(comb,q): #    ca=1 cb=1 for x in t: cb=cb*x tdiv=resb/cb resa=resa+tdiv*ca print game/2+1 print resa,resb #        


')

 #:~/hh/article$ time python 2.p [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] 16 0 8222838654177922817725562880000000 17 6014558687904548121004575 8222838654177922817725562880000000 18 6363613319405364461146200 8222838654177922817725562880000000 19 6381128687025974988156255 8222838654177922817725562880000000 20 6381886877953385972148180 8222838654177922817725562880000000 21 6381915085555093961253855 8222838654177922817725562880000000 22 6381915982709887260743580 8222838654177922817725562880000000 23 6381916006925362413306495 8222838654177922817725562880000000 24 6381916007474554489499970 8222838654177922817725562880000000 25 6381916007484879987901695 8222838654177922817725562880000000 26 6381916007485037971122090 8222838654177922817725562880000000 27 6381916007485039887292479 8222838654177922817725562880000000 28 6381916007485039905011334 8222838654177922817725562880000000 29 6381916007485039905128639 8222838654177922817725562880000000 30 6381916007485039905129134 8222838654177922817725562880000000 16 6381916007485039905129135 8222838654177922817725562880000000 real 23m2.424s user 23m0.238s sys 0m0.168s #:~/hh/article$ 


While counting 23 minutes, we solve other problems.

the fraction needs to be transferred to the maximum winnings - we divide the denominator by the numerator, we get the winnings of “self-sufficiency”.

 #:~/hh/article$ bc -l bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 8222838654177922817725562880000000/6381916007485039905129135 1288459240.85082818135254719839 ^C (interrupt) use quit to exit. #:~/hh/article$ 


We need a maximal prediction, in response we write the whole part, i.e. 1288459240

Found a bug?
Here we considered the wrong probabilities. When calculating, one must not only choose blue from many red, but also choose one red from many. Therefore, we change the program:

 from datetime import datetime import itertools game=30 comb=[] resb=1 for t in range(2,game+2): comb.append(t) resb=resb*t print comb resa=0 st=datetime.now() for q in range(0,game/2): #         ct=datetime.now() print q, ct-st st=ct for t in itertools.combinations(comb,q): #    q      ta=1 for x in t: ta=ta*(x-1) #      (  1)    -        ,     ,  itertools.combinations      x resa=resa+ta print resa,resb 


the answer is 15 minutes after launch





Task 3:



If the number of all the numbers not less than the left of them, it is called increasing. Example - 133456. Accordingly, if the numbers are not less than those on the right, it is called decreasing. Example: 66420.

Numbers that are neither increasing nor decreasing will be called jumping.

How many pyrgus numbers are there are less than 10 ^ 75?

Printscreen job




We think:


Brute force is long, and induction is applied perfectly (dynamic programming)

Indeed, any increasing number on the left can be assigned from 1 to the first digit of the number, inclusive, and to any decreasing number on the right, it can be assigned from zero to the last digit of the number, inclusive.

We solve:




 #    a={} #   b={} #   for t in range(0,11): a[1,t]=1 #   -  ,   - () .  -    b[1,t]=1 def snext(tail): global a,b tvar=0 btvar=0 for d in range(0,11): #      a[tail,d]=0 b[tail,d]=0 for d in range(1,10): #    var=0 bvar=0 t=a[tail-1,d] tb=b[tail-1,d] for q in range(1,d+1): var=var+t a[tail,q]=a[tail,q]+t#var tvar=tvar+var for d in range(0,10): #    bvar=0 tb=b[tail-1,d] for q in range(d,10): bvar=bvar+tb b[tail,q]=b[tail,q]+tb btvar=btvar+bvar btvar=btvar-1 print tail,tvar,btvar return [tvar,btvar] start=0 for q in range(2,76): [pa,pb]=snext(q) start=start-pa-pb-9 # ..     , , ....,    -   9 start=start-10 #     print "10^75", start 


Long output of the program
 #:~/hh/article$ time python 3.py 2 45 54 3 165 219 4 495 714 5 1287 2001 6 3003 5004 7 6435 11439 8 12870 24309 9 24310 48619 10 43758 92377 11 75582 167959 12 125970 293929 13 203490 497419 14 319770 817189 15 490314 1307503 16 735471 2042974 17 1081575 3124549 18 1562275 4686824 19 2220075 6906899 20 3108105 10015004 21 4292145 14307149 22 5852925 20160074 23 7888725 28048799 24 10518300 38567099 25 13884156 52451255 26 18156204 70607459 27 23535820 94143279 28 30260340 124403619 29 38608020 163011639 30 48903492 211915131 31 61523748 273438879 32 76904685 350343564 33 95548245 445891809 34 118030185 563921994 35 145008513 708930507 36 177232627 886163134 37 215553195 1101716329 38 260932815 1362649144 39 314457495 1677106639 40 377348994 2054455633 41 450978066 2505433699 42 536878650 3042312349 43 636763050 3679075399 44 752538150 4431613549 45 886322710 5317936259 46 1040465790 6358402049 47 1217566350 7575968399 48 1420494075 8996462474 49 1652411475 10648873949 50 1916797311 12565671260 51 2217471399 14783142659 52 2558620845 17341763504 53 2944827765 20286591269 54 3381098545 23667689814 55 3872894697 27540584511 56 4426165368 31966749879 57 5047381560 37014131439 58 5743572120 42757703559 59 6522361560 49280065119 60 7392009768 56672074887 61 8361453672 65033528559 62 9440350920 74473879479 63 10639125640 85113005119 64 11969016345 97082021464 65 13442126049 110524147513 66 15071474661 125595622174 67 16871053725 142466675899 68 18855883575 161322559474 69 21042072975 182364632449 70 23446881315 205811513764 71 26088783435 231900297199 72 28987537150 260887834349 73 32164253550 293052087899 74 35641470150 328693558049 75 39443226966 368136785015 10^75 -3497299458233 real 0m0.070s user 0m0.044s sys 0m0.020s #:~/hh/article$ 




Found a bug?
Solve analytically:

Find the number of increasing numbers, the number of decreasing and subtract the number of mirror numbers.



The number of increasing numbers is sought from considerations:

if several numbers are given from the set {0,1,2,3,4,5,6,7,8,9} they can be arranged in ascending order. We get the urn sampling scheme with the return and without taking into account the order



With decreasing, it will not be possible to arrange automatically - zeros will always go to the end of the number, but they can be at the beginning. So we will choose from the numbers {1,2,3,4,5,6,7,8,9} and place the zeros additionally on the empty places. Empty places can only be before or after all the numbers, otherwise a jumping number is obtained. N zeros we can arrange the N + 1 way

 import sys from scipy.misc import * game = int(sys.argv[1])# 75 def czero(game,fill): sort=long(round(comb(fill+8,fill),0)) #  fill   1  9 return sort*(game-fill+1) #      def nzero(game): return long(round(comb(game+9,game),0)) #    0  9 zans=0 nans=nzero(game) for fill in range(1,game+1): zans=zans+czero(game,fill) zans=zans+1 print nans, zans ans=nans+zans-(game-1)*9-10 print 10**game-ans 






Task 4:



Find the number of such a member of the Fibonacci sequence, that the number of digits in it is 1369

Full print job




We think:


We count Fibonacci numbers, translate into a string representation, and look at the length.

We solve:


 mlen=1369 a1=1 a2=1 ct=2 while len(str(a1+a2))<mlen: a3=a1+a2 a1=a2 a2=a3 ct=ct+1 ct=ct+1 print a3,len(str(a3)),ct 


 #:~/hh$ time python 4.py 1368 6548 real 0m0.183s user 0m0.164s sys 0m0.012s #:~/hh$ 


Found a bug?
It seems no :)



Task 5:



Find the last 10 digits in the final sum of the series, 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ... + 1145 ^ 1145

Full print job




We think:


The number 1145 to 1145 degrees is really big. The task is asked only the last 10 digits, then we take advantage of the modular arithmetic - we will immediately assume everything in the module.

We solve:


To calculate the degree modulo we use the package http://userpages.umbc.edu/~campbel/Computers/Python/numbthy.html

Download http://userpages.umbc.edu/~rcampbel/Computers/Python/lib/numbthy.py and put it in the program folder:

 import numbthy as np t=0 for i in range(1,1146): t=t+np.powmod(i,i,1000000000000000000000) print t % 10000000000 




 #:~/hh$ time python 5.py 7110603381 real 0m0.029s user 0m0.020s sys 0m0.004s #:~/hh$ 




Found a bug?
It seems no :)





They say that 2 tasks out of 5 have been solved correctly. I know one mistake, and where are the others?

Source: https://habr.com/ru/post/155015/



All Articles