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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n, x, d := scanInt(), scanInt(), scanInt()

	if d == 0 {
		if x == 0 {
			fmt.Fprintln(wr, 1)
		} else {
			fmt.Fprintln(wr, n+1)
		}
		return
	}

	a := map[int]ps{}
	tt := n * (n - 1) / 2

	for i := 0; i <= n; i++ {
		p := i * x % d
		l := i*x/d + i*(i-1)/2
		r := i*x/d + tt - (n-i)*(n-1-i)/2
		a[p] = append(a[p], pii{l, r})
	}

	ans := 0

	for i := range a {
		sort.Sort(a[i])
		l, r := a[i][0].a, a[i][0].b
		for j := 0; j < len(a[i]); j++ {
			if a[i][j].a <= r {
				r = max(r, a[i][j].b)
			} else {
				ans += r - l + 1
				l, r = a[i][j].a, a[i][j].b
			}
		}
		ans += r - l + 1
	}

	fmt.Fprintln(wr, ans)
}

type pii struct{ a, b int }

type ps []pii

func (a ps) Len() int           { return len(a) }
func (a ps) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ps) Less(i, j int) bool { return a[i].a < a[j].a }