출처 : OEIS- A071816
의 상한이 주어진 작업 n
은 방정식을 만족시키는 솔루션의 수를 찾는 것입니다.
a+b+c = x+y+z, where 0 <= a,b,c,x,y,z < n
순서는 OEIS 페이지에 설명 된대로 그리고 아래 (1)로 시작됩니다.
1, 20, 141, 580, 1751, 4332, 9331, 18152, 32661, 55252, 88913, 137292, 204763, 296492, 418503, 577744, 782153, 1040724, 1363573, 1762004, 2248575, 2837164, 3543035, 4382904, 5375005, 6539156, 7896825, 9471196, 11287235, 13371756
를 들어 n = 1
, 해결책은 단 하나입니다 :(0,0,0,0,0,0)
의 경우 n = 2
20 가지 주문 솔루션 (a,b,c,x,y,z)
이 있습니다 a+b+c = x+y+z
.
(0,0,0,0,0,0), (0,0,1,0,0,1), (0,0,1,0,1,0), (0,0,1,1,0,0), (0,1,0,0,0,1),
(0,1,0,0,1,0), (0,1,0,1,0,0), (0,1,1,0,1,1), (0,1,1,1,0,1), (0,1,1,1,1,0),
(1,0,0,0,0,1), (1,0,0,0,1,0), (1,0,0,1,0,0), (1,0,1,0,1,1), (1,0,1,1,0,1),
(1,0,1,1,1,0), (1,1,0,0,1,1), (1,1,0,1,0,1), (1,1,0,1,1,0), (1,1,1,1,1,1).
난 & O를
- 입력은를 나타내는 단일 정수입니다
n
. - 출력은 단일 정수 / 문자열 인 의미하는데
f(n)
,f(...)
상기 기능이다. - 인덱싱은 설명 된대로 정확하게 수행되며 다른 인덱싱은 허용되지 않습니다.
이것은 code-golf , 가장 낮은 바이트 수의 승리입니다.
답변
젤리 , 9 6 바이트
ṗ6ḅ-ċ0
O (n 6 ) 용액.
작동 원리
ṗ6ḅ-ċ0 Main link. Argument: n
ṗ6 Cartesian power 6; build all 6-tuples (a, x, b, y, c, z) of integers in
[1, ..., n]. The challenge spec mentions [0, ..., n-1], but since there
are three summands on each side, this doesn't matter.
ḅ- Unbase -1; convert each tuple from base -1 to integer, mapping (a, ..., z)
to a(-1)**5 + x(-1)**4 + b(-1)**3 + y(-1)**2 + c(-1)**1 + z(-1)**0, i.e.,
to -a + x - b + y - c + z = (x + y + z) - (a + b + c). This yields 0 if and
only if the 6-tuple is a match.
ċ0 Count the number of zeroes.
답변
Mathematica 17 또는 76 바이트
공식 사용하기 :
.55#^5+#^3/4+#/5&
(@GregMartin 및 @ngenisis 당 3 바이트 저장)
수식을 사용하는 대신 문자 그대로 모든 솔루션을 계산하고 계산합니다.
Length@Solve[a+b+c==x+y+z&&And@@Table[(0<=i<#),{i,{a,b,c,x,y,z}}],Integers]&
답변
하스켈 , 48 바이트
나는 이것을 작성하기 전에 공식을 알지 못했기 때문에 확실히 가장 짧은 (또는 가장 빠른) 일반적인 방법은 아니지만 그 방법이 예쁘다고 생각했습니다.
f n=sum[1|0<-foldr1(-)<$>pure[1..n]`mapM`[1..6]]
f n
에서 6 개의 요소 목록을 모두 생성 [1..n]
한 다음 교호 합계가 0 인 항목을 계산합니다 a+b+c==d+e+f
.와 동일한 사실을 사용하며 a-(d-(b-(e-(c-f))))==0
모든 숫자에 1을 추가해도 문제가되지 않습니다.
답변
MATL , 12 바이트
l6:"G:gY+]X>
설명
컨볼 루션을 다시 사용할 기회를 놓칠 수 없었습니다!
이는 OEIS의 다음 특성을 활용합니다.
a(n) = largest coefficient of (1+...+x^(n-1))^6
물론 다항식 곱셈은 컨볼 루션입니다.
l % Push 1
6:" % Do the following 6 times
G:g % Push a vector of n ones, where n is the input
Y+ % Convolution
] % End
X> % Maximum
답변
젤리 , 9 바이트
ṗ3S€ĠL€²S
@Dennis만큼 짧지는 않지만 입력을 위해 20 초 안에 완료됩니다 100
.
작동 원리
ṗ3S€ĠL€²S Main link. Argument: n
ṗ3 Cartesian power; yield all subsets of [1, ..., n] of length 3.
S€ Sum each.
Ġ Group indices by their values; for each unique sum S, list all indices whose
values are equal to S.
L€ Length each; for each unique sum S, yield the number of items in the original
array that sum to S.
² Square each; for each unique sum S, yield the number of pairs that both sum to S.
S Sum; yield the total number of equal pairs.
답변
Pyth, 13 12 바이트
JsM^UQ3s/LJJ
Leaky Nun 덕분에 1 바이트를 절약했습니다.
설명
JsM^UQ3s/LJJ
^UQ3 Get all triples in the range.
JsM Save the sums as J.
/LJJ Count occurrences of each element of J in J.
s Take the sum.