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 Common Lisp (SBCL 1.1.14) (judge verdict: Runtime Error)

;; F - Sum Difference

(defun main ()
  (let ((N (read))
        (X (read))
        (D (read)))
    (print (solve N X D))
    (fresh-line)))

;; 整数列の総和 total = NX + N(N - 1)D/2
;; 高橋君が取った数の個数 m
;; 高橋君が取った数の和 S
;;   Sの最小値: m X + m(m - 1)D/2      (mが1増えると X + m D 増加)
;;   Sの最大値: m X + m(2N - m - 1)D/2 (mが1増えると X + (N - m - 1)D 増加)
;;   Sの刻み: D
;; S - T == S - (total - S) == 2S - total (SとS - Tは一対一)

(defun solve (N X D)
  (cond
    ; X == D == 0ならS == 0.  X != 0, D == 0ならS == m X.
    ((zerop D) (if (zerop X) 1 N))
    ((minusp D) (solve-body N (+ X (* (1- N) D)) (- D))) ; Dを正に
    (t (solve-body N X D))))

(defun solve-body (N X D)
  (let ((m-cycle (if (zerop X) 1 (/ (lcm X D) (abs X)))))
    ; m: 高橋君が取った数の個数. m = m-base + i m-cycle
    ; m-cycle: m X mod D == 0 となる最小の正整数 m
    ; m-base == m mod m-cycle. m-baseが異なるSは重ならない.
    (loop for m-base from 0 below m-cycle sum
      ; Sの個数
      (reduce #'+
        (mapcar #'(lambda (range) (1+ (/ (- (cdr range) (car range)) D)))
          ; Sが取り得る重複のない区間のリスト
          (clean-range-list
            (S-ranges N X D m-base m-cycle)))))))

(defun S-ranges (N X D m-base m-cycle)
  "各m = m-base + i m-cycleに対してSが取り得る区間のリストを返す"
  (loop for m from m-base to N by m-cycle
        for low  = (+ (* m X) (ash (* m (1- m) D) -1))
        for high = (+ (* m X) (ash (* m (- (+ N N) m 1) D) -1))
        collect (cons low high)))

(defun clean-range-list (ranges)
  "重複のない区間のリストを返す"
  (setf ranges (sort ranges #'< :key #'car)) ; 始点の昇順
  (labels
    ((cleaner (head ranges result)
       (cond ((null ranges) (cons head result))
             ((<= (cdar ranges) (cdr head)) ; 包含
              (cleaner head (cdr ranges) result))
             ((<= (caar ranges) (cdr head)) ; 重なり
              (cleaner (cons (car head) (cdar ranges)) (cdr ranges) result))
             (t
              (cleaner (car ranges) (cdr ranges) (cons head result))))))
    (cleaner (car ranges) (cdr ranges) nil)))

(main)