Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.
Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).
Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?
The first line of the input contains an integer n (1 ≤ n ≤ 2·109) — the number of movements made by the operator.
The second line contains a single integer x (0 ≤ x ≤ 2) — the index of the shell where the ball was found after n movements.
Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.
4 2
1
1 1
0
In the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements.
- During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell.
- During the second move operator swapped the middle shell and the right one. The ball is still under the left shell.
- During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle.
- Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell.
题目大意: 换贝壳游戏,一个球藏在编号为0.1.2三个贝壳里面,现在一个操作员对其进行操作,当进行第奇数次操作时,0和一号贝壳交换,当进行第偶数次操作时,1和2号贝壳交换。然后告诉你操作数和最后球在第几个贝壳里面,问你最开始球在几号贝壳里面。
题目分析:一道找规律题目,没想到,我找规律找错了,,,真无语,每六次一个循环,直接模就行了。规律找错的人真心伤不起。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[100];
int main(){
long long n,op;
int t;
memset(a,0,sizeof(a));
a[0]=0;a[1]=1;a[2]=2;
while((scanf("%I64d%I64d",&op,&n))!=EOF){
op = op%6;
for(int i=1;i<=op;i++){
if( i%2 == 0){
t=a[1];
a[1]=a[2];
a[2]=t;
}
if( i%2 != 0){
t=a[1];
a[1]=a[0];
a[0]=t;
}
}
printf("%d\n",a[n]);
}
return 0;
}