[Codeforces 863C]1-2-3

Description

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.

Sample Input

10 2 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2

Sample Output

1 9

题解

显然序列是循环的,我们找出循环节,并单独考虑循环节之前的和之后的。

 1 //It is made by Awson on 2017.9.30
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <cstdio>
10 #include <string>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 using namespace std;
19 void read(int &x) {
20   char ch; bool flag = 0;
21   for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
22   for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
23   x *= 1-2*flag;
24 }
25 
26 int A[5][5], B[5][5];
27 LL k, ans;
28 int ra[105], rb[105], top;
29 int vis[10][10];
30 int a, b, l, r;
31 LL ans1, ans2, cnt1, cnt2;
32 
33 void vs(int a, int b, LL &x, LL &y) {
34   if (a == b) return;
35   if (a == 1) {
36     if (b == 2) y++;
37     else x++;
38     return;
39   }
40   if (a == 2) {
41     if (b == 3) y++;
42     else x++;
43     return;
44   }
45   if (a == 3) {
46     if (b == 1) y++;
47     else x++;
48     return;
49   }
50 }
51 void work() {
52   scanf("%I64d", &k);
53   read(a), read(b);
54   for (int i = 1; i <= 3; i++)
55     for (int j = 1; j <= 3; j++)
56       read(A[i][j]);
57   for (int i = 1; i <= 3; i++)
58     for (int j = 1; j <= 3; j++)
59       read(B[i][j]);
60   ra[++top] = a, rb[top] = b;
61   vis[a][b] = top;
62   while (true) {
63     a = A[ra[top]][rb[top]];
64     b = B[ra[top]][rb[top]];
65     if (vis[a][b]) {
66       l = vis[a][b], r = top;
67       break;
68     }
69     ra[++top] = a, rb[top] = b;
70     vis[a][b] = top;
71   }
72   if ((LL)l-1 > k) l = k+1;
73   k -= l-1;
74   for (int i = 1; i < l; i++)
75     vs(ra[i], rb[i], ans1, ans2);
76   if (k) {
77     LL lenth = r-l+1;
78     for (int i = l; i <= r; i++)
79       vs(ra[i], rb[i], cnt1, cnt2);
80     ans1 += k/lenth*cnt1;
81     ans2 += k/lenth*cnt2;
82     k %= lenth;
83     for (int i = 0; i < k; i++)
84       vs(ra[l+i], rb[l+i], ans1, ans2);
85   }
86   printf("%I64d %I64d
", ans1, ans2);
87 }
88 int main() {
89   work();
90   return 0;
91 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/7615219.html