Problem Author: | Hasan Saleh (MathModel) |
Prepared by: | Hasan Saleh (MathModel) |
Edited by: | Hasan Saleh (MathModel) |
Without the XOR condition, AM–GM puts the maximum of at a=b=c=x/3. The condition 3\mid a\oplus b\oplus c only deletes candidates, so we look for the answer near balance. Let t=x/3, m=\lfloor x/3\rfloor and
The product is not monotone in itself: for x=12, \delta(1,7,4)<\delta(8,2,2) yet 28<32. What is monotone is the best product at a given distance from balance.
For D\ge0, let denote the largest product attainable by a real triple at distance at least from balance:
Thus is a ceiling: no triple, integer or not, with \delta\ge D can have product above it.
Lemma. For 0\le D\le 2t,
attained at \left(t+D,\,t-\tfrac D2,\,t-\tfrac D2\right), and is strictly decreasing on .
Proof. By symmetry let a=t+s be an entry attaining , so |s|\ge D and -t\le s\le 2t. By AM–GM, bc\le\left(t-\tfrac s2\right)^2, so abc\le g(s)=t^3-\tfrac34s^2t+\tfrac14s^3. Since g(s)-g(-s)=\tfrac12s^3, the case s<0 is dominated by . Finally g'(u)=-\tfrac34u(2t-u)<0 on , so abc\le g(|s|)\le g(D), with equality at the stated triple.
A valid triple near balance. Since k\oplus k=0, the triple (k,k,x-2k) is valid exactly when 3\mid x-2k, i.e. k\equiv -x\pmod 3. As 3t\in\mathbb Z, the interval [t-\tfrac43,\,t+\tfrac43] always contains three consecutive integers, so some valid k=t-u has |u|\le\tfrac43; for x\ge25 both k\ge t-\tfrac43 and x-2k=t+2u\ge t-\tfrac83 are positive, so this is a legal triple. Its product is
because decreases in and h(u)\ge h(-u) for u\ge0.
The window. Comparing with the Lemma,
so for x\ge25 every optimal triple has \delta<3. Both the product and the condition are symmetric, so it suffices to enumerate a,b\in[\max(0,m-2),\,m+2] with c=x-a-b: a triple is missed only if no two of its entries lie in [m-2,m+2]. Then either some entry is \le m-3, giving \delta\ge3, or two entries are \ge m+3, forcing the third to be \le m-4. Hence the at most candidates always contain an optimum. The code scans the wider range [\max(1,m-3),\,m+2] and skips zero entries, which only matters when the answer is , already the initial value. The finitely many x<25 are verified against brute force; there x=1 has no valid triple, and for 2\le x\le4 the answer is .
Complexity per test, overall.
def solve():
x=int(input())
ans=0
for a in range(max(1,x//3-3),x//3+3):
for b in range(max(1,x//3-3),x//3+3):
c=x-a-b
if(c>=1 and (a^b^c)%3==0):
ans=max(ans,a*b*c)
print(ans)
for _ in range(int(input())):
solve()