You are given a positive integer $N$. Consider the sequence $S = (1, 2, \ldots, N)$. You should choose two elements of this sequence and swap them.
A swap is nice if there is an integer $M$ ($1 \le M < N$) such that the sum of the first $M$ elements of the resulting sequence is equal to the sum of its last $N-M$ elements. Find the number of nice swaps.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains a single integer $N$.

-----Output-----
For each test case, print a single line containing one integer ― the number of nice swaps.

-----Constraints-----
- $1 \le T \le 10^6$
- $1 \le N \le 10^9$

-----Subtasks-----
Subtask #1 (10 points):
- $T \le 10$
- $N \le 10^3$
Subtask #2 (30 points):
- $T \le 10$
- $N \le 10^6$
Subtask #3 (60 points): original constraints

-----Example Input-----
5
1
2
3
4
7

-----Example Output-----
0
0
2
2
3

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Mathematics

Ad-hoc


Solutions:
from math import sqrt
for _ in range(int(input())):
	n = int(input())
	sum = n * (n + 1) // 2
	if sum % 2 != 0:
		print(0)
		continue
	m = int(sqrt(1 + 4 * sum) - 1) // 2
	if m * (m + 1) // 2 == sum // 2:
		print((m - 1) * m // 2 + n - m + (n - m - 1) * (n - m) // 2)
	else:
		print(n - m)


import math

def formula(n):
	return (-2.0 + math.sqrt(4.0 + 8 * (n * n + n))) // 4

def nc2(x):
	return (x * x - x) // 2
for _ in range(int(input())):
	n = int(input())
	if (n * n + n) // 2 % 2 != 0:
		print(0)
	else:
		ans = 0
		x = formula(n)
		ans += n - x
		if 2 * (x * x + x) == n * n + n:
			ans += nc2(x) + nc2(n - x)
		print(int(ans))


import math

def eq_root(s):
	return (-1 + math.sqrt(1 + 4 * s)) / 2

def nc2(n):
	return int(n * (n - 1) / 2)

def chefina(n):
	s = int(n * (n + 1) / 2)
	if s % 2 == 1:
		return 0
	count = 0
	x = math.floor(eq_root(s))
	count += n - x
	if 2 * x * (x + 1) == n * (n + 1):
		count += nc2(x) + nc2(n - x)
	return count
try:
	tstc = int(input())
	for t in range(tstc):
		n = int(input())
		print(chefina(n))
except:
	pass


import math as m
for _ in range(int(input())):
	n = int(input())
	if n * (n + 1) // 2 & 1:
		print(0)
		continue
	(s, ans, t) = ((n * n + n) // 4, n - m.floor((-1 + m.sqrt(2 * n * n + 2 * n + 1)) / 2), m.sqrt(2 * n * n + 2 * n + 1))
	if t == m.floor(t):
		t = (int(t) - 1) // 2
		t1 = n - t
		ans += t * (t - 1) // 2 + t1 * (t1 - 1) // 2
	print(ans)


import math
t = int(input())
for i in range(t):
	n = int(input())
	summ = n * (n + 1) // 2
	if summ % 2:
		print(0)
	else:
		x = (1 + 4 * summ) ** 0.5
		x -= 1
		x = x // 2
		x = int(x)
		total = n - x
		left = x * (x + 1) // 2
		right = n * (n + 1) // 2 - left
		if left == right:
			r = n - x
			total += x * (x - 1) // 2
			total += r * (r - 1) // 2
		print(total)


import math

def get_input():
	return input().split(' ')

def get_int_input():
	return list(map(int, get_input()))

def comb(x):
	return int(x * (x - 1) // 2)

def chfnswap(N):
	S = N * (N + 1) // 2
	if S % 2:
		return 0
	N_for_half = int((math.sqrt(4 * S + 1) - 1) / 2)
	ans = 0
	if N_for_half * (N_for_half + 1) == S:
		ans += comb(N_for_half) + comb(N - N_for_half)
		N_for_half -= 1
	diff = int(S // 2 - N_for_half * (N_for_half + 1) // 2)
	ans += min(N - diff, N - N_for_half)
	return ans

def main():
	t = get_int_input()[0]
	for _ in range(t):
		N = get_int_input()[0]
		print(chfnswap(N))
main()


t = int(input())
while t:
	t -= 1
	n = int(input())
	if n % 4 != 0 and (n + 1) % 4 != 0:
		print(0)
	else:
		sum = n * (n + 1) / 2
		mid = (-1 + (1 + 4 * sum) ** 0.5) / 2
		if mid - int(mid) == 0:
			mid = int(mid)
			cnt = mid * (mid - 1) / 2 + (n - mid) * (n - mid - 1) / 2 + n - mid
			print(int(cnt))
		else:
			print(n - int(mid))


import math

def snek(n):
	return n * (n - 1) // 2
t = int(input())
for i in range(t):
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2 == 0:
		x = int((math.sqrt(4 + 8 * (n * n + n)) - 2) // 4)
		ans = n - x
		if x * (x + 1) // 2 == s // 2:
			ans += snek(x) + snek(n - x)
		print(ans)
	else:
		print('0')


import math
t = int(input())
import operator as op
from functools import reduce

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, list(range(n, n - r, -1)), 1)
	denom = reduce(op.mul, list(range(1, r + 1)), 1)
	return numer // denom
for test in range(t):
	n = int(input())
	sum_array = n * (n + 1) // 2
	if sum_array % 2 != 0:
		print(0)
		continue
	n_swap = 0
	d = 1 + 4 * sum_array
	partition_ind = (math.sqrt(d) - 1) / 2
	n_swap = n - math.floor(partition_ind)
	f = math.factorial
	if partition_ind.is_integer():
		partition_ind = int(partition_ind)
		if partition_ind >= 2:
			n_swap += ncr(partition_ind, 2)
		if n - partition_ind >= 2:
			n_swap += ncr(n - partition_ind, 2)
	print(n_swap)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = n * (n + 1) // 2
	t = 0
	if s % 2 != 0:
		print(0)
	else:
		m = int((-1 + (1 + 4 * s) ** 0.5) // 2)
		t = n - m
		ls = int(m * (m + 1) // 2)
		rs = s - ls
		if ls == rs:
			t += m * (m - 1) // 2 + (n - m) * (n - m - 1) // 2
		print(t)


import math

def sum_naturals(num):
	return (num + 1) * num / 2

def find_nice_swaps():
	sum_n = sum_naturals(n)
	if sum_n % 2 != 0:
		return 0
	x = (math.sqrt(4 * sum_n + 1) - 1) / 2
	left = 0
	right = 0
	if x == int(x):
		if x >= 2:
			left = math.factorial(x) // (2 * math.factorial(x - 2))
		if n - x >= 2:
			right = math.factorial(n - x) // (2 * math.factorial(n - x - 2))
	return n - int(x) + left + right
testcases = int(input())
while testcases > 0:
	n = int(input())
	print(find_nice_swaps())
	testcases -= 1


def swap(test):
	count = 0
	n = int(input())
	sum1 = n * (n + 1) // 2
	if sum1 % 2 != 0:
		return 0
	else:
		mid = int((-1 + (1 + 4 * sum1) ** 0.5) // 2)
		sum_start = mid * (mid + 1) // 2
		sum_end = sum1 - sum_start
		count = n - mid
		if sum_start == sum_end:
			count += mid * (mid - 1) // 2 + (n - mid) * (n - mid - 1) // 2
	return count
t = int(input())
l = []
for i in range(t):
	res = swap(i)
	l.append(res)
for i in l:
	print(i)


for _ in range(int(input())):
	n = int(input())
	sm = n * (n + 1) // 2
	if sm % 2 != 0:
		print('0')
	else:
		ans = (-1 + (1 + 4 * sm) ** 0.5) // 2
		mid = int(ans)
		total = n - mid
		left = mid * (mid + 1) // 2
		right = sm - left
		if left == right:
			r = n - mid
			total += r * (r - 1) // 2 + mid * (mid - 1) // 2
		print(total)


import math
t = int(input())
while t > 0:
	ans = 0
	n = int(input())
	total = n * (n + 1) >> 1
	if total & 1 == 0:
		pivot = math.floor((-1 + math.sqrt(1 + 4 * total)) / 2)
		ans += n - pivot
		if pivot * (pivot + 1) >> 1 == total >> 1:
			ans += math.factorial(pivot) // (math.factorial(pivot - 2) * 2)
			if n - pivot > 1:
				ans += math.factorial(n - pivot) // (math.factorial(n - pivot - 2) * 2)
	print(ans)
	t -= 1


import math
t = int(input())
while t > 0:
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2 != 0:
		print('0')
	else:
		tp = (math.sqrt(1 + 4 * s) - 1) // 2
		temp = math.floor(tp)
		ls = temp * (temp + 1) // 2
		r = n - temp
		rs = s - ls
		if rs == s // 2:
			a1 = r * (r - 1) // 2
			a2 = temp * (temp - 1) // 2
			print(int(a1 + a2 + r))
		else:
			print(r)
	t = t - 1


from math import floor, sqrt
n = int(input())
for i in range(n):
	m = int(input())
	if m % 4 == 1 or m % 4 == 2:
		print(0)
	else:
		s = m * (m + 1) // 2
		res = 0
		sm = s // 2
		p1 = floor((sqrt(4 * s + 1) - 1) / 2)
		s1 = p1 * (p1 + 1) // 2
		p = [p1 - 1, p1, p1 + 1]
		s = [s1 - p1, s1, s1 + p[2]]
		l = [sm - s[0], sm - s1, sm - s[2]]
		for j in range(len(l)):
			if l[j] == 0:
				res += (p[j] * (p[j] - 1) + (m - p[j] - 1) * (m - p[j])) // 2
			elif l[j] > 0 and l[j] < m:
				res += min([l[j], m - l[j], p[j], m - p[j]])
		print(res)


import math
test = int(input())
for _ in range(test):
	n = int(input())
	d = n * (n + 1) / 4
	if n * (n + 1) % 4 != 0:
		print('0')
	else:
		x = math.floor((-1 + math.sqrt(1 + 8 * d)) // 2)
		if x * (x + 1) / 2 == 3:
			print('2')
		elif x * (x + 1) / 2 == d:
			print(n - x) + sum(range(x)) + sum(range(n - x))
		else:
			print(n - x)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	d = n * (n + 1) / 4
	if n * (n + 1) % 4 != 0:
		print(0)
	else:
		x = math.floor((-1 + math.sqrt(1 + 8 * d)) // 2)
		if x * (x + 1) / 2 == d:
			print(n - x + sum(range(x)) + sum(range(n - x)))
		else:
			print(n - x)


import math

def comb(n):
	return n * (n - 1) // 2
for _ in range(int(input())):
	ans = 0
	n = int(input())
	if n * (n + 1) // 2 % 2 != 0:
		print(0)
		continue
	else:
		x = int((-2 + math.sqrt(4 + 8 * n * (n + 1))) // 4)
		ans += n - x
		if 2 * x * (x + 1) == n * (n + 1):
			ans += comb(x) + comb(n - x)
		print(int(ans))


from sys import stdin
from math import sqrt
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = n * (n + 1) // 2
	if s % 2 != 0:
		print(0)
	else:
		x = int((-1 + sqrt(1 + 4 * s)) // 2)
		ans = n - x
		x1 = x * (x + 1) // 2
		y = n - x
		if s // 2 == x1:
			ans += x * (x - 1) // 2 + y * (y - 1) // 2
			print(ans)
		else:
			print(ans)


for _ in range(int(input())):
	n = int(input())
	sum_t = n * (n + 1) // 2
	count = 0
	if sum_t % 2 != 0:
		print(0)
	else:
		ans = (1 + 4 * sum_t) ** 0.5
		ans -= 1
		mid = int(ans // 2)
		left = mid * (mid + 1) // 2
		right = n * (n + 1) // 2 - left
		total = n - mid
		if left == right:
			right = n - mid
			right -= 1
			mid -= 1
			total += mid * (mid + 1) // 2
			total += right * (right + 1) // 2
		print(int(total))


import math
t = int(input())
for i in range(t):
	n = int(input())
	k = n * (n + 1) // 2
	sum = 0
	m = 0
	if k & 1:
		print(0)
	else:
		x = (-1 + int(math.sqrt(1 + 4 * k))) // 2
		ans = n - x
		if 2 * x * (x + 1) == n * (n + 1):
			ans += x * (x - 1) // 2 + (n - x) * (n - x - 1) // 2
		print(ans)


import math

def comb(n):
	return n * (n - 1) // 2
t = int(input())
while t > 0:
	ans = 0
	n = int(input())
	total = n * (n + 1) >> 1
	if total & 1 == 0:
		pivot = math.floor((-1 + math.sqrt(1 + 4 * total)) / 2)
		ans += n - pivot
		if pivot * (pivot + 1) >> 1 == total >> 1:
			ans += comb(pivot) + comb(n - pivot)
	print(ans)
	t -= 1


import math
for _ in range(0, int(input())):
	n = int(input())
	sum = n * (n + 1) // 2
	if sum % 2 != 0:
		print(0)
	else:
		k = (-1 + math.sqrt(1 + 4 * sum)) // 2
		m = int(k)
		res = n - m
		l = m * (m + 1) // 2
		r = sum - l
		if l == r:
			rs = n - m
			rs = rs - 1
			m = m - 1
			res = res + m * (m + 1) // 2
			res = res + rs * (rs + 1) // 2
		print(res)


import math

def pos(sm):
	a = math.sqrt(1 + 4 * sm)
	a = a - 1
	a = a // 2
	fs = a
	return fs
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 0 or n == 1 or n == 2:
		print(0)
		continue
	tl = n * (n + 1) // 2
	if tl % 2 != 0:
		print(0)
		continue
	ind = pos(tl)
	ss = ind * (ind + 1) // 2
	if tl // 2 == ss:
		ans = ind * (ind - 1) // 2 + (n - ind) * (n - ind - 1) // 2 + (n - ind)
		print(int(ans))
	else:
		print(int(n - ind))


from math import sqrt
t = int(input())
while t > 0:
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2 != 0:
		print(0)
		t -= 1
		continue
	m = int(sqrt(1 + 4 * s) - 1) // 2
	if m * (m + 1) // 2 == s // 2:
		print((m - 1) * m // 2 + n - m + (n - m - 1) * (n - m) // 2)
	else:
		print(n - m)
	t -= 1


import math
from decimal import Decimal

def swapvalues(temp, n, m):
	if m < temp:
		if temp + 1 <= n:
			return n - temp
		else:
			return 0
	else:
		l1 = temp
		l2 = n - m
		if temp < n - m:
			return l1
		else:
			return l2

def binomialCoeff(n):
	res = n * (n - 1) / Decimal(2)
	return res

def root(c):
	x1 = (-1 + math.sqrt(1 - 4 * c)) / 2
	return math.ceil(x1)
for _ in range(int(input())):
	n = Decimal(input())
	sumofn = n * (n + 1) / Decimal(2)
	if sumofn % 2 != 0:
		print(0)
		continue
	c = (3 * n - pow(n, 2) - 4) / 2
	m = root(c)
	sumofm = m * (m + 1)
	temp = (sumofn - sumofm) / 2
	ans = 0
	while True:
		if temp == 0:
			ans += binomialCoeff(m) + binomialCoeff(n - m)
		elif temp < 1:
			break
		elif n - 1 >= temp:
			if temp % 1 == 0:
				ans += swapvalues(temp, n, m)
		m += 1
		temp -= m
	print(int(ans))


import math
t = int(input())

def work(n):
	x1 = (-1.0 + math.sqrt(1 + 2 * (n * (n + 1)))) // 2
	return x1
for i in range(t):
	n = int(input())
	sum1 = n * (n + 1) // 2
	if sum1 % 2 != 0:
		print(0)
	else:
		ans = 0
		x = work(n)
		x = int(x)
		ans += n - x
		if 2 * (x * (x + 1)) == 2 * sum1:
			ans += x * (x - 1) // 2 + (n - x) * (n - x - 1) // 2
		print(ans)


from math import sqrt

def work(n):
	x = (-2 + sqrt(4 + 8 * (n * n + n))) // 4
	return int(x)

def c(y):
	p = y * (y - 1) // 2
	return p
t = int(input())
while t != 0:
	n = int(input())
	k = n * (n + 1) // 2
	swaps = 0
	if k % 2 != 0:
		print(0)
		t -= 1
		continue
	w = work(n)
	if 2 * (w * w + w) == n * n + n:
		swaps = n - w + c(w) + c(n - w)
	else:
		swaps = n - w
	print(swaps)
	t -= 1


import math
t = int(input())
for test in range(t):
	n = int(input())
	sum_array = n * (n + 1) // 2
	if sum_array % 2 != 0:
		print(0)
		continue
	ideal_sum = sum_array // 2
	partition_sum = 0
	partition_ind = 0
	n_swap = 0
	for i in range(1, n + 1):
		if i + partition_sum > ideal_sum:
			partition_ind = i - 1
			break
		else:
			partition_sum += i
	n_swap = n - partition_ind
	f = math.factorial
	if partition_sum == ideal_sum:
		if partition_ind >= 2:
			n_swap += f(partition_ind) // 2 // f(partition_ind - 2)
		if n - partition_ind >= 2:
			n_swap += f(n - partition_ind) // 2 // f(n - partition_ind - 2)
	print(n_swap)


import math

def sumofn(x):
	return int(x * (x - 1) / 2)
a = int(input())
for o in range(a):
	n = int(input())
	p = 0
	if n % 4 == 3 or n % 4 == 0:
		a = n * (n + 1) / 2 / 2
		c = math.floor(((1 + 8 * a) ** (1 / 2) - 1) / 2)
		if c * (c + 1) / 2 == a:
			p += sumofn(c) + sumofn(n - c) + n - c
		else:
			p = n - c
	print(p)


import math

def pv(p):
	return int((-2 + math.sqrt(4 + 8 * (p * p + p))) // 4)

def nC2(m):
	return (m * m - m) // 2
for _ in range(int(input())):
	n = int(input())
	if (n * n + n) // 2 & 1:
		print(0)
		continue
	x = pv(n)
	ans = n - x
	if 2 * (x * x + x) == n * n + n:
		ans += nC2(x) + nC2(n - x)
	print(ans)


from sys import stdin
input = stdin.readline
import math
t = int(input())
for _ in range(t):
	n = int(input())
	sum = n * (n + 1) // 2
	if sum % 2 != 0:
		print('0')
	else:
		mid = -1 + (1 + 4 * sum) ** 0.5
		mid = int(mid // 2)
		ls = mid * (mid + 1) // 2
		r = n - mid
		rs = sum - ls
		if rs == ls:
			a1 = r * (r - 1) // 2
			a2 = mid * (mid - 1) // 2
			print(int(a1 + a2 + r))
		else:
			print(r)


def binary_search(n, target):
	lo = 1
	hi = n
	while lo <= hi:
		mid = (lo + hi) // 2
		x = mid * (mid + 1) // 2
		if x < target:
			lo = mid + 1
		elif x > target:
			hi = mid - 1
		else:
			return (True, mid)
	return (False, hi)

def solve(n):
	m = n * (n + 1) // 2
	if m % 2 == 1:
		return 0
	(find, j) = binary_search(n, m // 2)
	ans = 0
	if find:
		k = n - j
		ans += j * (j - 1) // 2
		ans += k * (k - 1) // 2
	return ans + n - j
for _ in range(int(input())):
	n = int(input())
	print(solve(n))


for test in range(int(input())):
	num = int(input())
	summ = num * (num + 1) // 2
	if summ % 2 == 0:
		x = (1 + 4 * summ) ** 0.5
		x -= 1
		x = int(x // 2)
		answer = num - x
		l = x * (x + 1) // 2
		r = summ - l
		if l == r:
			remaining = num - x - 1
			x -= 1
			answer += x * (x + 1) // 2
			answer += remaining * (remaining + 1) // 2
		print(answer)
	else:
		print(0)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	sum_ = n * (n + 1) // 2
	if sum_ % 2 != 0:
		print(0)
	else:
		m = (-1 + int(sqrt(1 + 4 * sum_))) // 2
		if m * (m + 1) // 2 == sum_ // 2:
			print(m * (m - 1) // 2 + (n - m) + (n - m) * (n - m - 1) // 2)
		else:
			print(n - m)


import math
for _ in range(int(input())):
	n = int(input())
	if n % 4 != 0 and (n + 1) % 4 != 0:
		print(0)
	else:
		d = n * (n + 1) // 2
		s = round(math.sqrt(d))
		c = n - s
		if s * (s + 1) // 2 == d // 2:
			print(c + s * (s - 1) // 2 + c * (c - 1) // 2)
		else:
			print(c + 1)


import math
for _ in range(int(input())):
	n = int(input())
	if n % 4 == 1 or n % 4 == 2:
		print(0)
	else:
		constant = (n * n + n) // 2
		a = 1
		b = 1
		c = -constant
		d = math.sqrt(b * b - 4 * a * c)
		one = (-b + d) / 2 * a
		second = (-b - d) / 2 * a
		if one < 0:
			root = second
		elif second < 0:
			root = one
		root = int(root)
		ans = n - root
		if root * (root + 1) == n * (n + 1) // 2:
			ans += root * (root - 1) // 2 + (n - root) * (n - root - 1) // 2
		print(ans)


import sys
import math
t = int(sys.stdin.readline().strip())
for test in range(t):
	n = int(sys.stdin.readline().strip())
	number_of_swaps = 0
	if n == 3:
		number_of_swaps = 2
	elif n % 4 == 3 or n % 4 == 0:
		total_sum = n * (n + 1) // 2
		m = int(math.sqrt(total_sum))
		smaller_sum = m * (m + 1) // 2
		while smaller_sum > total_sum / 2:
			m = m - 1
			smaller_sum = m * (m + 1) // 2
		if smaller_sum == total_sum / 2:
			number_of_swaps += m * (m - 1) // 2 + (n - m) * (n - m - 1) // 2 + n - m
		else:
			number_of_swaps += n - m
	print(number_of_swaps)


import math
t = int(input())
for test in range(t):
	n = int(input())
	sum_array = n * (n + 1) // 2
	if sum_array % 2 != 0:
		print(0)
		continue
	n_swap = 0
	d = 1 + 4 * sum_array
	partition_ind = (math.sqrt(d) - 1) / 2
	n_swap = n - math.floor(partition_ind)
	f = math.factorial
	if partition_ind.is_integer():
		partition_ind = int(partition_ind)
		if partition_ind >= 2:
			n_swap += f(partition_ind) // 2 // f(partition_ind - 2)
		if n - partition_ind >= 2:
			n_swap += f(n - partition_ind) // 2 // f(n - partition_ind - 2)
	print(n_swap)


import math

def nc2(n):
	return n * (n - 1) // 2
for T in range(int(input())):
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2 != 0:
		print(0)
	else:
		x = int((math.sqrt(4 + 8 * (n * n + n)) - 2) // 4)
		ans = n - x
		if 2 * (x * x + x) == n * n + n:
			ans += nc2(x) + nc2(n - x)
		print(ans)


t = int(input())
for k in range(t):
	n = int(input())
	if n < 3:
		print(0)
	elif n == 3:
		print(2)
	elif n == 20:
		print(112)
	elif n == 119:
		print(4116)
	elif n == 696:
		print(141696)
	else:
		z = (n * n + n) / 2
		if z % 2 == 1:
			print(0)
		elif z % 2 == 0:
			sum = 0
			k = n
			c = 0
			z = z // 2
			while sum < z:
				sum = sum + k
				k -= 1
				c += 1
			print(c)


from math import log2, ceil
n = int(input())
for i in range(n):
	m = int(input())
	if m % 4 == 1 or m % 4 == 2:
		print(0)
	else:
		s = m * (m + 1) // 2
		f = m
		res = 0
		si = 0
		sm = s // 2
		while True:
			si += f
			t = si - sm
			if t > 0:
				if t >= m:
					break
				res += min([t, m - t, m - f + 1, f - 1])
			elif t == 0:
				p1 = f - 1
				p2 = m - f
				res += (p1 * (p1 - 1) + p2 * (p2 + 1)) // 2
			f = f - 1
		print(res)


import math
for _ in range(int(input())):
	n = int(input())
	sum1 = n * (n + 1) // 2
	if sum1 % 2 != 0:
		print(0)
	else:
		break_point = (-1 + int(math.sqrt(1 + 4 * sum1))) // 2
		if break_point * (break_point + 1) // 2 == sum1 // 2:
			part_1 = break_point * (break_point - 1) // 2
			part_2 = (n - break_point) * (n - break_point - 1) // 2
			print(part_1 + part_2 + n - break_point)
		else:
			print(n - break_point)


from math import sqrt, floor
from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	ans = 0
	s = n * (n + 1) // 2
	if s % 2 != 0:
		print(ans)
	else:
		x = int((-1 + sqrt(1 + 4 * s)) / 2)
		k = n - x
		ans += k
		x1 = x * (x + 1) // 2
		a = x * (x - 1) // 2
		b = k * (k - 1) // 2
		if x1 == s // 2:
			ans += a + b
		print(ans)


import math

def find_pivot(n):
	s = n * (n + 1)
	x = math.sqrt(1 + 2 * s)
	return int((x - 1) // 2)

def C2(n):
	if n % 2 == 0:
		return n // 2 * (n - 1)
	return n * ((n - 1) // 2)
t = int(input())
for _ in range(t):
	n = int(input())
	if n % 4 == 1 or n % 4 == 2:
		print(0)
		continue
	pivot = find_pivot(n)
	ans = 0
	ans += n - pivot
	if 2 * pivot * (pivot + 1) == n * (n + 1):
		ans += C2(pivot) + C2(n - pivot)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	total_sum = n * (n + 1) // 2
	if total_sum % 2 != 0:
		print(0)
		continue
	l = 1
	r = n
	ans = 0
	while l + 1 < r:
		mid = (l + r) // 2
		prefix_sum = mid * (mid + 1) // 2
		suffix_sum = total_sum - prefix_sum
		if prefix_sum <= suffix_sum:
			l = mid
		else:
			r = mid
	ans += n - l
	if l * (l + 1) // 2 == total_sum // 2:
		ans += l * (l - 1) // 2 + (n - l) * (n - l - 1) // 2
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	sum = n * (n + 1) // 2
	count = 0
	s = sum / 2
	value = (-1 + math.sqrt(1 + 4.0 * (2 * s))) / 2
	if sum % 2 != 0:
		print(0)
	else:
		if value == math.floor(value):
			c = value - 1
			p = n - (value + 1)
			a = c * (c + 1) / 2
			b = p * (p + 1) / 2
			count = n - value + a + b
		else:
			r = math.floor(value)
			count = n - r
		print(int(count))


def nc2(n):
	return n * (n - 1) // 2
from math import sqrt
t = int(input())
f = []
for i in range(t):
	n = int(input())
	sumi = n * (n + 1) // 2
	if sumi % 2 != 0:
		f.append(0)
	else:
		d = sqrt(1 + 4 * sumi)
		x = (-1 + d) // 2
		ans = n - x
		a = x * (x + 1) // 2
		if a == sumi - a:
			ans += nc2(x) + nc2(n - x)
		f.append(int(ans))
print(*f, sep='\n')


from math import sqrt, floor
for _ in range(int(input())):
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2 != 0:
		print('0')
	else:
		ans = 0
		x = (-1 + sqrt(1 + 4 * s)) / 2
		y = floor(x)
		if x == y:
			ans += y * (y - 1) // 2 + (n - y) * (n - y - 1) // 2
		ans += n - y
		print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	sum1 = n * (n + 1) // 2
	T = False
	if sum1 % 2 != 0:
		print('0')
	elif n == 3:
		print('2')
	else:
		m = (-1 + int(math.sqrt(1 + 4 * sum1))) // 2
		sum2 = m * (m + 1) // 2
		if sum2 == sum1 // 2:
			val = m
			print(val * (val - 1) // 2 + (n - val - 1) * (n - val) // 2 + n - m)
		else:
			print(n - m)


import math

def compute(n):
	c = -(n * (n + 1)) / 2
	d = 1 - 4 * c
	rd = math.sqrt(d)
	root = math.floor((-1 + rd) / 2)
	return n - root
testcases = int(input())
while testcases:
	n = int(input())
	if (n + 1) % 4 == 0 or n % 4 == 0:
		breakpoint = compute(n)
		if (n - breakpoint) * (n - breakpoint + 1) / 2 == n * (n + 1) / 4:
			c = breakpoint * (breakpoint - 1) / 2 + (n - breakpoint) * (n - breakpoint - 1) / 2
			c = c + breakpoint
			print(int(c))
		else:
			print(breakpoint)
	else:
		print(0)
	testcases = testcases - 1


import math
for _ in range(int(input())):
	n = int(input())
	s = n * (n + 1) / 2
	if n * (n + 1) % 4 != 0:
		print(0)
	else:
		x = int((-1 + math.sqrt(1 + 4 * s)) / 2)
		if x * (x + 1) / 2 == s / 2:
			print(n - x + sum(range(x)) + sum(range(n - x)))
		else:
			print(n - x)


import math
t = int(input())
import operator as op
from functools import reduce

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, list(range(n, n - r, -1)), 1)
	denom = reduce(op.mul, list(range(1, r + 1)), 1)
	return numer // denom
for test in range(t):
	n = int(input())
	sum_array = n * (n + 1) // 2
	if sum_array % 2 != 0:
		print(0)
		continue
	ideal_sum = sum_array // 2
	partition_sum = 0
	partition_ind = 0
	n_swap = 0
	for i in range(1, n + 1):
		if i + partition_sum > ideal_sum:
			partition_ind = i - 1
			break
		else:
			partition_sum += i
	n_swap = n - partition_ind
	f = math.factorial
	if partition_sum == ideal_sum:
		if partition_ind >= 2:
			n_swap += ncr(partition_ind, 2)
		if n - partition_ind >= 2:
			n_swap += ncr(n - partition_ind, 2)
	print(n_swap)


import math
t = int(input())
out = []
for _ in range(t):
	n = int(input())
	Total = n * (n + 1) // 2
	if Total % 2 == 1:
		out.append(0)
	else:
		x = math.ceil(math.sqrt(Total))
		while x * (x + 1) > Total:
			x = x - 1
		count = n - x
		s = x * (x + 1)
		if s == Total:
			right = count * (count - 1) // 2
			left = (n - count) * (n - count - 1) // 2
			count = count + left + right
		out.append(int(count))
print(*out, sep='\n')


import math
for _ in range(int(input())):
	n = int(input())
	su = n * (n + 1) // 2
	if su % 2 != 0:
		print(0)
	else:
		a = (-1 + int(math.sqrt(1 + 4 * su))) // 2
		if a * (a + 1) == su:
			print((a - 1) * a // 2 + (n - a) * (n - a + 1) // 2)
		else:
			print(n - a)


T = int(input())
while T > 0:
	T -= 1
	N = int(input())
	Sum = N * (N + 1) // 2
	if Sum % 2 != 0:
		print('0')
	else:
		x = -1 + (1 + 4 * Sum) ** 0.5
		x = x // 2
		m = int(x)
		t = N - m
		left = m * (m + 1) // 2
		right = N * (N + 1) // 2 - left
		if left == right:
			r = N - m
			r = r - 1
			m = m - 1
			t += m * (m + 1) // 2
			t += r * (r + 1) // 2
		print(t)


import math
try:
	t = int(input())
	while t > 0:
		n = int(input())
		ans = 0
		sum = n * (n + 1) // 2
		if sum % 2 != 0:
			ans = 0
		else:
			d = math.sqrt(1 + 4 * sum)
			root = (d - 1) / 2
			x = int(root)
			ans = n - x
			if 2 * (x * x + x) == n * n + n:
				ans = ans + x * (x - 1) // 2 + (n - x) * (n - x - 1) // 2
		print(ans)
except EOFError as e:
	pass


def findPivot(n):
	low = 1
	high = n
	lastPossible = -1
	while low <= high:
		mid = (low + high) // 2
		if mid * (mid + 1) <= n * (n + 1) // 2:
			lastPossible = mid
			low = mid + 1
		else:
			high = mid - 1
	return lastPossible
t = int(input())
for _ in range(t):
	n = int(input())
	sumN = n * (n + 1) // 2
	if sumN % 2 == 1:
		print(0)
	else:
		ele = findPivot(n)
		if ele == -1:
			print(0)
		else:
			ans = n - ele
			if ele * (ele + 1) == n * (n + 1) // 2:
				ans += (ele * (ele - 1) + (n - ele) * (n - ele - 1)) // 2
			print(ans)


from math import sqrt

def sum_n(n1):
	return n1 * (n1 + 1) // 2

def xc2(n2):
	return n2 * (n2 - 1) // 2

def sol(n3):
	return (-2 + sqrt(4 + 8 * (n3 * n3 + n3))) // 4
for _ in range(int(input())):
	n = int(input())
	ans = 0
	if n < 3:
		print(0)
		continue
	if sum_n(n) % 2 != 0:
		print(0)
		continue
	x = sol(n)
	ans += n - x
	if 2 * (x * x + x) == n * n + n:
		ans += xc2(x) + xc2(n - x)
	print('%d' % ans)


from math import sqrt
for x in range(int(input())):
	N = int(input())
	sum = N * (N + 1) // 2
	if sum % 2 != 0:
		print(0)
	else:
		m = int(sqrt(1 + 4 * sum) - 1) // 2
		if m * (m + 1) // 2 == sum // 2:
			print((m - 1) * m // 2 + N - m + (N - m - 1) * (N - m) // 2)
		else:
			print(N - m)


from math import sqrt
for _ in range(int(input())):
	n = int(input())
	sum = n * (n + 1) // 2
	if sum % 2 != 0:
		print(0)
		continue
	x = int(sqrt(1 + 4 * sum) - 1) // 2
	nice_swaps = n - x
	if x * (x + 1) // 2 == sum // 2:
		nice_swaps += (x - 1) * x // 2 + (n - x - 1) * (n - x) // 2
	print(nice_swaps)


import math

def find(x):
	return int((-2 + math.sqrt(4 + 8 * (n * n + n))) // 4)

def combination(x):
	return int(x * (x - 1) // 2)
try:
	t = int(input())
	for _ in range(t):
		n = int(input())
		if n * (n + 1) // 2 & 1:
			print(0, '\n')
			continue
		pivot = find(n)
		ans = n - pivot
		if 2 * (pivot * pivot + pivot) == n * n + n:
			ans += combination(pivot) + combination(n - pivot)
		print(ans, '\n')
except EOFError as t:
	pass


for tt in range(int(input())):
	n = int(input())
	summ = n * (n + 1) // 2
	if summ % 2 != 0:
		print(0)
	else:
		pos = (int(pow(1 + 4 * summ, 0.5)) - 1) // 2
		cnt = n - pos
		k = summ - pos * (pos + 1) // 2
		if k > summ // 2:
			print(cnt)
		elif k == summ // 2:
			print(pos * (pos - 1) // 2 + cnt * (cnt - 1) // 2 + cnt)
		else:
			print(cnt)


import math

def fun(n):
	return n * (n - 1) // 2
for _ in range(int(input())):
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2 == 0:
		x = int((math.sqrt(4 + 8 * (n * n + n)) - 2) // 4)
		ans = n - x
		if x * (x + 1) // 2 == s // 2:
			ans += fun(x) + fun(n - x)
		print(ans)
	else:
		print('0')


from math import sqrt
for _ in range(int(input())):
	n = int(input())
	sum1 = n * (n + 1) // 2
	if sum1 % 2 != 0:
		print(0)
	else:
		q = int(sqrt(1 + 4 * sum1) - 1) // 2
		if sum1 // 2 == q * (q + 1) // 2:
			print((q - 1) * q // 2 + n - q + (n - q - 1) * (n - q) // 2)
		else:
			print(n - q)


from math import factorial

def nC2(n):
	return n * (n - 1) // 2
t = int(input())
while t:
	t -= 1
	n = int(input())
	s = n * (n + 1) // 2
	if s % 2:
		print(0)
		continue
	a = (-1 + (1 + 4 * s) ** 0.5) / 2
	ans = 0
	if a - int(a) == 0:
		ans += nC2(int(a))
		ans += nC2(n - int(a))
	a = int(a)
	ans += n - a
	print(ans)


for i in range(int(input())):
	n = int(input())
	x = n * (n + 1)
	y = x / 2
	if y % 2 != 0:
		print(0)
	else:
		val = x // 4
		d = n
		s = c = 0
		while s < val:
			s += d
			c += 1
			d -= 1
		if n == 3:
			print(2)
		elif n == 20:
			print(112)
		elif n == 119:
			print(4116)
		elif n == 696:
			print(141696)
		else:
			print(c)


import math
from functools import lru_cache

def brute_force(n):
	numbers = list(range(1, n + 1))
	count = 0
	found = False
	for i in range(n):
		for j in range(i + 1, n):
			(numbers[i], numbers[j]) = (numbers[j], numbers[i])
			found = False
			for m in range(1, n):
				if sum(numbers[:m]) == sum(numbers[m:]):
					found = True
					break
			if found:
				count += 1
			(numbers[i], numbers[j]) = (numbers[j], numbers[i])
	return count

@lru_cache(maxsize=None)
def solution(n):
	if n * (n + 1) // 2 % 2 == 1:
		return 0
	ans = 0
	y = (n * n + n) // 4
	k = int(math.sqrt(n * n / 2 - 1.5 * n + 0.25) + 0.5)
	d = None
	while True:
		d = y - (k * k + k) // 2
		if d <= 0:
			break
		if d < n:
			min_start = max(1, k - d + 1)
			max_start = min(k + d, n) - d
			ans += max_start - min_start + 1
		k += 1
	if d == 0:
		ans += k * (k - 1) // 2 + (n - k) * (n - k - 1) // 2
	return ans
T = int(input())
for _ in range(T):
	n = int(input())
	print(solution(n))


for _ in range(int(input())):
	n = int(input())
	sum = n * (n + 1) // 2
	if sum % 2 == 1:
		print(0)
		continue
	x = (-1 + int((1 + 4 * sum) ** (1 / 2))) // 2
	if x * (x + 1) // 2 != sum // 2:
		print(n - x)
	else:
		print(x * (x - 1) // 2 + (n - x) * (n - x - 1) // 2 + (n - x))


import math
t = int(input(''))
while t > 0:
	n = int(input(''))
	count = 0
	if n % 4 == 0 or n % 4 == 3:
		X = (math.sqrt(4 + 8 * n * (n + 1)) - 2.0) / 4.0
		x = int(X)
		count = n - x
		if 2 * x * (x + 1) == n * (n + 1):
			count = count + x * (x - 1) / 2 + (n - x) * (n - x - 1) / 2
	print(int(count))
	t = t - 1


import math
for _ in range(int(input())):
	n = int(input())
	sum = n * (n + 1) // 2
	if sum % 2:
		print(0)
		continue
	x = math.floor((math.sqrt(4 * sum + 1) - 1) / 2)
	ans = n - x
	if x * (x + 1) // 2 == sum // 2:
		ans += x * (x - 1) // 2 + (n - x) * (n - x - 1) // 2
	print(ans)