2015 facebook hacker cup

Upload: sad-dad

Post on 01-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 2015 facebook hacker cup

    1/5

    2015 Facebook Hacker Cuphttps://www.facebook.com/hackercup/

    Round 1

    A. Homework

    Your first-grade math teacher, Mr. Book, has just introduced you to anamazing new concept primes! According to your notes, a prime is apositie integer greater than that is diisi"#e "y on#y and itse#f.

    $rimes seem fun, "ut without giing you and your %-year-o#dco##eagues time to consider their imp#ications, he&s prompt#y gone on todefine another term' primacity. (e e)p#ains that the primacity of aninteger is the num"er of distinct primes which diide it. *or e)amp#e,the primacity of + is + as it&s diisi"#e "y primes + and , theprimacity of //0 is as it&s diisi"#e "y primes +, /, and , and theprimacity of 1 is as the on#y prime it&s diisi"#e "y is 1.

    *o##owing his #esson, Mr. Book has gien you homework with somerather mean 2uestions of the fo##owing form' 3ien integers A, B,and K, how many integers in the inc#usie range 4A, B5 hae aprimacity of e)act#y K6

    Mr. Book pro"a"#y e)pects his #itt#e homework assignment to take youand your c#assmates the rest of the year to comp#ete, giing him time tos#ack off and nap during the remaining math c#asses. (oweer, youwant to #earn more things from him instead! 7an you use the ski##syou&e #earned in your first-grade computer science c#asses to finishMr. Book&s homework "efore tomorrow&s math c#ass6

    Input

    8nput "egins with an integer T, the num"er of homework 2uestions. *oreach 2uestion, there is one #ine containing space-separatedintegers'A, B, and K.

    Output

    *or the ith 2uestion, print a #ine containing 97ase :i' 9 fo##owed "y thenum"er of integers in the inc#usie range 4A, B5 with a primacity of K.

    Constraints

    ; T; 00+ ; A; B; 01 ; K; 0

  • 8/9/2019 2015 facebook hacker cup

    2/5

    2015 Facebook Hacker Cuphttps://www.facebook.com/hackercup/

    Round 1

    B. Autocomplete

    >ince you crae state-of-the-art techno#ogy, you&e just purchased a phonewith a great new feature' autocomp#ete! Your phone&s ersion ofautocomp#ete has some pros and cons. ?n the one hand, it&s ery cautious. 8ton#y autocomp#etes a word when it knows e)act#y what you&re trying towrite. ?n the other hand, you hae to teach it eery word you want to use.

    You hae Ndistinct words that you&d #ike to send in a te)t message in order.Before sending each word, you add it to your phone&s dictionary. @hen, youwrite the sma##est non-empty prefi) of the word necessary for your phone toautocomp#ete the word. @his prefi) must either "e the who#e word, or aprefi) which is not a prefi) of any other word yet in the dictionary.

    hat&s the minimum num"er of #etters you must type to send a## Nwords6

    Input

    8nput "egins with an integer T, the num"er of test cases. *or each test case,there is first a #ine containing the integer N. @hen, N#ines fo##ow, eachcontaining a word to send in the order you wish to send them.

    Output

    *or the ith test case, print a #ine containing 97ase :i' 9 fo##owed "y theminimum num"er of characters you need to type in your te)t message.

    Constraints

    ; T; 00 ; N; 00,000

    @he Nwords wi## hae a tota# #ength of no more than ,000,000 characters.@he words are made up of on#y #ower-case a#pha"etic characters.@he words are pairwise distinct.

    NOTE:@he input fi#e is a"out 0-+0MB.

    Explanation of Sample

    8n the first test case, you wi## write 9h9, 9he9, 9#9, 9hi#9, 9hi##9, for a tota# of + = C characters.

    Sample Input

    5

    5

    hi

    hello

    lol

    hills

    hill

    5

    a

    aa

    aaa

    aaaa

    aaaaa

    5

    aaaaa

    aaaa

    aaa

    aa

    a

    6

    to

    be

    or

    not

    twobee

    3

    having

    fun

    yet

    Sample Output

    Case #1: 11

    Case #2: 15

    Case #3: 11

    Case #4: 9Case #5: 3

  • 8/9/2019 2015 facebook hacker cup

    3/5

    2015 Facebook Hacker Cuphttps://www.facebook.com/hackercup/

    Round 1

    C. Winning at Sports

    8n the game of Sports, the o"ject is hae more points than the otherteam after a certain amount of time has e#apsed. >cores are denoted "ytwo hyphen-separated integers. *or e)amp#e, scores may inc#ude -+,=-, or 0-0. @he first num"er is how many points you&e scored, andthe second is the num"er of points scored "y the opposing team. You&reery good at Sports, and conse2uent#y you a#ways win. (oweer, youdon&t a#ways achiee ictory the same way eery time.

    @he two most e)treme kinds of ictory are ca##ed stress-freeand stressful. 8n a stress-freeictory, you score the first point andfrom then on you a#ways hae more points than your opponent. 8na stressfulictory, you neer hae more points than your opponent

    unti# after their score is e2ua# to their fina# score.

    3ien the fina# score of a game of Sports, how many ways cou#d youarrange the order in which the points are scored such that you secure astress-freeor stressfulwin6

    Input

    8nput "egins with an integer T, the num"er of games you# p#ay. *oreach game, there is one #ine containing the fina# score of the game inthe format descri"ed a"oe.

    Output

    *or the ith game, print a #ine containing 97ase :i' 9 fo##owed "y twospace-separated integers, the num"er of ways you can achiee a stress-freeor stressfulwin, respectie#y. >ince these num"ers may "e ery#arge, output them modu#o ,000,000,001.

    Constraints

    ; T; 00

    >ince you a#ways win, the first num"er in any fina# score wi## a#ways"e #arger than the second. Both scores wi## "e non-negatie integers note)ceeding +000.

    Explanation of Sample

    8n the third test case, you can get a stress-free win "y scoring points ,+, and =, or points , +, and . You can get a stressfu# win "y scoringpoints +, =, and /, or points , =, and /.

    Sample Input

    5

    2-1

    3-1

    3-2

    10-5

    1000-500

    Sample Output

    Case #1: 1 1

    Case #2: 2 1

    Case #3: 2 2

    Case #4: 1001 42

    Case #5: 70047606 59113740

  • 8/9/2019 2015 facebook hacker cup

    4/5

    2015 Facebook Hacker Cuphttps://www.facebook.com/hackercup/

    Round 1

    D. Corporate Gifting

    @he fine peop#e of 7orpro 7orp. are a festie "unch. Dery ho#iday season, eery"ody"uys a gift for their manager. A cynic might say that the emp#oyees are just trying to"ri"e their way to a "etter performance reiew, "ut if you asked them yourse#f, they&dsay they just wanted to spread cheer.

    @he fine peop#e of 7orpro 7orp. are a fruga# "unch. hen they "uy gifts, theycooperate to co##ectie#y "uy the #east e)pensie gifts that they can. A cynic might saythat the emp#oyees are cheap, "ut if you asked them yourse#f, they&d say it&s thethought that counts.

    @here are Nemp#oyees working at 7orpro 7orp., and each of them has a manager,e)cept for the 7D? who has no manager the 7D? a#so "uys a gift eery year, "ut she

    donates it to charity. @he emp#oyees each hae a uni2ue emp#oyee 8E which is aninteger from to N. As you might e)pect, the 7D? has the 8E .

    8f there e)ists a set of two or more emp#oyees Fp1, ..., pkG such that, for a## iH k, piisthe manager of pi+1, then we say that p1is 9responsi"#e for9 pk. @here are neer twoemp#oyees who are responsi"#e for each other. @hat wou#d "e a si##y hierarchy indeed.

    @here are Nkinds of gifts aai#a"#e for purchase, and the ith kind of giftcosts ido##ars. @hat is, the prices of the different kinds of gifts are FI, I+, I, ... ING.@here are Ncopies of each gift aai#a"#e for purchase.

    @he on#y thing that stops a## emp#oyees from purchasing gifts that cost I is theawkwardness of "uying a gift for their manager that&s the same as the one theirmanager is giing away. Jo emp#oyee wou#d eer do such a thing!

    *or e)amp#e, in a company with just + emp#oyees, at #east I must "e spent in tota#. 8femp#oyee : the 7D? "uys a I gift to donate to charity, then emp#oyee :+ cannot"uy a I gift for emp#oyee : their manager, "ut they can "uy a I+ gift instead. Jotethat it wou#d "e e2ua##y optima# for the 7D? to "uy a I+ gift, whi#e receiing a I giftfrom her su"ordinate.

    hat&s the minimum possi"#e tota# e)penditure across the who#e company during thegift e)change6

    Input

    8nput "egins with an integer T, the num"er of corporate hierarchies to consider. Dachhierarchy is made up of two #ines. @he first #ine contains the integer N. @he second#ine contains Nspace-separated integers. @he ith integer is the emp#oyee 8E of themanager of emp#oyee i, with the e)ception that the first integer is a#ways 0, denotingthat the 7D? has no manager.

    Sample Input

    5

    3

    0 1 1

    0 1 1 2 2 3 3 3

    5

    0 1 2 3 4

    9

    0 1 2 3 4 5 5 5

    0 1 1 1 1 2 2 2

    Sample OutputCase #1: 4

    Case #2: 10

    Case #3: 7

    Case #4: 12

    Case #5: 11

  • 8/9/2019 2015 facebook hacker cup

    5/5

    2015 Facebook Hacker Cuphttps://www.facebook.com/hackercup/

    Round 1

    Output

    *or the ith hierarchy, print a #ine containing 97ase :i' 9 fo##owed "y the sma##estamount of money the entire company wou#d need to spend.

    Constraints

    ; T; 00 ; N; +00,000

    NOTE:@he input fi#e is a"out 0-+0MB.

    Explanation of Sample

    8n the first test case, the 7D? wi## spend I+, and the other emp#oyees wi## spend I.

    8n the second test case, emp#oyees :+ and : wi## spend I+, and the other emp#oyeeswi## spend I.