フラット化されたスパイラル順列インデックス


8

環境

n最初のn^2(つまりn2乗された)正の整数を含む列と行をもつ正方行列を考えます。ここで、nが奇数。行列の要素は、整数1n^2が、中心から始まり、最初は左に移動する反時計回りのらせん状に順次配置されるように配置されます。これらの行列を呼び出すM(n)

ために n=1、この単純に1つの要素行列を与えますM(1)=[[1]]

M(3) 行列です

9 8 7
2 1 6
3 4 5

M(5) 行列です

25 24 23 22 21
10  9  8  7 20
11  2  1  6 19
12  3  4  5 18
13 14 15 16 17

そして M(7)行列です

49 48 47 46 45 44 43
26 25 24 23 22 21 42
27 10  9  8  7 20 41
28 11  2  1  6 19 40
29 12  3  4  5 18 39
30 13 14 15 16 17 38
31 32 33 34 35 36 37

次に、行を上から連結して下に移動することにより、この行列をリスト/配列にフラット化することを検討してください。これらのリストを呼び出しますL(n)L(3)L(5)およびL(7)要素をスペースで区切って以下に示します。

9 8 7 2 1 6 3 4 5     (n=3)
25 24 23 22 21 10 9 8 7 20 11 2 1 6 19 12 3 4 5 18 13 14 15 16 17    (n=5)  
49 48 47 46 45 44 43 26 25 24 23 22 21 42 27 10 9 8 7 20 41 28 11 2 1 6 19 40 29 12 3 4 5 18 39 30 13 14 15 16 17 38 31 32 33 34 35 36 37    (n=7)

の順列の辞書式にソートされたリストでのインデックスi(n)を見つけることができますL(n)L(n)。Jellyでは、Œ¿アトムは作用するリストにこのインデックスを提供します。

チャレンジ

あなたの課題は、正の奇数の整数nを入力として受け取り、インデックスを出力することですi(n)です。

最初のいくつかの値は

n  i(n)
-------
1  1
3  362299
5  15511208759089364438087641
7  608281864033718930841258106553056047013696596030153750700912081

ご了承ください i(n)〜=である(n^2)!。これはOEISにはありません。

これは言語ごとのコードゴルフなので、可能な限り少ないバイト数でこれを実現します。

サンドボックスの投稿

回答:


3

ゼリー21 19バイト

-;,N$ṁx"RFḣNṙ-+\ỤŒ¿

オンラインでお試しください!

ボリュートに関するJの記事​​の方法に基づいています。

説明

-;,N$ṁx"RFḣNṙ-+\ỤŒ¿  Main link. Input: integer n
-;                   Prepend -1. [-1, n]
  ,N$                Pair with its negated value. [[-1, n], [1, -n]]
     ṁ               Mold it to length n.
        R            Range. [1, 2, ..., n]
      x"             Vectorized copy each value that many times.
         F           Flatten
           N         Negate n
          ḣ          Head. Select all but the last n values.
            ṙ-       Rotate left by -1 (right by 1).
              +\     Cumulative sum.
                Ụ    Grade up.
                 Œ¿  Permutation index.

2

J、 48 38バイト

@milesのおかげで-10バイト!

[:A.@/:_1+/\@|.(2#1+i.)#&}:+:$_1,],1,-

古い:

3 :'A.,(,~|.@(>:@i.@#+{:)@{.)@|:@|.^:(+:<:y),.1'

結果は0インデックスなので、 i(1) = 0i(5) = 15511208759089364438087640

説明(旧):

3 :'                                           ' | Explicit verb definition
                                            ,.1  | Make 1 into a 2d array
                                     (+:<:y)     | 4*n, where y = 2*n + 1
                                   ^:            | Repeat 4*n times
                              |:@|.              | Clockwise rotation
       (                    )@                   | Monadic Hook:
             (          )@{.                     | To the first row, apply...
                      {:                         | The last and largest item
                     +                           | Added to...
              >:@i.@#                            | The list 1, 2, ..., n; where n is the row length
          |.@                                    | Reverse
        ,~                                       | Append to the top of the array
      ,                                          | Ravel
    A.                                           | Permutation index

スパイラルを作るのはもっと速いかもしれませんが、方向はめちゃくちゃになります。

Jがこれをどのように最適化しているかはわかりませんが0.000414n=7(新しいJコンソールセッションで)計算するのに数秒しかかかりません。


たぶんJは私がジェリーにそれをさせた方法(コード)に似た何かをしますか?
ジョナサンアラン

私はあなたのメソッドを39バイトにゴルフしました[:A.@,,.@*0&((,~(#\.+{:)@{.)@|:|.)~2*<:。また、ボリュート記事のメソッドのバージョンを38バイトに変更しました[:A.@/:_1+/\@|.(2#1+i.)#&}:+:$_1,],1,-
マイル


1

MATL16 15 14バイト

lYL!PletY@wXmf

より大きいテストケースでは失敗 3浮動小数点の不正確さとメモリの制限の両方原因ます。

オンラインでお試しください!

説明

lYL    % Implicit input n. Spiral matrix of that side length
!P     % Transpose, flip vertically. This is needed to match the orientation
       % of columns in the spiral with that of rows in the challenge text
le     % Convert to a row, reading in column-major order (down, then across)
t      % Duplicate
Y@     % All permutations, arranged as rows of a matrix, in lexicographical
       % order
w      % Swap
Xm     % Row membership: gives a column vector containing true / false,
       % where true indicates that the corresponding row in the first input 
       % matches a row from the second output. In this case the second input
       % consists of a single row
f      % Find: gives indices of nonzeros. Implicit display

MATLにはスパイラルのビルトインがありますか?
Erik the Outgolfer 2018

@EriktheOutgolferそれは持っていることが1
ルイスMendo

説明を追加
Luis Mendo 2018
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.