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)$

The answer is $\color{blue}{d^2/(12ab)}$ where $\color{blue}{d=\gcd(a,b)}$. A reasonably short solution is based on $$\newcommand{\saw}[1]{\left(\!\left(#1\right)\!\right)}\saw{x}:=\begin{cases}\{x\}-1/2,&x\notin\mathbb{Z}\\\hfill 0,\hfill&x\in\mathbb{Z}\end{cases}=-\sum_{n=1}^\infty\frac{\sin 2n\pi x}{n\pi}$$ in $L^2(0,1)$; the same holds if $x$ is replaced by $ax$ or $bx$, hence Parseval's theorem yields $$\int_0^1\saw{ax}\saw{bx}dx=\frac1{2\pi^2}\sum_{\substack{m,n>0\\am=bn}}\frac1{mn}=\frac1{2\pi^2}\sum_{k=1}^\infty\frac1{(bk/d)(ak/d)}=\frac{d^2}{12ab}$$ ($am=bn$ holds if and only if $m=bk/d$ and $n=ak/d$ for some $k\in\mathbb{Z}^+$).
There's also an elementary one, using $\saw{nx}=\sum_{k=0}^{n-1}\saw{x+k/n}$ for $n\in\mathbb{Z}^+$, so that $$\int_0^1\saw{ax}\saw{bx}dx=\sum_{m=0}^{a-1}\sum_{n=0}^{b-1}f(m/a,n/b),\\f(u,v):=\int_0^1\saw{x+u}\saw{x+v}dx=f(u-v,0),\\f(t,0)=\frac1{12}-\frac{t(1-t)}2\quad\text{for}\quad 0\leqslant t\leqslant 1.$$
