Chef has a safe with N N dials. The i i-th dial ...

创建于:2025年3月26日

使用 O1 回答 Chat01

问题

Chef has a safe with
N
N dials. The
i
i-th dial initially shows the digit
A
i
A
i

.

To unlock the safe, the digit shown on the
i
i-th dial should be
B
i
B
i

.
In order to achieve this, Chef can choose a dial and perform one of the following two moves on it:

UP move: Increases the number on the chosen dial by
1

  1. If it is
    9
    9 then it becomes
    1

DOWN move: Decreases the number on the chosen dial by
1

  1. If it is
    1
    1 then it becomes
    9

In order to not seem too suspicious, Chef thinks he has to use exactly
K
K moves to unlock the safe.
Is it possible for Chef to unlock the safe while performing exactly
K
K moves in the process?

Note that Chef is allowed to reach the target digits even before
K
K moves: however, even if he does, he must still continue making moves till he has made exactly
K
K moves - it is not allowed to stop before that.
See the example cases below for more clarity.

Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
Each test case consists of three lines of input.
The first line contains two space-separated integers
N
N and
K
K — the number of dials and the exact number of moves allowed.
The second line contains
N
N space-separated integers
A
1
,
A
2
,

,
A
N
A
1

,A
2

,…,A
N

— the initial digits on the dials.
The third line contains
N
N space-separated integers
B
1
,
B
2
,

,
B
N
B
1

,B
2

,…,B
N

— the target digits on the dials.
Output Format
For each test case, print the answer on a new line: YES if Chef can unlock the safe after using exactly
K
K moves, and NO otherwise.

You may print each character of the answer in either uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).

Constraints

1

T

1
0
5
1≤T≤10
5


1

N

2

1
0
5
1≤N≤2⋅10
5


0

K

1
0
9
0≤K≤10
9

1

A
i
,
B
i

9
1≤A
i

,B
i

≤9
The sum of
N
N over all test cases does not exceed
2

1
0
5
2⋅10
5
.
Sample 1:
Input
Output
6
3 2
1 2 3
2 1 4
3 7
1 2 1
2 1 9
2 1
5 6
5 6
2 9
5 6
5 6
4 10
4 6 1 7
6 5 8 9
4 12
4 6 1 7
6 5 8 9
No
Yes
No
Yes
No
Yes
Explanation:
Test case
1
1: All three dials are different, but Chef wants to unlock in two moves. This is not possible, since only one dial can be changed in one move - so after two moves at least one dial will be incorrect.

Test case
2
2: One sequence of
K

7
K=7 moves is as follows:

UP on the first dial, now the sequence is
[
2
,
2
,
1
]
[2,2,1].
DOWN on the second dial, now the sequence is
[
2
,
1
,
1
]
[2,1,1].
UP on the third dial two times, now the sequence is
[
2
,
1
,
3
]
[2,1,3].
DOWN on the third dial three times, now the sequence is
[
2
,
1
,
9
]
[2,1,9], and we've used
1
+
1
+
2
+
3

7
1+1+2+3=7 moves as needed.
Test case
3
3: We already have
A
i

B
i
A
i

=B
i

for every
i
i initially. However, we need to perform a move, which will break that equality.

Test case
4
4: We have
A
i

B
i
A
i

=B
i

for every
i
i initially, and need to make
9
9 moves. Simply perform an UP move on the first dial
9
9 times, and it will end up back where it started.

accepted
Accepted
4
total-Submissions
Submissions
9
accuracy
Accuracy
44.44
Did you like the problem statement?
2 users found this helpful
More Info
Time limit1 secs
Memory limit1.5 GB
Source Limit50000 Bytes

code in java without comments

思考

Warming up the gears

I'm setting the stage by considering the safe's mechanics: N dials, each showing an initial digit from A1 to AN, that need to be rotated to unlock the safe.

Determining the angle

OK, let me see: each dial's target angle is the difference between its initial and target digit, modulo 30°. I’m piecing together the best steps to unloc' the safe.

Setting target angles

Figuring out each dial's target angle as the difference between digit and initial reading, modulo 30°, aiming to simplify the sequence of positions for unlocking the safe.

Navigating initial angles

OK, let me see: I’m calculating each dial's initial angle based on its initial digit, working through the sequence to align the dials symmetrically. Let's see how the pattern unfolds.

Calculating dials

I'm working through determining the minimum number of operations to set each dial at the right angle to unlock the safe, based on their initial and target numbers.

Identifying operational shifts

I’m piecing together the minimum rotations for each dial to hit the right angle, ensuring the safe opens without overdoing the turns. This step is about precise calibration.

Charting the path

I'm figuring out the safest routes to unlock each dial, ensuring even turns to avoid costly misalignments while keeping some progress.

Ensuring optimal alignment

I'm working through confirming that each dial's alignment stays sociable and efficient, aiming to keep the turns smooth and avoid errors. This helps compare using the output.

Reasoned for 7 seconds

回答

Here is the Java code for solving the problem:

java
import java.util.Scanner; public class SafeUnlock { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T-- > 0) { int N = sc.nextInt(); int K = sc.nextInt(); int[] A = new int[N]; int[] B = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } for (int i = 0; i < N; i++) { B[i] = sc.nextInt(); } int totalMoves = 0; for (int i = 0; i < N; i++) { int diff = Math.abs(A[i] - B[i]); totalMoves += Math.min(diff, 10 - diff); } if (totalMoves <= K && (K - totalMoves) % 2 == 0) { System.out.println("YES"); } else { System.out.println("NO"); } } sc.close(); } }

This code reads multiple test cases, calculates the number of moves required to transform the digits of the dials, and checks if exactly K moves can be performed to unlock the safe.

分享这个问答