Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice".
So now Ilya wants to compare his robots' performance in a simple game called "1-2-3". This game is similar to the "Rock-Paper-Scissors" game: both robots secretly choose a number from the set {1, 2, 3} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3.
Both robots' programs make them choose their numbers in such a way that their choice in (i + 1)-th game depends only on the numbers chosen by them in i-th game.
Ilya knows that the robots will play k games, Alice will choose number a in the first game, and Bob will choose b in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all k games, so he asks you to predict the number of points they will have after the final game.
Input
The first line contains three numbers k, a, b (1 ≤ k ≤ 1018, 1 ≤ a, b ≤ 3).
Then 3 lines follow, i-th of them containing 3 numbers Ai, 1, Ai, 2, Ai, 3, where Ai, j represents Alice's choice in the game if Alice chose i in previous game and Bob chose j (1 ≤ Ai, j ≤ 3).
Then 3 lines follow, i-th of them containing 3 numbers Bi, 1, Bi, 2, Bi, 3, where Bi, j represents Bob's choice in the game if Alice chose i in previous game and Bob chose j (1 ≤ Bi, j ≤ 3).
Output
Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after k games.
Examples
Input
10 2 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
Output
1 9
Input
8 1 1
2 2 1
3 3 1
3 1 3
1 1 1
2 1 1
1 2 3
Output
5 2
Input
5 1 1
1 2 2
2 2 2
2 2 2
1 2 2
2 2 2
2 2 2
Output
0 0
Note
In the second example game goes like this:
The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve: Two robots playing k games (k up to 1e18). They have deterministic transition functions: Alice's next choice based on (previous Alice choice, previous Bob choice). Similarly Bob's next choice based on same pair. So we have a finite state machine with states being (a, b) where a, b ∈ {1,2,3}. Total 9 states. The game evolves deterministically: given current state (a_i, b_i), next state is (A_{a_i, b_i}, B_{a_i, b_i}). Also there is scoring each game (except maybe first? Actually the game description: "both robots secretly choose a number and say it at the same moment." So each game yields a point to one robot if numbers differ. The first game uses given initial choices a and b. Then after first game, we have result, then they compute next choices, and so on. So we need total scores after k games. So we need to simulate the state transition and scoring for k steps, but k huge, so we need to detect cycles and use fast exponentiation.
Since state space is only 9, the sequence of states (a_i, b_i) will eventually become periodic after some prefix. The total steps k could be huge, so we can find the cycle and compute scores efficiently.
We need to output Alice's score and Bob's score.
Approach:
Define state s = (a, b) with a,b ∈ {1,2,3}. Map to index 0..8: id = (a-1)*3 + (b-1). But also we need scores. Each state yields a score from that game? Actually the game's outcome is based on the choices a and b in that game. So when we are in state (a,b) at step i (i.e., game i's choices), then we add to scores accordingly. After the game, we compute next state for next game. So we have a sequence of states: s1, s2, ..., sk. Scores: for each st = (a_t, b_t), compute winner: if a_t == b_t -> no points. Else if (a_t == 1 && b_t == 3) or (a_t == 2 && b_t == 1) or (a_t == 3 && b_t == 2) then Alice gets 1. Else Bob gets 1. Actually rule: 3 beats 2, 2 beats 1, 1 beats 3. So Alice wins if (a,b) ∈ {(3,2), (2,1), (1,3)}; Bob wins otherwise when different. Check: if a=1,b=3: Alice beats Bob? 1 beats 3, yes. So Alice wins. If a=3,b=1: then Bob wins? 1 beats 3, so Bob gets point. Similarly a=2,b=3: 3 beats 2, Bob wins. a=3,b=2: Alice wins. a=1,b=2: 2 beats 1, Bob wins. a=2,b=1: Alice wins. Good.
So we can precompute score contribution per state: alice_add[s] ∈ {0,1} bob_add[s] ∈ {0,1} and they are complementary except draws (both 0). So we can treat as a pair.
We need total score after k steps. The transition from s to next(s) is deterministic. So we have a functional graph on 9 nodes, which is a set of cycles with trees feeding into them (like each node has exactly one outgoing edge). Since each node maps to exactly one node, the graph consists of cycles with directed trees leading into them (each node has exactly one outgoing, but multiple incoming). Starting from initial state s1 (given a,b), we will traverse a path that eventually enters a cycle. Since k is huge, we need to compute sum of scores along the path of length k.
We can do classic cycle detection: keep visited array for states with step index when first encountered. Compute prefix sum of scores for Alice and Bob up to each step. When we encounter a state that has been visited before, we have found a cycle. Then we can compute: let prefix length pre = first occurrence of the repeated state, say at step p (1-indexed). The current step is q (q > p) with same state. Then the cycle length = q - p. The scores accumulated in the cycle (from step p+1 to q inclusive? Actually careful: state s appears at step p and then again at step q. But note that state s at step p and step q are the same, but the transitions from s to next state is same. The cycle comprises the states from step p+1? Let's define: At step p we are at state sp. Then we compute next state for step p+1. So the sequence of states from step p to step q-1? Usually we think of states visited: s1, s2, ..., sp, sp+1, ..., sq where sp = sq. So the cycle includes states sp+1 through sq (which is same as sp). Actually the cycle is the sequence of states after sp up to sq. But the state sp appears again at sq, so the cycle length is (q - p). The states in the cycle: sp, sp+1, ..., sq-1? But sp and sq are same, so the cycle can be considered as length L = q-p, and the states within the cycle are sp, sp+1, ..., sp+L-1? But since sp = sq, we have L states distinct? Let's illustrate: Suppose we have states: s1, s2, s3, s4, s5, s6... and we detect that s4 = s2. Then p=2, q=4. The cycle is states s2, s3? Actually s2 -> s3 -> s4 (=s2). So the cycle consists of s2 and s3? But s2 appears at both start and end, but s2 is part of cycle. Usually in such functional graph, when we detect a repetition, the cycle is the states from the first occurrence to just before the second occurrence, inclusive? More precisely: If we have path: start -> ... -> X (first at step p) -> Y1 -> Y2 -> ... -> YL -> X (again at step q), then the cycle length is L (steps from step p to q-1). The cycle states are X, Y1, ..., YL-1? Because after X we go to Y1, then ... eventually some YL-1 leads to X. So the cycle consists of the distinct states visited from step p to step q-1. That's the typical cycle detection. So we can store when we first see a state, we record the step number and accumulated scores up to that step. Then when we later see a state we have seen, we can compute the prefix before cycle, the cycle sum, and remaining steps.
Implementation details:
We'll simulate step by step until we either exceed k or encounter a previously visited state. Since only 9 states, at most 10 steps we'll find repetition. So we can do a loop up to k, but k huge; but we break early once we find a cycle. Then compute using math.
But careful: We need to handle the fact that the state includes the scores? No, scores are separate. We only need state for transition. We'll maintain arrays visited_step[9] initialized to -1. Also we'll keep arrays prefA[step], prefB[step] maybe not for all steps but we can store cumulative scores up to each visited step at the moment we first visit a state. Since we will simulate step by step, we can store cumulative scores for each step as we go, but we can just store when we first encounter a state: the cumulative scores up to that step (including the score from that step? Actually careful: The step index starts at 1. At step 1, we have initial state s1 (given a,b). We should compute score for step 1. Then we transition to s2, which is next state. So at step i, we are at state s_i, and we add score(s_i) to totals. So the cumulative score after step i is sum_{t=1..i} score(s_t). At the moment we are at step i, we have visited state s_i. If we haven't seen s_i before, we record visited_step[s_i] = i, and also record cumulative scores up to i: cumA[i], cumB[i] perhaps store at that step. Alternatively we can store cumA at first visit for that state. That's fine.
When we later at step j see the same state s_j = s_i, we have that the cycle starts at step i, but we need to determine the cycle sum from step i+1 to step j? Actually we need to be careful: The state s_i appears at step i and step j. The sequence of states from step i to step j-1 is a cycle that returns to s_i. So the cycle length L = j - i. The cycle sum of scores (over the L steps) is: total score from step i+1 to step j? Let's compute: cumulative after step j is cum_j. Cumulative after step i-1 is cum_{i-1}. The sum of scores for steps i..j? Since state s_i appears at step i and step j, but the step j's state is s_i. So the cycle includes step i? Actually if we consider the path from step i to step j-1, that's L steps? Let's enumerate: step i: state s_i; step i+1: next; ... step j-1: some state; step j: s_i again. So the sequence of states from step i to step j-1 (inclusive) are the states that form a cycle (with s_i at start and repeated at end). The length (number of steps) from i to j-1 is j-i. So there are L steps in the cycle: steps i, i+1, ..., j-1. But step i is included. Then step j repeats s_i, so it's the start of the next cycle. Typically, we want to break the whole path into three parts: pre-cycle (steps before the first occurrence of the cycle start), then some number of full cycles, then remaining steps within the cycle.
Thus if we detect at step j that state s_j equals state seen at step i (i < j), then the pre-cycle length is i-1 (steps from 1 to i-1). The cycle starts at step i and consists of L = j - i steps (i to j-1). And note that step j is the first step of the next cycle repetition (but it's the same as step i). So the cycle's states are those at steps i, i+1, ..., i+L-1 (since j = i+L, steps i+L is step j, which is same state as step i). So we can treat the cycle as having L distinct states? Not necessarily distinct? Actually because it's a cycle, all states from i to i+L-1 are distinct; the cycle might have length L with L distinct states. Since s_i = s_{i+L}, it means that the set {s_i, s_{i+1}, ..., s_{i+L-1}} is the cycle, with s_i following s_{i+L-1} (since s_{i+L} = s_i). So indeed the cycle has L distinct states if the functional graph is deterministic, but could there be a case where some state repeats earlier? But we detect at the first repetition, so the cycle is minimal, and all states in the cycle are distinct.
Thus we can compute:
pre_len = i - 1
pre_A = cumA[i-1] (scores before entering cycle)
pre_B = cumB[i-1]
cycle_A = cumA[j] - cumA[i]? Wait: cumA[j] includes scores from steps 1..j. To get scores of the cycle (steps i..j-1), we need sum from step i to step j-1 inclusive. cumA[j-1] - cumA[i-1] gives that. But we may not have stored cumA at step j-1 directly. But we can compute cycle sums as (cumA[j-1] - cumA[i-1]) and similarly. However we have cumA[j] which includes step j as well (which is same state as step i). Step j is the first step of the next cycle. So cumA[j] - cumA[i] would be sum from step i+1 to j. That is not the cycle sum either. So we need to be careful.
Better approach: While simulating, we can store for each step the cumulative scores. We can store arrays for steps up to when we detect cycle. Since at most 9+1 steps before detection, it's fine. So we can simply store cumulative scores in vectors up to some point. But we might need to handle when k is less than the length of the path before cycle detection, in which case we just output cumulative up to k directly. So algorithm:
Simulate step = 1 to k:
At each step, compute current state s = (a_curr, b_curr). Compute score for this step and add to total.
Record visited step for this state if not visited: visited[s] = step; also store cumulative scores at this step (or maybe store cumulative before this step? Let's define: after processing step i, we have cumA_i, cumB_i. If we want to later compute cycle sum as difference between cumulative after step j-1 and after step i-1, we need cum after step i-1. So it may be easier to record when we first encounter a state before processing its score? But we need to know the state at step i to compute score. Actually state is known at step i, we can decide: When we first encounter a state (i.e., we are at step i and state s is seen), we record the step index i and the cumulative scores BEFORE processing this step? Or after? Let's think.
We need to detect repetition of state. The state we are at at step i is the state before we compute transition? Actually we start with initial state s1. Then we compute score for step 1 based on s1. Then we compute next state for step2. So at step i, we have state s_i, compute score(s_i), then transition to s_{i+1}. So when we detect repetition, we see s_i equal to some previous s_j. That means the sequence from j to i-1 forms a cycle? Let's examine: Suppose we first see state X at step j. Then we continue: step j -> score, then transition to next state. Eventually at step i we see X again. At step i we are about to compute score for that state. The cycle that repeats includes the states from step j to step i-1? Because after step j we go to step j+1 (some state). Then eventually after step i-1, the transition leads to step i where state X appears again. So the cycle length L = i - j. The cycle states are the states at steps j, j+1, ..., i-1 (since step i is the repetition of step j, but not included as part of the cycle if we consider the cycle as the set of states that repeat). Usually we treat the cycle as the sequence of states that leads from X back to X. That sequence includes X as start, and then each transition: X -> Y1 -> Y2 -> ... -> Y_{L-1} -> X. So the states visited in the cycle are X, Y1, ..., Y_{L-1}. These correspond to steps j, j+1, ..., i-1. So step i is the repetition of step j, and would be the start of the next cycle iteration. So to compute cycle sum, we want sum of scores for states X, Y1, ..., Y_{L-1}. That's steps j through i-1. That is length L. So cycle sum = cumulative after step i-1 minus cumulative after step j-1.
Thus if we store visited_step[s] = j (the step number where state s first appeared), we also need cumA[j-1] and cumA[i-1] to compute cycle sums. But we might not have cumA[j-1] directly unless we store cumulative at each step. Since number of steps small before detection, we can just store cumulative after each step in arrays (or vector) as we simulate. Then when we detect a state s that was visited before at step first = visited[s], we can compute:
pre_len = first - 1 (steps before cycle start)
cycle_len = i - first (i is current step)
cycle_score_A = cumA[i-1] - cumA[first-1]
cycle_score_B = cumB[i-1] - cumB[first-1]
Then total steps k. Let's compute:
If k <= number of steps we have simulated so far (i.e., we haven't yet detected cycle or we have already passed k), we can just output cumA[k], cumB[k] directly (since we simulate until we either exceed k or detect cycle). But note we simulate step by step; we might stop early when we detect a cycle before reaching k. If we detect cycle at step i (i.e., we are about to process step i and we see that state s_i was seen before), we can break out of simulation and compute using cycle math. But we must ensure that we have processed steps up to i-1 (since at step i we haven't added its score yet? Actually in the simulation loop, when we are at step i, we have state s_i. We check if visited[s_i] is set; if not, we mark visited and then compute score and add to cumulative, then transition. But if it is visited, we have detected a cycle. However note: At step i, we have not yet added score for this state. Should we include it? In the cycle detection, we see that state s_i repeats a previous state at step first. If we process step i normally, we would add score for s_i and then transition to s_{i+1}. But if we treat cycle detection, we can stop and compute totals using formulas. But we need to be careful because the state at step i is the same as at step first. But the step i is part of the cycle? In the cycle definition, the states that form the cycle are from step first to i-1 inclusive. Step i is the repetition of step first, which is the first state of the next cycle iteration. So if we were to continue, step i would be the start of the next cycle. So for computing total scores up to k, we need to account that steps first .. (i-1) form one full cycle, and then step i is the start of the next cycle. So it's convenient to treat the cycle as length L = i - first, and the scores for that cycle are from steps first to i-1 inclusive. Then step i is the first step of the next cycle, which is also step first of the cycle (i.e., it's the same state as step first). So if we want to compute total for k steps, we can break the sequence into:
Prefix: steps 1 to first-1.
Then some number of full cycles of length L (each cycle covering steps from some start to start+L-1), where each cycle sum = cycle_score_A, cycle_score_B.
Then suffix: remaining steps after full cycles, which are the first few states of the cycle (starting from step first, but not necessarily including the entire cycle).
But note that after the prefix, the sequence from step first onward is periodic with period L: the state at step t (t >= first) equals state at step first + ((t - first) mod L). However careful: The mapping of steps: step first corresponds to index 0 within cycle, step first+1 corresponds to index 1, ..., step first+L-1 corresponds to index L-1, step first+L corresponds to index 0 again. So indeed it's periodic with period L. So we can compute total score as:
total = prefix_scores + full_cycles * cycle_scores + suffix_scores.
Where prefix_scores = cumA[first-1], cumB[first-1].
Cycle_scores = (sum over one cycle) = cycle_score_A, cycle_score_B.
Suffix: Let remaining = k - (first-1). Then let num_cycles = remaining // L, and rem = remaining % L. Then suffix scores = sum of scores for the first rem states of the cycle (i.e., states at steps first, first+1, ..., first+rem-1). We need to compute those sums quickly. Since L <= 9, we can precompute prefix sums of the cycle states. We can record the order of states in the cycle: after we detect the cycle, we have the list of states in the cycle (from step first to step i-1). That's L states. Then we can compute cumulative sums over these states. Then suffix sum for rem is just sum of first rem states in that list.
So algorithm steps:
Parse input.
Initialize current state (a, b) from input.
Prepare arrays nextA[3][3], nextB[3][3] from input.
Initialize visited array of size 9 with -1.
Also we will store two arrays: stepStates list (maybe) and cumulative scores cumA and cumB per step, but we can also store them in vectors as we simulate steps. Since max steps before detection is at most 9 (since only 9 states, the earliest repetition can occur at step <= 10 because after 10 steps we have visited 10 states but only 9 possible, so by pigeonhole, repetition occurs by step 10. Actually it could be that initial state is repeated exactly at step 10? For 9 states, the maximum steps before seeing a repeat is 10: if first 9 steps are all distinct, step 10 must repeat one of them. So at most 10 steps simulation. So we can simulate up to k, but if k is small (k <= some threshold) we might just simulate directly without cycle detection. But we can always simulate step by step until either we have processed k steps, or we detect a repetition. Since k up to 1e18, we will simulate at most min(k, maybe 10) steps before detection. That's fine.
Simulation process:
Let step = 1.
Initialize cumA = 0, cumB = 0.
Initialize vectors: states (maybe for each step, store state? Actually we need to record order of states visited, and cumulative scores at each step. We can keep arrays stepState[], cumA_step[], cumB_step[] for step index from 1 upward as we go. But we also need to know when we first encountered a state. We'll maintain visited[ state_index ] = step number when first seen (i.e., the step index at which we are at that state). Note: At step step, we are at state s. We need to check if visited[s] is set. If not, set visited[s] = step. Then we compute score for this state, update cumA, cumB, then store cumA_step[step] = cumA, cumB_step[step] = cumB, and also maybe store state in a list. Then compute next state for next step. Increment step.
If visited[s] is already set (say firstStep = visited[s]), then we have found a cycle. But careful: At this moment, we haven't yet processed the score for this state (step). Actually we are at step step, and we are about to process it. The repetition means that we are seeing state s again at step step, which was first seen at firstStep. This step step is the start of the next cycle iteration. Should we process it now? In the total for k steps, if k >= step, we need to include the score for this state if step <= k. But note that we haven't yet processed steps beyond step-1. So our cumulative arrays currently reflect up to step-1. At the moment we detect cycle, we haven't added score for step step. But step step is the first step of the next cycle. For our cycle analysis, we want to treat the cycle as the states from firstStep to step-1 inclusive. So we should not add the score for step step now; we will later compute using cycles. However, what if k is exactly step-1? Then we already have cumA[step-1] and we can output that. So after detecting cycle, we can break out of simulation and compute total using formula. But before breaking, we need to have recorded the cumulative scores up to step-1. We already have them. Also we need to know the list of states in the cycle: from step firstStep to step-1. We can extract that from the stored states array.
Thus our simulation loop can be: for step from 1 while step <= k:
s = current state
if visited[s] != -1:
// found cycle start
firstStep = visited[s]
// we have already processed steps 1..step-1, with cumA = total after step-1
// cycle length = step - firstStep
// states in cycle = states[firstStep .. step-1] ( inclusive)
// cumulative before cycle = cumA_step[firstStep - 1] (score after step firstStep-1)
// we can break now to compute remaining using cycles.
break
else:
visited[s] = step
// compute score for this step
(addA, addB) = score(s)
cumA += addA
cumB += addB
// store cumulative for this step
cumA_step[step] = cumA
cumB_step[step] = cumB
// store state for this step
state_step[step] = s (or we can push onto list)
// compute next state
next_a = A[a][b] (but careful: arrays are 1-indexed)
next_b = B[a][b]
a = next_a; b = next_b
step++
But note: we increment step after processing? In the loop we can manage step counter. Typically we do:
step = 1
while step <= k:
s = (a,b)
if visited[s] != -1:
first = visited[s]
break
visited[s] = step
// add scores
// store
// compute next state
step += 1
// update a,b
But careful: after processing step, we increment step. So after loop break, step is the current step number that caused detection (i.e., we are about to process step but haven't). The loop condition checks step <= k; if step > k, we exit naturally after processing step-1 = k. So we need to handle both cases: we might finish k steps without detecting a cycle (or detecting after k? Actually if k is small, we might never hit a repetition because we stop before repetition). So we need to handle both.
Better: We'll simulate for up to k steps, but we break if we detect a cycle before we exceed k. However, detection should only happen when we are about to process a step that would cause repetition, but we haven't processed it yet. But if k is less than that step, we would have already stopped because step > k. So detection only occurs if we are about to process a step <= k. So break inside loop when visited[s] already set and step <= k.
After loop, we have two scenarios:
We have processed exactly k steps (i.e., step > k because we incremented step after processing each step, or we exited because step > k). We need to output cumA and cumB.
Or we broke due to cycle detection before processing step s (step). Then we need to compute the total for k steps using cycle decomposition.
Implementation details: We'll keep step as the index of the next game to process (starting at 1). We'll also keep current cumulative scores after last processed game. We'll keep arrays: cumA_arr, cumB_arr indexed by step number after processing that step, maybe we store them as we go. Also we store state at each processed step.
Pseudo-code:
read k, a, b
read A[4][4], B[4][4] (index 1..3)
define function getScore(a,b):
if a == b: return (0,0)
elif (a==1 and b==3) or (a==2 and b==1) or (a==3 and b==2): return (1,0)
else: return (0,1)
Initialize visited[9] = -1
Initialize step = 1
curr_a = a; curr_b = b
cumA = 0; cumB = 0
vectors: cumA_vec, cumB_vec, state_vec (maybe index from 1)
while step <= k:
s = (curr_a-1)*3 + (curr_b-1)
if visited[s] != -1:
first = visited[s] // first occurrence step number
// we have not yet processed this step (step)
break
// first time
visited[s] = step
// compute score
addA, addB = getScore(curr_a, curr_b)
cumA += addA; cumB += addB
// store cumulative and state for this step
cumA_vec.push_back(cumA); // index step-1? we can store at index step (1-based) by resizing.
cumB_vec.push_back(cumB)
state_vec.push_back(s) // index corresponds to step (i.e., state_vec[step-1] = s)
// compute next state
next_a = A[curr_a][curr_b]
next_b = B[curr_a][curr_b]
curr_a = next_a; curr_b = next_b
step += 1
After loop, if step > k (i.e., we processed all k steps):
output cumA, cumB
else: // we broke due to cycle detection, with step <= k
// At this point:
// - step: the current step index (the one we haven't processed)
// - first: step number where this state was first seen
// - cumA, cumB are cumulative after step-1 (since we processed steps 1..step-1)
// - cumA_vec and cumB_vec contain entries for steps 1..step-1 (indexed 0..step-2)
// - state_vec contains states for steps 1..step-1
// The cycle consists of steps from first to step-1 inclusive.
cycle_len = step - first
// Compute scores for the prefix before cycle:
pre_len = first - 1
if pre_len > 0:
pre_A = cumA_vec[pre_len-1] // because cumA_vec[first-2]? Actually cumA_vec indices: step 1 stored at index 0, step i at index i-1.
pre_B = cumB_vec[pre_len-1]
else:
pre_A = pre_B = 0
// Compute cycle total scores:
// The scores for steps first..step-1:
// cumA after step-1 is cumA (since we have processed up to step-1). cumA after first-1 is pre_A.
cycle_A = cumA - pre_A // because cumA is after step-1, pre_A after first-1.
cycle_B = cumB - pre_B
// Now we need total for k steps.
total_steps_done = step - 1 // already processed steps count
steps_remaining = k - total_steps_done
// However, note that the cycle we detected starts at step first. The remaining steps include the current step (step) which is part of the next cycle iteration. Actually after step-1, the next game to play is step. The state at step is the same as at step first. So the sequence from step first onward is periodic. So we can compute total as:
// total = pre_A + (number of full cycles covering steps from first to k) * cycle_A + suffix
// But we already have processed up to step-1. Instead of using total_steps_done, we can compute using prefix and cycles.
// Let's compute total steps from start:
// steps_before_cycle = first - 1
// steps_in_cycle = cycle_len
// The steps from step first onward are periodic.
// So we can compute: total_steps = k
// if k <= first-1, then we would have already output, but we are in case k >= step > first.
// So k >= first.
// Let L = cycle_len
// Let offset = k - (first - 1) // number of steps from first onward (including step first)
// Let full_cycles = offset / L
// let rem = offset % L
// total_A = pre_A + full_cycles * cycle_A + sum of scores for first rem steps of the cycle.
// sum of first rem steps of cycle: we need to compute scores for steps first, first+1, ..., first+rem-1.
// We have stored the states for these steps (since we have state_vec for steps up to step-1). But if rem > (step-1 - first + 1) maybe? Actually step-1 is first+L-1. So the cycle includes exactly steps first..first+L-1. So rem is from 0 to L-1. So we can get scores for those steps from stored data.
// But we only have cumulative scores per step. To get sum of first rem steps of cycle, we can compute:
// if rem == 0: suffix = 0
// else: let sum_rem_A = (cumA after step first+rem-1) - pre_A
// Since we have cumA_vec for step indices, we can compute using indices: cumA_vec[first+rem-2] - pre_A (since cumA_vec index i corresponds to step i+1)
// However, we must be careful: we have stored cumA_vec only up to step-1. But step = first+L, so step-1 = first+L-1, which is the last step of the cycle. So cumA_vec contains all steps up to first+L-1. So for any rem <= L, we have cumA_vec for step first+rem-1 as long as first+rem-1 <= step-1. That's true. So we can compute suffix_A = cumA_vec[first+rem-1 - 1] - pre_A = cumA_vec[first+rem-2] - pre_A. Similarly for B.
// But we need to be careful: cumA_vec is stored only for processed steps (1..step-1). Since first+rem-1 <= first+L-1 = step-1, it's fine.
// However, what if rem == L? Then suffix = cycle_A (full cycle). In that case offset % L = 0, and we treat rem = 0 because offset is multiple of L, meaning we have exactly full_cycles cycles, no extra partial cycle. So rem == 0.
// So we can compute as above.
// Then total_A = pre_A + full_cycles * cycle_A + suffix_A
// total_B similarly.
// Note: We must also consider that we already have cumA after step-1, but we are recomputing from start. That's fine.
code
// Compute offset:
offset = k - (first - 1)
full_cycles = offset // cycle_len
rem = offset % cycle_len
suffix_A = 0
suffix_B = 0
if rem > 0:
// cumA after step first+rem-1 is stored at index (first+rem-1)-1 = first+rem-2
// But need to ensure cumA_vec has that index.
// Also note: pre_A is cumA after step first-1.
// So suffix_A = cumA_vec[first+rem-2] - pre_A
suffix_A = cumA_vec[first + rem - 2] - pre_A
suffix_B = cumB_vec[first + rem - 2] - pre_B
total_A = pre_A + full_cycles * cycle_A + suffix_A
total_B = pre_B + full_cycles * cycle_B + suffix_B
output total_A, total_B
But we need to verify correctness: Consider scenario where k is exactly step-1 (i.e., we have already processed all steps before detection). In that case step <= k would not hold; we would exit loop normally because step > k (since after processing step = k we increment step to k+1 and loop condition fails). So we output cumA, cumB. So detection break only when step <= k and visited[s] is set. That ensures we haven't processed step yet, but step <= k, so there are remaining steps. Good.
Edge Cases: The cycle might start at step 1 (first=1). Then pre_len=0, pre_A=0. The offset = k. full_cycles = k // L, rem = k % L. But careful: We have processed steps up to step-1 (where step = first+L = 1+L). So we have processed L steps (1..L). Since step = L+1 (because step = first+L? Actually step = first+L? Let's derive: first = 1, we detected at step = ?. At step = L+1, we see state that was first seen at step 1, because steps 1..L are distinct and step L+1 repeats step 1. So step = L+1. So we have processed steps 1..L. So cumA after step-1 = cumA after step L, which is total after L steps. cycle_A = cumA - pre_A = cumA (since pre_A=0). That's correct. Then offset = k - 0 = k. full_cycles = k // L, rem = k % L. But note that if k < L, we would have already exited loop before detection, because we process steps up to k < L, and never see a repeat. So detection only occurs if k >= L+1? Actually detection occurs when step (the next step to process) is <= k and we see a repeat. So if k is exactly L, we will process steps 1..L, then step becomes L+1; since step > k (L+1 > L), loop condition fails, we don't detect, we output cumA after L steps. So fine. So detection scenario only when k >= step (i.e., k >= L+1). So offset = k. The formula total_A = pre_A + full_cycles * cycle_A + suffix_A = 0 + (k // L) * cycle_A + suffix_A. But suffix_A = sum of first rem steps of cycle. However, note that if rem = 0, then total_A = (k/L) * cycle_A. But is that correct? For k = L * t, where t integer, total after Lt steps should be t * cycle_A. That matches. For k = Lt + r (r < L), total = t * cycle_A + sum of first r steps of cycle. That is correct.
What about the case where the prefix length > 0? Works.
Now we need to ensure that the cycle sum is computed correctly: cycle_A = cumA - pre_A, where cumA is cumulative after step-1 (which is after processing steps first .. step-1 inclusive? Let's check: pre_A is cumulative after step first-1. cumA is cumulative after step-1, which includes steps first .. step-1. So indeed cycle_A = cumA - pre_A equals total scores of steps first .. step-1. Good.
Now we need to store cumA_vec and cumB_vec as we go. Since step count before detection is at most 9, we can use arrays of size maybe up to 9 but could be more if k is small? Actually if k is huge, we break early after at most 10 steps. So vectors can hold up to step-1 which is at most 9 (if detection at step 10, step-1=9) or maybe 10 if detection at step 11? Actually maximum distinct states =9, so first repetition occurs at step <= 10. At step 10, we might see state that was seen earlier. So step can be at most 10 when detection occurs (since first step=1, at step 10 we have seen 10 states, but only 9 possible, so at least one repeat). So step <= 10. So step-1 <= 9. So vectors are tiny.
But careful: If the initial state repeats exactly after many steps? Not possible because there are only 9 states. So maximum number of steps before repetition is 10. So indeed detection occurs at step <= 10. So simulation loop will run at most min(k, 10) steps before either finishing k (if k <= 9?) Actually if k is small like 1, we just simulate 1 step and stop because step becomes 2 > k. So we never get to detection. If k is larger than 9, we will simulate until detection (since step will reach at most 10 before repetition). So at most 10 steps simulated. Good.
Thus we can implement with arrays of size maybe 20.
One more nuance: When we detect cycle, we break out of loop without processing step. But we also haven't stored the state for step (the repeated one) in state_vec. That's fine because we only need states up to step-1 for cycle representation.
Now compute suffix_A: if rem > 0, we need sum of scores for first rem steps of the cycle. Since the cycle consists of steps first, first+1, ..., first+L-1, and we have cumA_vec for each step index. But note that cumA_vec stores cumulative after each step. So to get sum of scores for steps first .. first+rem-1, we can compute: if rem>0, suffix_A = cumA_vec[first+rem-1 - 1] - pre_A. But careful about indexing: cumA_vec is 0-indexed: cumA_vec[i] corresponds to cumulative after step i+1. So for step j, its cumulative is cumA_vec[j-1]. So pre_A = cumA_vec[first-2] if first>1 else 0. For step first+rem-1, cumulative is cumA_vec[first+rem-2]. So suffix_A = cumA_vec[first+rem-2] - pre_A. That works if first+rem-2 is a valid index. Since first+rem-1 <= first+L-1 = step-1, we have first+rem-2 <= step-2, and we have cumA_vec up to step-2? Actually cumA_vec stores entries for each processed step. We have processed steps up to step-1, so the last index is (step-1)-1 = step-2. And first+rem-2 <= step-2 because first+rem-1 <= step-1 => first+rem-2 <= step-2. So it's valid.
But we need to be careful when first=1 and rem>0: pre_A=0, cumA_vec[first+rem-2] = cumA_vec[rem-1] which exists as long as rem-1 <= step-2. Since rem <= L-1 (if rem>0), and step-1 = L, so step-2 = L-1, so rem-1 <= L-2? Actually if rem <= L-1, then rem-1 <= L-2, which is <= L-1, so safe. But if rem == L? Then rem would be 0 because offset % L = 0, so rem=0, not >0. So safe.
So algorithm seems good.
Let's test with examples.
Example 1:
Input:
10 2 1
Alice transitions: all 1s
Bob transitions: all 2s
So A always 1, B always 2 after first? Actually first game: a=2,b=1. Score: 2 beats 1? 2 beats 1? rule: 2 beats 1 -> Alice gets point? Actually 2 beats 1: Alice wins. So Alice gets 1. Then next: A=1, B=2 -> game: 1 vs 2: 2 beats 1 -> Bob wins. So Bob gets 1. Then next: A=1, B=2 again (since always same). So from game 2 onward, always (1,2) and Bob wins. So for k=10: first game Alice wins, games 2-10 Bob wins (9 games). So scores: Alice 1, Bob 9. Output matches.
Our algorithm: initial state (2,1) -> score (1,0). visited[state] set for step1. Next: (A[2][1], B[2][1]) = (1,2). Step2: state (1,2) first time -> score (0,1). Next: (A[1][2]=1, B[1][2]=2) -> (1,2) again. So at step3, we see state (1,2) already visited at step2. So detection: first=2, step=3. pre_len=1, pre_A = cumA after step1 = 1, pre_B = 0? Actually after step1: A=1,B=0. cycle_len = 3-2=1. Wait step=3, first=2, cycle_len=1. So cycle consists of step2 only? Actually steps: step1: (2,1); step2: (1,2); step3 would be (1,2) again. So the cycle is step2 only? But state at step2 is (1,2) and from step2 to step3? Let's compute: At step2 we had state (1,2). Then after step2 we computed next state: (1,2) again. So the cycle is: from state (1,2) it transitions to itself. So indeed cycle length 1. So cycle includes step2? According to our definition: cycle includes steps first to step-1: steps 2 to 2 inclusive, so step2 only. That's correct. cycle_A = cumA after step2? cumA after step2 = 1 (Alice only from step1). pre_A after step1 = 1. So cycle_A = 0. cycle_B = cumB after step2 = 1, pre_B=0, so cycle_B=1. So cycle sum: Alice 0, Bob 1 per cycle. Now k=10. prefix steps: first-1 = 1. So steps before cycle: step1. prefix scores: (1,0). Then offset = k - 1 = 9 steps from step2 onward. L=1. full_cycles = 9, rem=0. So total = prefix + 9 * cycle = (1,0) + 9*(0,1) = (1,9). Good.
Example 2: Let's compute manually later to verify.
Example 3: Output 0 0.
Seems fine.
Potential pitfalls:
When we compute next state: we need to use the transition tables A and B with indices based on previous choices. So after processing a game, we update a,b for next game based on the current a,b (the ones used for that game). That matches algorithm.
Need to ensure we use correct indexing: A[a][b] and B[a][b] where a,b are in {1,2,3}. Provided input rows correspond to i (Alice previous) and columns to j (Bob previous). Yes.
Now we must consider the case where the cycle is detected exactly at step = first, i.e., the state repeats immediately? That would mean that after processing step first, the next state is same as step first? Actually detection occurs when we are about to process step (step) and the current state is same as some previous state. If the first occurrence was at step first, and step = first+? Actually if the state repeats at step = first+1, that's the earliest possible. Could step = first? That would mean we are seeing the same state at the same step? Not possible because step increments. So step > first always. So cycle length >= 1.
Now consider if k is extremely large, but we compute full_cycles * cycle_A, both could be up to 1e18. Need to use 64-bit integers (long long). k up to 1e18, scores up to k. Use unsigned 64? But could be negative? No scores non-negative. Use long long (signed 64-bit) since k up to 1e18, total score up to 1e18 fits in signed 64-bit (max 9e18). Actually 1e18 fits within 2^63-1 (9.22e18). So safe.
Edge Cases: When first=1, pre_A=0, cumA_vec[first-2] doesn't exist. But we only use pre_A via cumA_vec when first>1; we set pre_A=0 manually.
Need to store cumA_vec and cumB_vec for step indices up to step-1. Since step is small, we can use vector and push_back after each processed step. But we need random access to indices like first+rem-2. Since we store sequentially, we can index via vector.
We also need state_step maybe for suffix sum? Actually we use cumA_vec to compute suffix sums directly without needing state list, as we use cumulative differences. That works as long as we have cumA_vec entries for steps first+rem-1. So we need to store cumulative after each step. That's fine.
But we must ensure that when rem > 0, we compute suffix_A = cumA_vec[first+rem-2] - pre_A. However, we need to guarantee that cumA_vec has that index. Since we have processed steps up to step-1, and first+rem-1 <= step-1, so first+rem-2 <= step-2. Since we have cumA_vec of size step-1 (index 0..step-2). So we can access.
But careful: If first = 1 and rem = 1, then first+rem-2 = 0? Actually first=1, rem=1 => 1+1-2=0, valid. So we need cumA_vec[0] which is cumulative after step1. That's fine.
Now, what about the case where we break due to detection and k is exactly step-1? Actually if k is exactly step-1, then step <= k? At the moment of detection, step is the next step to process. If step-1 = k, then step = k+1, but then step <= k would be false. So we wouldn't break inside loop because we would have already incremented step after processing the k-th step, making step = k+1, and the loop condition step <= k false, so we exit normally. So detection only when step <= k, i.e., k >= step. Since step is at least 2 (because we need at least two steps to have a repeat), detection implies k >= step, meaning there is at least one remaining step to process. So our handling for detection is for k > step-1. Good.
Let's simulate manually to confirm cycle detection.
We'll compute step by step:
Game1: a=1,b=1 -> draw (0,0). Next: A[1][1] = 2? From first row: 2 2 1 -> so A[1][1]=2. B[1][1] = first row of Bob: 1 1 1 -> so B[1][1]=1. So state2: (2,1).
Game2: a=2,b=1 -> Alice wins? 2 beats 1 => Alice +1. Score: (1,0). Next: A[2][1] = from second row: 3 3 1 -> A[2][1]=3. B[2][1] = second row of Bob: 2 1 1 -> B[2][1]=2? Wait second row: "2 1 1", so B[2][1] = 2. So state3: (3,2).
Game3: a=3,b=2 -> Alice wins? 3 beats 2 => Alice +1. Score: (2,0). Next: A[3][2] = third row: 3 1 3 -> A[3][2]=1? Actually third row: "3 1 3", so A[3][2] = 1 (since second column). B[3][2] = third row of Bob: "1 2 3", so B[3][2] = 2 (second column). So state4: (1,2).
Game4: a=1,b=2 -> Bob wins? 2 beats 1 => Bob +1. Score: (2,1). Next: A[1][2] = first row: 2 2 1 -> A[1][2]=2. B[1][2] = first row Bob: 1 1 1 -> B[1][2]=1. So state5: (2,1).
Game5: a=2,b=1 -> same as game2. Score: Alice wins => (3,1). Next: A[2][1]=3, B[2][1]=2 -> state6: (3,2).
Game6: a=3,b=2 -> same as game3. Score: Alice wins => (4,1). Next: A[3][2]=1, B[3][2]=2 -> state7: (1,2).
Game7: a=1,b=2 -> same as game4. Score: Bob wins => (4,2). Next: A[1][2]=2, B[1][2]=1 -> state8: (2,1).
Game8: a=2,b=1 -> Score: Alice wins => (5,2). Next: A[2][1]=3, B[2][1]=2 -> state9: (3,2). (but game ends after 8 games, k=8, so we stop after game8).
So scores: Alice 5, Bob 2. Matches output.
Now let's see if our cycle detection would help:
States: step1: (1,1)
step2: (2,1)
step3: (3,2)
step4: (1,2)
step5: (2,1)