Mathematica,58个字节,多项式(n)时间
Abs[Sum[(k-1)Hypergeometric2F1[k,k-#,2,2](#-k)!,{k,#}]-1]&
怎么运行的
与其使用蛮力迭代排列,不如使用包含-排除原理对它们进行组合计数。
设S是该组的所有排列的[1,...,N]与σ 1 = 1,σ Ñ = Ñ,并令S 我是集合置换σ∈S,从而使|σ 我 - σ 我 + 1 | =1。那么我们要寻找的数量是
| S | - | S 1 ∪⋯∪小号ñ - 1 | =Σ 2≤ ķ ≤ Ñ + 1; 1≤ 我2 <⋯< 我ķ - 1 < Ñ(-1)的k - 2 | S 我2 ∩⋯∩小号我ķ - 1 |。
现在,| S 我2 ∩⋯∩小号我ķ - 1 | 仅取决于k和[ i 1,i 2,…,i k -1,i k ]中连续索引的游程数j,为方便起见,我们将i 1 = 0和i k = n固定。特别,
| S 我2 ∩⋯∩小号我ķ - 1 | = 2 Ĵ - 2(Ñ - ķ)!2≤ Ĵ ≤ ķ ≤ Ñ,
| S 我2 ∩⋯∩小号我ķ - 1 | = 1,对于j = 1,k = n + 1。
带有j游程的此类索引集[ i 1,i 2,…,i k − 1,i k ]的数量为
(k − 1摄氏度 Ĵ - 1)( ñ - ķ Ç Ĵ - 2),2≤ Ĵ ≤ ķ ≤ Ñ,
如图1所示, Ĵ = 1, ķ = Ñ + 1。
结果是
(-1)ñ - 1 +Σ 2≤ ķ ≤ Ñ Σ 2≤ Ĵ ≤ ķ (-1)ķ - 2( ķ - 1 c ^ Ĵ - 1)( ñ - ķ Ç Ĵ - 2)2 Ĵ - 2( n - k)!
j的内部和可以使用超几何2 F 1函数来写:
(-1)ñ - 1 +Σ 2≤ ķ ≤ Ñ(-1) ķ( ķ - 1) 2 ˚F 1(2 - ķ, ķ - Ñ ; 2; 2)( ñ - ķ)!
我们对其应用了Pfaff变换,使我们可以使用绝对值消除-1的幂:
(-1)ñ - 1 +Σ 2≤ ķ ≤ Ñ(-1)Ñ(ķ - 1)2 ˚F 1(ķ,ķ - Ñ ; 2; 2)(ñ - ķ)!
= | -1 +Σ 1≤ ķ ≤ Ñ(ķ - 1)2 ˚F 1(ķ,ķ - Ñ ; 2; 2)(ñ - ķ!)|。
演示版
In[1]:= Table[Abs[Sum[(k-1)Hypergeometric2F1[k,k-#,2,2](#-k)!,{k,#}]-1]&[n],{n,50}]
Out[1]= {1, 0, 0, 0, 0, 2, 10, 68, 500, 4174, 38774, 397584, 4462848,
> 54455754, 717909202, 10171232060, 154142811052, 2488421201446,
> 42636471916622, 772807552752712, 14774586965277816, 297138592463202402,
> 6271277634164008170, 138596853553771517492, 3200958202120445923684,
> 77114612783976599209598, 1934583996316791634828454,
> 50460687385591722097602304, 1366482059862153751146376304,
> 38366771565392871446940748410, 1115482364570332601576605376898,
> 33544252621178275692411892779180, 1042188051349139920383738392594332,
> 33419576037745472521641814354312790,
> 1105004411146009553865786545464526206,
> 37639281863619947475378460886135133496,
> 1319658179153254337635342434408766065896,
> 47585390139805782930448514259179162696722,
> 1763380871412273296449902785237054760438426,
> 67106516021125545469475040472412706780911268,
> 2620784212531087457316728120883870079549134420,
> 104969402113244439880057492782663678669089779118,
> 4309132147486627708154774750891684285077633835734,
> 181199144276064794296827392186304334716629346180848,
> 7800407552443042507640613928796820288452902805286368,
> 343589595090843265591418718266306051705639884996218154,
> 15477521503994968035062094274002250590013877419466108978,
> 712669883315580566495978374316773450341097231239406211100,
> 33527174671849317156037438120623503416356879769273672584588,
> 1610762789255012501855846297689494046193178343355755998487686}
[2..n-1]
包含1
或的增量-1
,还必须检查它们是否没有2
以n-1
... 开头或结尾...