首页 > 其他 > 详细

HackerRank "Manasa and Prime game"

时间:2016-02-11 10:03:06      阅读:250      评论:0      收藏:0      [点我收藏+]

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"

原文:http://www.cnblogs.com/tonix/p/5186301.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!