Sort by

recency

|

20 Discussions

|

  • + 5 comments

    Hi, don't know a safe place?

  • + 0 comments

    Python3

    TL;DR

    def powersGame(n):
        return 'Second' if n%8 == 0 else 'First'
    

    What is behind this solution

    Frankly speaking, after few attemtps to find solution, I gave up. But copy&pasting the answer doesn't tune your mind to solve similar problems. Thus I've invested my time into below explanation. I hope, it can help you as well.

    Modulo

    This task is about modulo mathematic, particualry:

    • ( a + b) % c = ( ( a % c ) + ( b % c ) ) % c
    • ( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
    • ( a – b) % c = ( ( a % c ) – ( b % c ) ) % c
    • HOWEVER: ( a / b ) % c NOT EQUAL ( ( a % c ) / ( b % c ) ) % c

    In our case we transfer ( sign_1*2^1 + sign_2*2^2 + .. sign_n*2^n) % 17 into ( (sign_1*2^1)%17 + (sign_2*2^2)%17 + .. +(sign_n*2^n)%17) % 17

    >>> (1+2-3-4)%17
    13
    >>> (1%17+2%17-3%17-4%17)%17
    13
    >>> (1%17+(2%17)-(3%17)-(4%17))%17
    13
    >>> (1%17+(2%17)+(-3%17)+(-4%17))%17
    13
    
    How it works in our case

    Let have a look what will be modulo 17 for 2 in various power:

    max_power = 20
    print("power i:    ", end="")
    for i in range(1,max_power):
        print(f"{i:^5}", end="|")
    print("\n+(2**i)%17: ", end="")
    for i in range(1,max_power):
        print(f"{((2**i)%17):>4}", end=" |")
    print("\n(-2**i)%17: ", end="")
    for i in range(1,max_power):
        print(f"{(-2**i)%17:>4}", end=" |")
    print("\n")
    
    power i:      1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  | ...
    +(2**i)%17:    2 |   4 |   8 |  16 |  15 |  13 |   9 |   1 | ...
    (-2**i)%17:   15 |  13 |   9 |   1 |   2 |   4 |   8 |  16 | ...
    

    Note how group of [2,4,8,16,15,13,9,1] is repeating over and over in intervals of power 1-8, 9-16, 17-24, .. for . The same logic is for

    What does it mean? For those cases when n is divisable by 8, the Second always have one ore more options to counter-strike, i.e. to compensate any move done by First.

    This is so complex task and this solution is so clever.

    Happy pythonong!

  • + 0 comments

    Here is Powers Game problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-powers-game-problem-solution.html

  • + 0 comments

    I'm getting an error... https://drifthuntersgame.com/

  • + 0 comments

    Hi

    All posts here includes code samples but no logic behind the solution. I am gonna try to explain where "8" comes from.

    Crtitical quotes from question:

    P1 always moving first (Does that mean P1 is lucky? just think a little)

    Assume both players always move optimally. (that means result of the game can be determined before game starts)

    If the total number of moves is even, half of the moves were made by P1 and the other half by P2. If odd, P1 had one more move (the last move).

    If the last move was made by P1, there is no chance P2 to win. Because P1 knows the optimal move, and there are two option (+ or - some power of 2). If one of the option may cause division by 17, other option exactly cause non-zero remainder.

    OK. But how can P2 beat P1 even if the moves are even :)

    We understand that, P2 should counter-attack for each moves of P1 in accordance with a strategy. Lets stop here, and jump somewhere else.

    Since the series does not include 2^0, sum of series must be even. "division by 17" exacly equals with "division by 34" then. let's see 2s.

    34 = 32 + 2 = 2^5 + 2^1

    If sum of series results as m(32 + 2) then P2 will be the goddamn winner.

    Now, the strategy for the P2 is following:

    If P1 picks +2, P2 picks +32

    ...

    If P1 picks +16, P2 picks +256

    If P1 picks +32, P2 picks +2

    ...

    If P1 picks +256, P2 pics +16

    whichever P1 picks the sign, P2 picks the same sign.

    Since the cycle counstructed by 8 elements, P2 can win if and only if n = 8 and its multiples