Evaluate $\int_0^1 \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$ for $a,b\in\mathbb{Z}^+$ Evaluate $$\int_0^1 \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$$
where $a$ and $b$ are positive integers and $\{x\}:=x-\lfloor x\rfloor$
WLOG, we may assume that $a\le b$. Via the substitution $x \mapsto x/a$, we obtain
$$\int_0^a \left(\{x\}-\frac{1}{2}\right)\left(\left\{\frac{bx}{a}\right\}-\frac{1}{2}\right) dx$$
I know that we should partition the interval $(0,1)$ in such a way that the values of $\{x\}$ and $\{bx/a\}$ are constant in each subinterval. How do I think of such a partition? I also tried to express $a=gu$ and $b=gv$ where $u:=\gcd(a,b)$. But then again I face the same issue of partitioning $(0,1)$

This is a brute force approach:
$$\int_0^1 \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$$
Break the interval up into $ab$ parts to avoid the switch:
$$\sum_{k=1}^{ab}\int_{(k-1)/(ab)}^{k/(ab)} \left(\{ax\}-\frac{1}{2}\right)\left(\{bx\}-\frac{1}{2}\right) dx$$
Now $\{ax\}$ is just a straight line segment with a slope of $a$ starting at a height of $(k \bmod b)/b$.
$$\sum_{k=1}^{ab}\int_{0}^{1/(ab)} \left( \frac{k \bmod b}{b} + xa -\frac{1}{2}\right)\left(\frac{k \bmod a}{a} + xb -\frac{1}{2}\right) dx$$
$$\frac{1}{12a^2b^2} \sum_{k=1}^{ab} \left(\begin{array} {rl} 
     &(k \bmod a)(k \bmod b)\cdot 12
\\ + &(k \bmod a)(6 - 6b)
\\ + &(k \bmod b)(6 - 6a)
\\ + &3ab-3b-3a+4
\end{array}\right)$$
And now just a number theory problem.  Using $d=\gcd(a,b)$
$$\sum_{k=1}^{ab} (k \bmod a)(k \bmod b)
 = d\sum_{0 \le p < a \\ 0 \le q < b \\ p \equiv q \pmod d } pq
 = \frac{ab(d^2+3ab-3b-3a+2)}{12}
$$
$$
 \sum_{k=1}^{ab} (k \bmod a)
= b\sum_{k=0}^{a-1} k
= \frac{ab(a-1)}{2}
$$
So altogether:
$$\frac{1}{12a^2b^2} \left(\begin{array} {l} 
     \frac{ab(d^2+3ab-3b-3a+2)}{12}\cdot 12
\\ + \frac{ab(a-1)}{2}(6 - 6b)
\\ + \frac{ab(b-1)}{2}(6 - 6a)
\\ + ab(3ab-3b-3a+4)
\end{array}\right)$$
Which is:
$$\frac{d^2}{12ab}$$
