P2693 [USACO1.3] Combination Lock
Background
Thanks to @[Fond_Dream](https://www.luogu.com.cn/user/321680) for providing the standard statements for five USACO problems.
Description
Farmer John’s cows keep escaping from his farm, causing a lot of damage. To prevent them from escaping again, he buys a large combination lock to secure the pasture gate.
Farmer John knows his cows are smart, so he wants to make sure they cannot easily open the lock by simply trying many different number combinations. The lock has three dials, each labeled with the numbers $1$ to $n$. Because the dials are circular, $1$ and $n$ are adjacent. There are two combinations that can open the lock: one set by Farmer John, and another “preset” combination set by the locksmith. However, the lock has some tolerance: if on each dial the chosen number is within two positions (inclusive) of the corresponding number in some valid combination, the lock will also open.
For example, if Farmer John’s combination is ( $1$ , $2$ , $3$ ) and the preset combination is ( $4$ , $5$ , $6$ ), then the lock will open when the dials are set to ( $1$ , $4$ , $5$ ) (because this is close enough to Farmer John’s combination) or ( $2$ , $4$ , $8$ ) (because this is close enough to the preset combination). Note that ( $1$ , $5$ , $6$ ) will not open the lock because it is not close enough to either combination.
Given Farmer John’s combination and the preset combination, compute the number of distinct combinations that open the lock. The numbers are ordered, so ( $1$ , $2$ , $3$ ) is different from ( $3$ , $2$ , $1$ ).
Input Format
The first line contains an integer $n$, representing the number of numbers on each dial.
The second line contains three integers $x, y, z$, representing Farmer John’s combination.
The third line contains three integers $a, b, c$, representing the preset combination.
Output Format
Output a single integer on one line representing the number of combinations that open the lock.
Explanation/Hint
Explanation for Sample 1.
Each dial is labeled $1$ to $50$. Farmer John’s combination is ( $1$ , $2$ , $3$ ), and the preset combination is ( $5$ , $6$ , $7$ ).
Constraints.
For $100\%$ of the testdata, it is guaranteed that $1 \leq n \leq 100$, $1 \leq x, y, z, a, b, c \leq n$.
Translated by ChatGPT 5