Educational Codeforces Round 29-C-1-2-3(循环节)

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

题意:两个人玩比点数大小的游戏,一共进行k轮,初始时第一个人猜a,第二个人猜b,每个人当前所猜的点数与上一次两个人的点数状态有关,两人的相关矩阵已经给你,问你k轮后两个人分别得了多少分(点数只有1,2,3,其中3比2大,2比1大,1比3大,相等的话两人都不得分)

题解:k有1e18这么大,当然不能模拟游戏过程,但是因为当前和上次的状态有关,我们会想到一定存在一个时候出现循环,并且之后就一直这样循环下去了,其中循环节一定很小,目测不会超过10,所以我们找到循环节一除就好了。

#include<stdio.h>
#define ll long long
int a[5][5],b[5][5],flag[5][5],A,B,AA,BB;
ll k,ans1,ans2,sm1,sm2;
void work(int x,int y)
{
	if(x==y) return;
	if(x==3 && y==2 || x==2 && y==1 || x==1 && y==3)
		sm1++;
	else
		sm2++;
}
int main(void)
{
	scanf("%lld%d%d",&k,&A,&B);
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			scanf("%d",&a[i][j]);
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			scanf("%d",&b[i][j]);
	//printf("%d\n",b[1][1]);
	ll num=0,sum1=0,sum2=0;AA=A;BB=B;
	while(flag[A][B]==0 && num<k)
	{
		num++;
		work(A,B);
		flag[A][B]=num;
		//printf("%d %d\n",A,B);
		int t1=A,t2=B;
		A=a[t1][t2];B=b[t1][t2];
	}
	//printf("%lld\n",num);
	ans1+=sm1;ans2+=sm2;k-=num;
	if(A!=AA || B!=BB)
		num=num-flag[A][B]+1;
	sum1=sm1;sum2=sm2;sm1=sm2=0;
	while(AA!=A || BB!=B)
	{
		work(AA,BB);
		int t1=AA,t2=BB;
		AA=a[t1][t2],BB=b[t1][t2];
	}
	sm1=sum1-sm1;sm2=sum2-sm2;
	ans1+=sm1*(k/num);ans2+=sm2*(k/num);
	k%=num;sm1=sm2=0;
	while(k>0)
	{
		k--,work(A,B);
		int t1=A,t2=B;
		A=a[t1][t2];B=b[t1][t2];
	}
	ans1+=sm1;ans2+=sm2;
	printf("%lld %lld\n",ans1,ans2);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值