質問: n
≥2の場合、 n
次元 nxnxnxnxnxn
の格子上に、
... xn0
から n-1
までの座標の範囲は、少なくとも n
離れていますか?ペアの
{(2,1,3,1)、(3,2,1,3)}
および {(3,2,1,3)、(2,1、
3,1)}
は、逆の順序で同じ2つの点で構成されているため、互いに区別されているとはみなされません。ペアの総数は非常に急速に増加することに注意してください。合計ペア数は
6
、 351
、 32 640
、 4
、
881 2501 088 367 840
など
テストケース:
2 -> 0 (all pairs are at most a distance of sqrt(2) < 2 apart)
3 -> 28 (They must either be (2,2,1) or a permutation apart, or (2,2,2) apart. Each corner
has three non-corner (2,2,1) points corresponding to it. And each corner is associated
with a corner pair that is a (2,2,2). Thus. 3.5 * 8 = 28.
4 -> 4,888
5 -> 1,501,948
6 -> 486,039,360 (I would like someone to verify this if possible)
Your code should work for n <= 5, at least in theory.
Don’t hardcode it, that’s a standard loophole.
ベストアンサー
MATL, 12 bytes
tt:Z^tZPR>~z
説明
tt % Implicit input n. Duplicate twice
% STACK: n, n, n
: % Range [1 2 ... n]
% STACK: n, n, [1 2 ... n]
Z^ % Cartesian power. Gives an n^n × n matrix C where each row is a Cartesian tuple
% STACK: n, C
t % Duplicate
% STACK: n, C, C
ZP % Euclidean distance. Gives an n^n × n^n matrix D of pairwise distances
% STACK: n, D
R % Upper triangular part: sets elements below the main diagonal to 0. Call that U
% STACK: n, U
>~ % Less than or equal? Element-wise. Gives a true-false matrix B
% STACK: n, B
z % Number of nonzeros. Implicitly display
% STACK: number of entries in B that equal true