102552: [AtCoder]ABC255 C - ±1 Operation 1

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $300$ points

Problem Statement

You are given an integer $X$. The following action on this integer is called an operation.

  • Choose and do one of the following.
    • Add $1$ to $X$.
    • Subtract $1$ from $X$.

The terms in the arithmetic progression $S$ with $N$ terms whose initial term is $A$ and whose common difference is $D$ are called good numbers.
Consider performing zero or more operations to make $X$ a good number. Find the minimum number of operations required to do so.

Constraints

  • All values in input are integers.
  • $-10^{18} \le X,A \le 10^{18}$
  • $-10^6 \le D \le 10^6$
  • $1 \le N \le 10^{12}$

Input

Input is given from Standard Input in the following format:

$X$ $A$ $D$ $N$

Output

Print the answer as an integer.


Sample Input 1

6 2 3 3

Sample Output 1

1

Since $A=2,D=3,N=3$, we have $S=(2,5,8)$.
You can subtract $1$ from $X$ once to make $X=6$ a good number.
It is impossible to make $X$ good in zero operations.


Sample Input 2

0 0 0 1

Sample Output 2

0

We might have $D=0$. Additionally, no operation might be required.


Sample Input 3

998244353 -10 -20 30

Sample Output 3

998244363

Sample Input 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

Sample Output 4

444445

Input

题意翻译

【题目翻译】 输入 $4$ 个数 $x, a, d, n$,求在以首项为 $a$,公差为 $d$,项数为 $n$ 的等差数列中,$x$ 与离 $x$ 最近的某一项之差。 translated by @[Lhy114514](https://www.luogu.com.cn/user/749988)。

Output

分数:300分

问题描述

给你一个整数 $X$。对该整数进行以下操作称为一个 操作

  • 从以下选项中选择并执行其中一个。
    • 将 $X$ 加 $1$。
    • 将 $X$ 减 $1$。

初始项为 $A$、公差为 $D$ 的具有 $N$ 项的等差数列 $S$ 中的项被称为 好数字。考虑对 $X$ 执行零个或多个操作使其成为好数字。求执行最少的操作数。

限制条件

  • 输入中的所有值都是整数。
  • $-10^{18} \le X,A \le 10^{18}$
  • $-10^6 \le D \le 10^6$
  • $1 \le N \le 10^{12}$

输入

输入通过标准输入给出,格式如下:

$X$ $A$ $D$ $N$

输出

打印一个整数作为答案。


样例输入 1

6 2 3 3

样例输出 1

1

因为 $A=2,D=3,N=3$,所以 $S=(2,5,8)$。
你可以将 $X$ 减 $1$ 一次,使 $X=6$ 成为一个好数字。
在零个操作中使 $X$ 成为好数字是不可能的。


样例输入 2

0 0 0 1

样例输出 2

0

我们可能有 $D=0$。此外,可能不需要任何操作。


样例输入 3

998244353 -10 -20 30

样例输出 3

998244363

样例输入 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

样例输出 4

444445

加入题单

算法标签: