V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bestkayle
V2EX  ›  算法

google foobar 的一个题目

  •  
  •   bestkayle · 2017-08-30 17:17:16 +08:00 · 2746 次点击
    这是一个创建于 2421 天前的主题,其中的信息可能已经有所发展或是发生改变。

    You're so close to destroying the LAMBCHOP doomsday device you can taste it! But in order to do so, you need to deploy special self-replicating bombs designed for you by the brightest scientists on Bunny Planet. There are two types: Mach bombs (M) and Facula bombs (F). The bombs, once released into the LAMBCHOP's inner workings, will automatically deploy to all the strategic points you've identified and destroy them at the same time.

    But there's a few catches. First, the bombs self-replicate via one of two distinct processes: Every Mach bomb retrieves a sync unit from a Facula bomb; for every Mach bomb, a Facula bomb is created; Every Facula bomb spontaneously creates a Mach bomb.

    For example, if you had 3 Mach bombs and 2 Facula bombs, they could either produce 3 Mach bombs and 5 Facula bombs, or 5 Mach bombs and 2 Facula bombs. The replication process can be changed each cycle.

    Second, you need to ensure that you have exactly the right number of Mach and Facula bombs to destroy the LAMBCHOP device. Too few, and the device might survive. Too many, and you might overload the mass capacitors and create a singularity at the heart of the space station - not good!

    And finally, you were only able to smuggle one of each type of bomb - one Mach, one Facula - aboard the ship when you arrived, so that's all you have to start with. (Thus it may be impossible to deploy the bombs to destroy the LAMBCHOP, but that's not going to stop you from trying!)

    You need to know how many replication cycles (generations) it will take to generate the correct amount of bombs to destroy the LAMBCHOP. Write a function answer(M, F) where M and F are the number of Mach and Facula bombs needed. Return the fewest number of generations (as a string) that need to pass before you'll have the exact number of bombs necessary to destroy the LAMBCHOP, or the string "impossible" if this can't be done! M and F will be string representations of positive integers no larger than 10^50. For example, if M = "2" and F = "1", one generation would need to pass, so the answer would be "1". However, if M = "2" and F = "4", it would not be possible.

    Languages

    To provide a Python solution, edit solution.py To provide a Java solution, edit solution.java

    Test cases

    Inputs: (string) M = "2" (string) F = "1" Output: (string) "1"

    Inputs: (string) M = "4" (string) F = "7" Output: (string) "4"

    我的理解大概意思是 m 和 f 初始 1,1。可以用 m = m + f 或者 f = f +m 中的一步,计算生成到目标 m=xxx,f=xxx 需要几步。 遇到了递归深度的问题,经过测试题目把递归深度限定在 5000,请教各位有什么好的写法。下面是我的错误的写法。

    count = 0
    def answer(M, F):
        M = long(M)
        F = long(F)
        global count
        if M > F:
            prem = M -F
            count += 1
            return answer(prem,F)
        if M < F:
            pref = F - M
            count += 1
            return answer(M,pref)
        if M == F:
            if M == 1 and F == 1:
                return str(count)
            else:
                return 'impossible'
    
    4 条回复    2017-08-30 21:12:53 +08:00
    wingkou
        1
    wingkou  
       2017-08-30 17:50:07 +08:00 via Android
    TLE
    正解 GCD
    yunkchen
        2
    yunkchen  
       2017-08-30 17:50:59 +08:00
    #没有用递归
    def foolbar(M, F):
    count = 0
    while M != F:
    M, F = sorted([M, F, abs(M-F)])[:2]
    count += 1
    if M <= 0 or F <= 0:
    return "impossible"
    if M == 1:
    return count
    else:
    return "impossible"
    wingkou
        3
    wingkou  
       2017-08-30 17:51:10 +08:00 via Android
    就是加减太慢了,用乘除
    ryd994
        4
    ryd994  
       2017-08-30 21:12:53 +08:00 via Android
    这题很简单,倒推就行
    不要在这里问 foobar 的题目,一不道德,二可以举报
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2781 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:36 · PVG 23:36 · LAX 08:36 · JFK 11:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.