Codeforces863C-1-2-3

1-2-3  

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 22 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 kab (1 ≤ k ≤ 10181 ≤ a, b ≤ 3).

Then 3 lines follow, i-th of them containing 3 numbers Ai, 1Ai, 2Ai, 3, where Ai, jrepresents 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, 1Bi, 2Bi, 3, where Bi, jrepresents 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.

Example
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.


题解:我们可以发现(a,b)是有规律的,将循环节找出即可快速求解。

Code:

var
  i,q,p,j,u,v,t1,t2:longint;k,ans1,ans2,k1,k2:int64;
  a,b,f:array[1..3,1..3] of longint;
  x,y:array[1..100] of longint;
function check(x,y:longint):boolean;
begin
  if (x-y=1)or(x-y=-2) then exit(true) else exit(false);
end;
begin
  readln(k,x[1],y[1]);
  for i:=1 to 3 do
    for j:=1 to 3 do read(a[i,j]);
  for i:=1 to 3 do
    for j:=1 to 3 do read(b[i,j]);
  for i:=1 to 100 do
  begin
    if f[x[i],y[i]]=0 then f[x[i],y[i]]:=1 else
      begin
        for j:=1 to i do
          if (x[j]=x[i])and(y[i]=y[j]) then break;
        j:=j-1;u:=j;
        v:=i-j-1;
        break;
      end;
    p:=a[x[i],y[i]];q:=b[x[i],y[i]];
    x[i+1]:=p;y[i+1]:=q;
  end;
  if k<=u then
  begin
    for i:=1 to k do
      if check(x[i],y[i]) then
        inc(ans1) else inc(ans2);
  end else
  begin
    for i:=1 to u do
      if check(x[i],y[i]) then
        inc(ans1) else
        if x[i]<>y[i] then inc(ans2);
    for i:=u+1 to u+v do
      if check(x[i],y[i]) then
        inc(t1) else
        if x[i]<>y[i] then inc(t2);
    k:=k-u;
    k1:=k div v;k2:=k mod v;
    ans1:=ans1+t1*k1;ans2:=ans2+t2*k1;
    for i:=u+1 to u+k2 do
      if check(x[i],y[i]) then
        inc(ans1) else
        if x[i]<>y[i] then inc(ans2);
  end;
  writeln(ans1,' ',ans2);
end.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack-Oran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值