首页 > 其他分享 >Codeforces863C-1-2-3

Codeforces863C-1-2-3

时间:2022-11-22 19:03:49浏览次数:37  
标签:do Codeforces863C robots game Bob Alice inc


 

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

Input

The first line contains three numbers kab (1 ≤ k ≤ 1018, 1 ≤ 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

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.

标签:do,Codeforces863C,robots,game,Bob,Alice,inc
From: https://blog.51cto.com/u_15888102/5878356

相关文章