Problem Author: | Hasan Saleh (MathModel) |
Prepared by: | Hasan Saleh (MathModel) |
Edited by: | Hasan Saleh (MathModel) |
Solution
Every pair contributes at least , so the all-ones array gives \binom{n}{2}. Let M=10^9, D=M-1 and
be the excess we must add.
Structure the array as a prefix of ones, then a block of large values, then exactly trailing ones. If the large values are non-decreasing, every pair inside the block costs ; a large value pays v-1 against each of the trailing ones. Writing E=q\cdot T+I with I=E\bmod q, the block must supply a base contribution of and generate exactly extra from its internal pairs.
To reach greedily, set c-1 values to and put the deficit in a single value V=T-(c-1)D+1. The extra units come from shifting right by positions among the 's: each preceding then pays instead of . This is worth exactly per position only when
so both bounds must be enforced. A single smaller element among c-1 larger ones creates at most c-1 such pairs, hence I<c is required, and the layout must fit, c+q\le n.
Taking c=\lceil T/D\rceil breaks the upper bound: when D\mid T the greedy fills every slot to capacity, V=M, and each intended pair degenerates to \lceil M/M\rceil=1, leaving the sum short by exactly . For I>0 take instead
which guarantees V<M and T<cD.
If V\le M/2 the greedy placement is abandoned and the sum is spread evenly: with S=T+c=cQ+r, set X=Q+1 and Y=S-(c-1)X=Q+r-c+1, so (c-1)(X-1)+(Y-1)=T identically. Here T<cD gives X\le M, and Q>\frac{(c-1)D}{c}\ge\frac{D}{2} against c\le 10^6 gives 2Y-X=Q+2r-2c+1>0, so Y<X\le 2Y and the shifted positions again pay exactly each.
Since k\le 10^{12}, the choice q=1 already succeeds whenever n-1\ge\lceil E/D\rceil, so the loop advances past q=1 only for n\le 1002: the scan is O(\min(n,1002)) and the construction is per test, O(\sum n) overall.
def solve():
n,k=map(int,input().split())
E=k-n*(n-1)//2
if E==0:
print(*[1]*n)
return
for q in range(1,n):
I,T=E%q,E//q
c=(T+999999998)//999999999
if I>0 and T%999999999==0: c+=1
if c<=n-q and I<c:
V=T-(c-1)*999999999+1
X,Y=1000000000,V
if I>0 and V<=500000000:
X=(T+c)//c+1
Y=T+c-(c-1)*X
print(*([1]*(n-q-c)+[X]*I+[Y]+[X]*(c-I-1)+[1]*q))
return
for _ in range(int(input())):
solve()