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

题解

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

 //It is made by Awson on 2017.9.30
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int A[][], B[][];
LL k, ans;
int ra[], rb[], top;
int vis[][];
int a, b, l, r;
LL ans1, ans2, cnt1, cnt2; void vs(int a, int b, LL &x, LL &y) {
if (a == b) return;
if (a == ) {
if (b == ) y++;
else x++;
return;
}
if (a == ) {
if (b == ) y++;
else x++;
return;
}
if (a == ) {
if (b == ) y++;
else x++;
return;
}
}
void work() {
scanf("%I64d", &k);
read(a), read(b);
for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++)
read(A[i][j]);
for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++)
read(B[i][j]);
ra[++top] = a, rb[top] = b;
vis[a][b] = top;
while (true) {
a = A[ra[top]][rb[top]];
b = B[ra[top]][rb[top]];
if (vis[a][b]) {
l = vis[a][b], r = top;
break;
}
ra[++top] = a, rb[top] = b;
vis[a][b] = top;
}
if ((LL)l- > k) l = k+;
k -= l-;
for (int i = ; i < l; i++)
vs(ra[i], rb[i], ans1, ans2);
if (k) {
LL lenth = r-l+;
for (int i = l; i <= r; i++)
vs(ra[i], rb[i], cnt1, cnt2);
ans1 += k/lenth*cnt1;
ans2 += k/lenth*cnt2;
k %= lenth;
for (int i = ; i < k; i++)
vs(ra[l+i], rb[l+i], ans1, ans2);
}
printf("%I64d %I64d\n", ans1, ans2);
}
int main() {
work();
return ;
}

最新文章

  1. java关于压缩包的处理,我这里贴出三中格式的(zip,rar,tar)解压方法(目前只用到解压,所以压缩过多研究)。
  2. android重复的文件复制APK META-INF许可证错误记录
  3. Sublime、Webstorm,还有CLI、Atom,这些开发工具的更新你清楚吗?
  4. iOS多线程
  5. 视频聊天室可以用php制作吗?
  6. [问题2015S13] 复旦高等代数 II(14级)每周一题(第十四教学周)
  7. 28. Triangle &amp;&amp; Pascal&#39;s Triangle &amp;&amp; Pascal&#39;s Triangle II
  8. [HUST 1017] Exact cover
  9. jdbc&mdash;&mdash;CallableStatement,cst调用存储过程
  10. D3DXMatrixMultiply 函数
  11. 海量数据处理算法—Bloom Filter
  12. 与众不同 windows phone (7) - Local Database(本地数据库)
  13. weblogic 双机集群搭建
  14. ZOJ2286 Sum of Divisors 筛选式打表
  15. 【转载】详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
  16. 唱吧DevOps的落地,微服务CI/CD的范本技术解读----最大的难点并不是实际业务代码的编写,而是服务的监控和调试以及容器的编排
  17. Loading Data into a Table;MySQL从本地向数据库导入数据
  18. Kali学习笔记33:Linux系统缓冲区溢出实验
  19. [javascript] 看知乎学习js事件触发过程
  20. /etc/resolv.conf服务器客户端DNS重要配置文件

热门文章

  1. 听翁恺老师mooc笔记(4)--指针的应用场景
  2. alpha-咸鱼冲刺day3-紫仪
  3. 敏捷开发每日报告--day5
  4. 201621123057 《Java程序设计》第11周学习总结
  5. python day1 基本语法作业
  6. php代码开启缓冲的使用方法
  7. 第二章 初识JSP
  8. Python内置函数(64)——classmethod
  9. 如何排查CPU飙升的Java问题
  10. Python学习之输入输出、数据类型