Official

A - B = C Editorial by evima


First, for an integer \(C\), how many pairs \(A, B\) of integers between \(L\) and \(R\) (inclusive) satisfy \(A-B=C\)? If \(C\gt R-L\), there is no such pair; otherwise, there are \(R-C-L+1\) pairs: \((B,A) = (L,L+C), \dots, (R-C,R)\).

Thus, we can find the answer by summing this over \(C = L, L+1, \dots, R\). This can be done by first finding the range of numbers such that \(C\le R-L\) and then using the formula to find the sum of an arithmetic progression, in \(O(1)\) time.

Sample Implementation in C++

Sample Implementation in Python

posted:
last update: