让我们做一个三角形


15

大多数人都熟悉Pascal的三角形。

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

帕斯卡(Pascal)的三角形是一个自动机,其中单元格的值是左上角和右上角的单元格之和。现在我们将定义一个相似的三角形。不仅将单元格带到左上角和右上角,我们还将沿延伸到左上角和右上角的两条无限线来获取所有单元格。就像Pascal的三角形一样,我们从一个1零开始无限填充,然后从那里向下构建。

例如,计算用 x

   1
  1 1
 2 2 2
4 5 5 4
   x

我们将对以下单元格求和

   .
  . .
 2 . 2
. 5 5 .
   x

制作我们的新细胞14

任务

给定一个行号(n),然后计算距左数(r)的距离,并在第n行上从左数计算第r个非零项。(在Pascal三角形上的等效值为nCr)。您可以假设r小于n

这是,我们的目标是尽量减少解决方案中的字节数。

测试用例

0,0 -> 1
1,0 -> 1
2,0 -> 2
4,2 -> 14
6,3 -> 106

这是三角形形式的前几行:

                  1
                1   1
              2   2   2
            4   5   5   4
          8  12  14  12   8
       16  28  37  37  28  16
     32  64  94  106 94  64  32
   64  144 232 289 289 232 144 64
 128 320 560 760 838 760 560 320 128


我们的提交可以改用基于1的索引吗?
Kritixi Lithos'7

9
@KritixiLithos当然。不过这会让我难过。
发布Rock Garf Hunter'7

Answers:


8

果冻18 17字节

SṚ0;+Sṭ
1WWÇ⁸¡ṪṙḢ

使用基于0的索引。

在线尝试!

怎么运行的

1WWÇ⁸¡ṪṙḢ  Main link. Arguments: n, r

1          Set the return value to 1.
 W         Wrap; yield [1].
  W        Wrap; yield [[1]].
           This is the triangle with one row.
   Ç⁸¡     Call the helper link n times.
           Each iteration adds one row to the triangle.
      Ṫ    Tail; take the last array, i.e., the row n of the triangle.
       ṙ   Rotate row n r units to the left.
        Ḣ  Head; take the first element, i.e., entry r of row n.


SṚ0;+Sṭ    Helper link. Argument: T (triangle)

S          Take the column-wise sums, i.e., sum the ascending diagonals of the 
           centered triangle, left to right.
 Ṛ         Reverse the array of sums. The result is equal to the sums of the 
           descending diagonals of the centered triangle, also left to right.
  0;       Prepend a 0. This is required because the first element of the next row 
           doesn't have a descending diagonal.
     S     Take the column-wise sum of T.
    +      Add the ascending to the descending diagonals.

5

Python 3,72个字节

1字节感谢Kritixi Lithos。

f=lambda n,r:n>=r>=0and(0**n or sum(f(i,r)+f(i,r+i-n)for i in range(n)))

在线尝试!


1
您可以重新排列以获取此信息:n>=r>=0and并保存一个字节
Kritixi Lithos'7

@EinkornEnchanter如果n为0,则为1;否则,它的值为0。类似于n and ... or 1,但更短。
Leaky Nun

我知道,很可能会滥用行为来破坏+1。
发布Rock Garf Hunter'7

@EinkornEnchanter 0^01 大多数编程语言
Arnauld

@Arnauld那没有实现;)
发布Rock Garf Hunter,

3

ES6,80 78个字节

p=(n,r,c=0)=>n?(o=>{while(n&&r<n)c+=p(--n,r);while(o*r)c+=p(--o,--r)})(n)||c:1

在行动!

感谢Arnauld,两个字节。


您可以使用while(n&&r<n)和保存2个字节while(o*r)
Arnauld

1
这个答案是完全正确的。因此,无论是谁投票否决,绝对应该提供一个解释……或者它可以是来自Community的这些奇怪的自动downvotes之一?
Arnauld

2

PHP,94字节

递归方式0索引

function f($r,$c){for($s=$r|$c?$r<0?0:!$t=1:1;$t&&$r;)$s+=f($r-=1,$c)+f($r,$c-++$i);return$s;}

在线尝试!

PHP,125字节

0索引

for(;$r<=$argv[1];$r++)for($z++,$c=~0;++$c<$z;$v+=$l)$x[$c]+=$t[+$r][$c]=$l=($v=&$y[$r-$c])+$x[$c]?:1;echo$t[$r-1][$argv[2]];

在线尝试!

PHP> = 7.1,159字节

0索引用于50列以上的行

for([,$m,$n]=$argv;$r<=$m;$r++)for($z++,$c=0;$c<$z;$v=bcadd($v,$l),$x[$c]=bcadd($x[$c],$l),$c++)$t[+$r][$c]=$l=bcadd(($v=&$y[$r-$c]),$x[$c])?:1;echo$t[$m][$n];


0

帕斯卡,145字节

function f(n,k:integer):integer;var i,r:integer;begin r:=1-Ord((k<0)or(k>n));if n>0 then r:=0;for i:=1 to n do r:=r+f(n-i,k-i)+f(n-i,k);f:=r;end;

在线尝试!

使用T(n, r) = T(n-1, r-1) + T(n-1, r)递归。

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.