Haidora and sticks#12297

Haidora and sticks (Editorial)

Problem Author:

Haidara Al Makdid

Prepared by:

Haidara Al Makdid

Edited by:

Hasan Saleh (MathModel)

Solution

It's optimal to construct with the Triangular Construction i.e. we keep summing numbers from to until sum S \le x , We know that the sum of first integers is :

\displaystyle T(m)=\sum_{i=1}^m i = \frac{m(m+1)}{2}

Notice that is increasing ! , thus we can binary search on maximum such that T(m) \le m , With bounds l=1 and r=2 \cdot 10^9 which will be safe , since T(2 \cdot 10^9) > 2 \cdot 10^{18} which is sufficient in order to solve the problem and also fits the long 'long long' range for C++.

Once we get , we come to find the maximum stick length , the answer in this case will be the maximum of — the largest integer that we used in the triangular construction and the maximum of left integers , Fromally

\max(m,n-T(m-1))

Complexity \mathcal{O}(\log(2\cdot 10^9) \cdot t).

def T(n):return (n*(n+1))//2

def solve():
    n=int(input())
    l,r=1,2*10**9
    while(l<=r):
        m=(l+r)//2
        if(T(m)<n<T(m+1)):
            ans=m+1
            break
        if(T(m)==n):
            ans=m
            break
        if(T(m)<n):
            l=m+1
        if(T(m)>n):
            r=m-1
    if(T(ans)>n):ans-=1
    print(ans,max(ans,n-T(ans-1)))

for _ in range(int(input())):
    solve()