A frog is initially at position $0$ on the number line. The frog has two positive integers $a$ and $b$. From a position $k$, it can either jump to position $k+a$ or $k-b$.
Let $f(x)$ be the number of distinct integers the frog can reach if it never jumps on an integer outside the interval $[0, x]$. The frog doesn’t need to visit all these integers in one trip, that is, an integer is counted if the frog can somehow reach it if it starts from $0$.
Given an integer $m$, find $\sum_{i=0}^{m} f(i)$. That is, find the sum of all $f(i)$ for $i$ from $0$ to $m$.
Input
The first line contains three integers $m, a, b$ ($1 \leq m \leq 10^9, 1 \leq a,b \leq 10^5$).
Output
Print a single integer, the desired sum.
Examples
input
7 5 3
output
19
input
1000000000 1 2019
output
500000001500000001
input
100 100000 1
output
101
input
6 4 5
output
10
Note
In the first example, we must find $f(0)+f(1)+\ldots+f(7)$. We have $f(0) = 1, f(1) = 1, f(2) = 1, f(3) = 1, f(4) = 1, f(5) = 3, f(6) = 3, f(7) = 8$. The sum of these values is $19$.
In the second example, we have $f(i) = i+1$, so we want to find $\sum_{i=0}^{10^9} i+1$.
In the third example, the frog can’t make any jumps in any case.
Solution:
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int m, a, b; cin >> m >> a >> b; vector<int> first(a + b, -1); int x = 0; int y = 0; while (true) { first[x] = y; if (x >= b) { x -= b; } else { x += a; } if (x == 0) { break; } y = max(y, x); } long long ans = 0; for (int i = 0; i < a + b; i++) { if (first[i] != -1) { ans += max(0, m - first[i] + 1); } } if (m >= a + b) { int left = m - (a + b); int g = __gcd(a, b); int u = left / g * g; long long first = left - 0 + 1; long long last = left - u + 1; long long cnt = (first - last) / g + 1; ans += (first + last) * cnt / 2; } cout << ans << '\n'; return 0; }