Problem:
Score : 600 points

Problem Statement

We have an integer sequence A of length N, where A_1 = X, A_{i+1} = A_i + D (1 \leq i < N ) holds.

Takahashi will take some (possibly all or none) of the elements in this sequence, and Aoki will take all of the others.

Let S and T be the sum of the numbers taken by Takahashi and Aoki, respectively. How many possible values of S - T are there?

Constraints

-10^8 \leq X, D \leq 10^8

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X D

Output

Print the number of possible values of S - T.

Sample Input 1

3 4 2

Sample Output 1

8

A is (4, 6, 8).

There are eight ways for (Takahashi, Aoki) to take the elements: ((), (4, 6, 8)), ((4), (6, 8)), ((6), (4, 8)), ((8), (4, 6))), ((4, 6), (8))), ((4, 8), (6))), ((6, 8), (4))), and ((4, 6, 8), ()).

The values of S - T in these ways are -18, -10, -6, -2, 2, 6, 10, and 18, respectively, so there are eight possible values of S - T.

Sample Input 2

2 3 -3

Sample Output 2

2

A is (3, 0). There are two possible values of S - T: -3 and 3.

Sample Input 3

100 14 20

Sample Output 3

49805

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;

class Main {
	Scanner sc;
	long N, X, D;
	Map<Integer, List<long[]>> f;
	Comparator<long[]> c;
	{
		c = Comparator.comparingLong(a -> a[0]);
		c = c.thenComparingLong(a -> a[1]);
	}
	
	private void calc() {
		sc = new Scanner(System.in);
		N = sc.nextInt();
		X = sc.nextLong();
		D = sc.nextLong();
		
		if (D == 0) {
			if (X == 0) System.out.println(0);
			else System.out.println(N+1);
			return;
		}
		if (D < 0) { X = -X; D = -D; }
		
		f = new HashMap<>();
		for (int k = 0; k <= N; k++) {
			long s = X*k+D*(k-1)*k/2;
			long e = X*k+D*(2L*N-k-1)*k/2;
			int m = (int)((k*X)%D);
			List<long[]> segs = f.get(m);
			if (segs == null) segs = new ArrayList<>();
			segs.add(new long[] {s, e});
			f.put(m, segs);
		}
		long cnt = 0;
		for (List<long[]> segs : f.values()) {
			segs.sort(c);
			long doneE = Long.MIN_VALUE;
			for (long[] seg : segs) {
				long s = Math.max(doneE+D, seg[0]);
				long e = seg[1];
				if (s > e) continue;
				cnt += (e-s)/D+1;
				doneE = e;
			}
		}
		System.out.println(cnt);
	}
	
	public static void main(String[] args) {
		new Main().calc();
	}
}