HackerRank "Manasa and Prime game"

Intuitive one to learn about Grundy basic :) 

Now every pile becomes a game, so we need to use Sprague-Grundy Theory. Calculation is quite intuitive - and if you print them out, you will find these Grundy numbers loops by 9. 

def firstMissing(s):
    ret = 0
    while 1:
        if ret not in s: break
        else: ret += 1
    return ret

primes = [2,3,5,7,11,13]
def grundy(v):    
    if v <= 2: return 0    

    tmp = set([])
    for p in primes:
        if p >= v: break
        else: tmp.add(grundy(v - p))
    ret = firstMissing(tmp)
    grundySet[v] = ret
    return ret

####################
def simpleGrundy(v):
    return [0,0,1,1,2,2,3,3,4][v%9]

####################
T = int(input())
for _ in range(T):
    N = int(input())
    A = map(int, input().split())
    sg = map(simpleGrundy, A)
    ret = 0
    for g in sg: ret ^= g
    print(['Sandy','Manasa'][ret!=0])

你可能感兴趣的:(HackerRank "Manasa and Prime game")