#abc255c. C - ±1 Operation 1

C - ±1 Operation 1

Score : 300300 points

问题描述

给定一个整数 XX。以下对该整数的操作被称为 操作

  • 选择并执行以下任一操作:
    • 11 加到 XX 上。
    • XX 中减去 11

在等差数列 SS 中,具有 NN 项且首项为 AA、公差为 DD 的各项被称为 好数

考虑通过执行零次或多次操作,使 XX 变成一个好数。找出完成这一目标所需的最少操作次数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

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

  • Choose and do one of the following.
    • Add 11 to XX.
    • Subtract 11 from XX.

The terms in the arithmetic progression SS with NN terms whose initial term is AA and whose common difference is DD are called good numbers. Consider performing zero or more operations to make XX a good number. Find the minimum number of operations required to do so.

Constraints

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

Input

Input is given from Standard Input in the following format:

XX AA DD NN

Output

Print the answer as an integer.

Sample Input 1

6 2 3 3

Sample Output 1

1

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

Sample Input 2

0 0 0 1

Sample Output 2

0

We might have D=0D=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

update @ 2024/3/10 10:51:25