#DSU0000062. Trò chơi trên trục số (Game on Axis)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trò chơi trên trục số (Game on Axis)
Game on Axis
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A game takes place on a number line with positions . Position stores an integer . The player always starts at position .
When the player is at a position inside , they must move to position . If the new position lies outside , the game ends immediately. Otherwise the player continues under the same rule. A game may run forever if the player repeats a cycle of positions.
Before starting, you must choose exactly one pair satisfying and and replace with . Choosing is allowed and still counts as a pair. Count the pairs for which the modified game ends after finitely many moves.
Input
The first line contains ; the second contains with .
Output
Print the number of valid pairs .
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
2
1 -1
Output
6
Explanation
Initially, play alternates between positions and , so it never terminates. If is changed, choosing makes play leave the interval immediately. If is changed, choosing makes it leave after reaching position . There are valid pairs .