计算到n的最短路径数


21

这段代码的挑战将你计算的方法来达到多少n从开始2使用形式的地图xx+xj(与j一个非负整数),并在最少的步骤数这么做。

(注意,这与OEIS序列A307092有关。)

因此,例如,f(13)=2是因为需要三个映射,并且三个映射的两个不同序列将发送213

xx+x0xx+x2xx+x0orxx+x2xx+x1xx+x0

结果为231213261213

值示例

f(2)   = 1 (via [])
f(3)   = 1 (via [0])
f(4)   = 1 (via [1])
f(5)   = 1 (via [1,0])
f(12)  = 2 (via [0,2] or [2,1])
f(13)  = 2 (via [0,2,0] or [2,1,0], shown above)
f(19)  = 1 (via [4,0])
f(20)  = 2 (via [1,2] or [3,1])
f(226) = 3 (via [2,0,2,1,0,1], [3,2,0,0,0,1], or [2,3,0,0,0,0])
f(372) = 4 (via [3,0,1,0,1,1,0,1,1], [1,1,0,2,0,0,0,1,1], [0,2,0,2,0,0,0,0,1], or [2,1,0,2,0,0,0,0,1])

挑战

面临的挑战是产生一个程序来的整数n2作为输入,并且输出从不同的路径的数目2n经由形式的地图的最小数量的xx+xj

这是,因此最少的字节获胜。


1
我认为应该明确指出该^符号表示幂。也可以是XOR(例如C ^用于按位XOR)。
拉米利斯

1
@Ramillies也许应该将其更改为MathJax。即而不是。x=x+xjx -> x + x^j
凯文·克鲁伊森

@KevinCruijssen:好点,那肯定会有所帮助。
拉米利斯

我已将它作为A309997添加到OEIS中。(除非获得批准,否则将是草案。)
Peter Kagey

Answers:



5

JavaScript(ES6), 111 ... 84  80字节

对于n = 2,返回true而不是1个ñ=2

f=(n,j)=>(g=(i,x,e=1)=>i?e>n?g(i-1,x):g(i-1,x+e)+g(i,x,e*x):x==n)(j,2)||f(n,-~j)

在线尝试!

已评论

f = (                     // f is the main recursive function taking:
  n,                      //   n = input
  j                       //   j = maximum number of steps
) => (                    //
  g = (                   // g is another recursive function taking:
    i,                    //   i = number of remaining steps
    x,                    //   x = current sum
    e = 1                 //   e = current exponentiated part
  ) =>                    //
    i ?                   // if there's still at least one step to go:
      e > n ?             //   if e is greater than n:
                          //     add the result of a recursive call with:
        g(i - 1, x)       //       i - 1, x unchanged and e = 1
      :                   //   else:
                          //     add the sum of recursive calls with:
        g(i - 1, x + e) + //       i - 1, x + e and e = 1
        g(i, x, e * x)    //       i unchanged, x unchanged and e = e * x
    :                     // else:
      x == n              //   stop recursion; return 1 if x = n
)(j, 2)                   // initial call to g with i = j and x = 2
|| f(n, -~j)              // if it fails, try again with j + 1

4

哈斯克尔 78 75字节

此实现在迭代所有必要映射的“树”中使用“呼吸优先”搜索x -> x + x^j

j#x=x+x^j
f n=[sum[1|x<-l,x==n]|l<-iterate((#)<$>[0..n]<*>)[2],n`elem`l]!!0

在线尝试!

说明

-- computes the mapping x -> x + x^j
j#x=x+x^j                          
--iteratively apply this function for all exponents [0,1,...,n] (for all previous values, starting with the only value [2])
                            iterate((#)<$>[0..n]<*>)[2] 
-- find each iteration where our target number occurs
    [                   |l<-...........................,n`elem`l] 
-- find how many times it occurs
     sum   [1|x<-l,x==n] 
-- pick the first entry
f n=.............................................................!!0




1

CJam(27字节)

qi2a{{_W$,f#f+~2}%_W$&!}ge=

在线演示

警告:这会非常占用大量内存。

解剖:

qi            e# Read input and parse to int n (accessed from the bottom of the stack as W$)
2a            e# Start with [2]
{             e# Loop
  {           e#   Map each integer x in the current list
    _W$,f#f+~ e#     to x+x^i for 0 <= i < n
    2         e#   and add a bonus 2 for the special case
  }%          e#   Gather these in the new list
  _W$&!       e#   Until the list contains an n
}g
e=            e# Count occurrences

红利2s(用于处理input的特殊情况2,因为while循环比do-while循环更昂贵)意味着列表的大小增长非常快,使用指数最多n-1意味着列表中较大数字的值增长非常快。



1

R78 77字节

function(n,x=2){while(!{a=sum(x==n)})x=rep(D<-x[x<n],n+1)+outer(D,0:n,'^')
a}

在线尝试!

使用简化的广度优先搜索

展开代码并提供说明:

function(n){                              # function taking the target value n

  x=2                                     # initialize vector of x's with 2

  while(!(a<-sum(x==n))) {                # count how many x's are equal to n and store in a
                                          # loop while a == 0

    x=rep(D<-x[x<n],n+1)+outer(D,0:n,'^') # recreate the vector of x's 
                                          # with the next values: x + x^0:n
  }
a                                         # return a
}   

具有较大内存分配的较短版本(在较大的情况下失败):

R70 69字节

function(n,x=2){while(!{a=sum(x==n)})x=rep(x,n+1)+outer(x,0:n,'^')
a}

在线尝试!

-1个字节感谢@RobinRyder


!(a<-sum(x==n))!{a=sum(x==n)}在两种情况下都可以为-1字节。
罗宾·赖德

0

Pyth,24个字节

VQIJ/mu+G^GHd2^U.lQ2NQJB

在线尝试!

这应该产生正确的输出,但是非常慢(TIO上的372测试用例超时)。我可以通过替换.lQ2为来使其更短Q,但这会使运行时变得可怕。

注意:对于不可达的数字不产生任何输出 ñ1个

说明

VQ                        # for N in range(Q (=input)):
   J                      #   J =
     m                    #     map(lambda d:
      u                   #       reduce(lambda G,H:
       +G^GH              #         G + G^H,
            d2            #         d (list), 2 (starting value) ),
              ^U.lQ2N     #       cartesian_product(range(log(Q, 2)), N)
    /                Q    #     .count(Q)
  IJ                  JB  #   if J: print(J); break
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.