CF BUDDY
← Problems·

1146D · Frog Jumping

2100 · dfs and similar, math, number theory

Problem: A frog is initially at position 00 on the number line. The frog has two positive integers aa and bb. From a position kk, it can either jump to position k+ak+a or kbk-b.

Let f(x)f(x) be the number of distinct integers the frog can reach if it never jumps on an integer outside the interval [0,x][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 00.

Given an integer mm, find i=0mf(i)\sum_{i=0}^{m} f(i). That is, find the sum of all f(i)f(i) for ii from 00 to mm.

Input Format: The first line contains three integers m,a,bm, a, b (1m109,1a,b1051 \leq m \leq 10^9, 1 \leq a,b \leq 10^5).

Output Format: Print a single integer, the desired sum.

Note: In the first example, we must find f(0)+f(1)++f(7)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)=8f(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 1919.

In the second example, we have f(i)=i+1f(i) = i+1, so we want to find i=0109i+1\sum_{i=0}^{10^9} i+1.

In the third example, the frog can't make any jumps in any case.

Sample Cases

Case 1

Input

7 5 3

Output

19

Case 2

Input

1000000000 1 2019

Output

500000001500000001

Case 3

Input

100 100000 1

Output

101

Case 4

Input

6 4 5

Output

10

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback