Shell Game (Codeforces-777A)

题目链接:

http://codeforces.com/problemset/problem/777/A

A. Shell Game
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

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

Output

Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.

Examples
input
4
2
output
1
input
1
1
output
0

题目大意:

有一个贝壳,三个碗(编号为0 1 2)。用任意一个碗盖住贝壳(有两个碗是空的),一次一次的交换三个碗的位置,但是交换有一定的规则:第(基数次)1、3、5、7......次交换然左边的碗和中间的碗;第2、 4、 6、 8......(偶数次)交换中间的碗和右边的碗。然后让观察者猜测最终贝壳在哪个几号碗了。但是题目要求我们的并不是这样,题目说是:你不知道开始时贝壳在哪个碗里,但是知道经过n次交换知道贝壳在x号碗里,然后让你计算最初贝壳在几号碗里(注意:编号从左到右为0 1 2,碗上并没有写编号,只是按照从左到右的顺序记为0 1 2)。

解题思路:

这是一道规律题(如下表所示),循环周期为6


知道了规律,代码就不成问题了,代码最好自己编写。

代码:

#include<iostream>
using namespace std;
int main()
{
    long long int n,x;
    while(cin>>n>>x)
    {
        switch(n%6)
        {
            case 0:
                cout<<x<<endl;break;
            case 1:
                switch(x)
                {
                    case 0:cout<<1<<endl;break;
                    case 1:cout<<0<<endl;break;
                    case 2:cout<<2<<endl;break;
                }
                break;
            case 2:
                switch(x)
                {
                    case 0:cout<<1<<endl;break;
                    case 1:cout<<2<<endl;break;
                    case 2:cout<<0<<endl;break;
                }
                break;
            case 3:
                switch(x)
                {
                    case 0:cout<<2<<endl;break;
                    case 1:cout<<1<<endl;break;
                    case 2:cout<<0<<endl;break;
                }
                break;
            case 4:
                switch(x)
                {
                    case 0:cout<<2<<endl;break;
                    case 1:cout<<0<<endl;break;
                    case 2:cout<<1<<endl;break;
                }
                break;
            case 5:
                switch(x)
                {
                    case 0:cout<<0<<endl;break;
                    case 1:cout<<2<<endl;break;
                    case 2:cout<<1<<endl;break;
                }
                break;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值