I am working on solving this problem: https://open.kattis.com/problems/tractor
Bessie the Cow has stolen Farmer John’s tractor and is running wild on the coordinate plane! She, however, is a terrible driver, and can only move according to the following rules:
Each of her movements is in the same direction as either the positive x-axis or the positive y-axis. Her nth movement takes her $2^{n−1}$ units forward in her chosen direction. (On her first movement, n=1, so she moves 1 unit.) Farmer John’s farm is on the coordinate plane, in the shape of a rectangle with corners at (0,0), (A,0), (0,B) and (A,B). If Bessie starts at (0,0), how many points inside the farm, including the boundary, could she reach?
I believe I have reduced the problem to the following:
Consider a set $X$ such that $X =\{ (a,b) \in \mathbb{N}^2 | \exists n \in \mathbb{N} (a+b = 2^n-1) \} $. The points in this set fall on the line $y = -x + 2^n $ for all $n \in \mathbb{N}$ (and also $(0,0)$).
Given $(A,B)$, return $|\{ (a,b) \in X | (a \le A \land b \le B) \}|$.
I feel like this can be done quite efficiently by calculating the value of an elementary function, but I am drawing a blank here. My initial approach involved summing along the larger of $A$ and $B$, but I couldn't quite get it right--and it'd be much too slow for maximum inputs: $1 < A, B < 10^8$.
Any insights or hints would be much appreciated.