Educational Codeforces Round 29

来源:互联网 发布:安信证券客户经理 知乎 编辑:程序博客网 时间:2024/06/15 16:23

C. 1-2-3
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 22beats 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, 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, 1Bi, 2Bi, 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 kgames.

Examples
input
10 2 11 1 11 1 11 1 12 2 22 2 22 2 2
output
1 9
input
8 1 12 2 13 3 13 1 31 1 12 1 11 2 3
output
5 2
input
5 1 11 2 22 2 22 2 21 2 22 2 22 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.


题意:

两个机器人猜拳,1,2,3分别代表3种可能。他们猜k次拳,第一次的情况已经告诉你了,剩下的局,每局两个人出什么都由上一局确定了,且唯一。让你求k次后,两个人的胜利的局数。


POINT:

总共就9种情况,根据规则找出循环节,再模拟即可。注意LL。


#include <iostream>#include <string.h>#include <stdio.h>#include <math.h>#include<algorithm>using namespace std;#define LL long longstruct node{    LL flag;    LL cnt;    LL asc;    LL bsc;}mp[4][4];LL aa[4][4];LL bb[4][4];void doit(LL n,LL &ansa,LL &ansb,LL a,LL b){    while(n){        if((a==2&&b==1)||(a==3&&b==2)||(a==1&&b==3)){            ansa++;        }        else if((b==2&&a==1)||(b==3&&a==2)||(b==1&&a==3)){            ansb++;        }        LL a1=aa[a][b];        b=bb[a][b];        a=a1;        n--;    }}int main(){    LL n,a,b;    memset(mp,0,sizeof mp);    scanf("%lld %lld %lld",&n,&a,&b);    LL sa=a,sb=b;    for(LL i=1;i<=3;i++){        for(LL j=1;j<=3;j++){            scanf("%lld",&aa[i][j]);        }    }    for(LL i=1;i<=3;i++){        for(LL j=1;j<=3;j++){            scanf("%lld",&bb[i][j]);        }    }    LL now=0;    LL asc=0;    LL bsc=0;    while(mp[a][b].flag==0){        mp[a][b].flag=1;        mp[a][b].cnt=++now;        if((a==2&&b==1)||(a==3&&b==2)||(a==1&&b==3)){            asc++;        }        else if((b==2&&a==1)||(b==3&&a==2)||(b==1&&a==3)){            bsc++;        }        mp[a][b].asc=asc;        mp[a][b].bsc=bsc;        LL a1=aa[a][b];        b=bb[a][b];        a=a1;    }    if((a==2&&b==1)||(a==3&&b==2)||(a==1&&b==3)){        asc++;    }    else if((b==2&&a==1)||(b==3&&a==2)||(b==1&&a==3)){        bsc++;    }    LL xunqian=mp[a][b].cnt-1;    LL xun=now-mp[a][b].cnt+1;    LL geta=asc-mp[a][b].asc;    LL getb=bsc-mp[a][b].bsc;    LL ansa=0,ansb=0;        if(n<=xunqian){            doit(n,ansa,ansb,sa,sb);        }        else{            doit(xunqian,ansa,ansb,sa,sb);            n=n-xunqian;            LL cnt=n/xun;            ansa+=cnt*geta;            ansb+=cnt*getb;            n=n%xun;            doit(n,ansa,ansb,a,b);        }    printf("%lld %lld\n",ansa,ansb);}