A. Shell Game

前端之家收集整理的这篇文章主要介绍了A. Shell Game前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 from0to2from left to right. Thus the left shell is assigned number0,the middle shell is1and the right shell is2. Bomboslav has missed the moment when the ball was placed beneath the shell,but he knows that exactlynmovements were made by the operator and the ball was under shellxat the end. Now he wonders,what was the initial position of the ball?

Input

The first line of the input contains an integern(1 ≤ n ≤ 2·109)— the number of movements made by the operator.

The second line contains a single integerx(0 ≤ x ≤ 2)— the index of the shell where the ball was found afternmovements.

Output

Print one integer from0to2— the index of the shell where the ball was initially placed.

Examples

input

4
2

output

1

input

1
1

output

0

Note

In the first sample,the ball was initially placed beneath the middle shell and the operator completed four movements.

  1. During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell.
  2. During the second move operator swapped the middle shell and the right one. The ball is still under the left shell.
  3. During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle.
  4. Finally,the operators swapped the middle shell and the right shell. The ball is now beneath the right shell.

———————————————————————————————————

题目的意思是给你3个杯子,其中一个有物品,每次奇数次交换0 1 两个,偶数次交换1 2两个,给出操作次数和最后物品的位置,求出物品起始位置

思路:找出6循环节,mod之后反向推

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
  int n,m;
  while(~scanf("%d%d",&n,&m))
  {
      n%=6;
      if(m==0)
      {
          if(n==1||n==2)
            printf("1\n");
          else if(n==3||n==4)
            printf("2\n");
          else
            printf("0\n");
      }
      else if(m==1)
      {
          if(n==0||n==3)
            printf("1\n");
          else if(n==2||n==5)
            printf("2\n");
          else
            printf("0\n");
      }
      else
      {
          if(n==4||n==5)
            printf("1\n");
          else if(n==0||n==1)
            printf("2\n");
          else
            printf("0\n");
      }
  }
    return 0;
}
原文链接:https://www.f2er.com/bash/392499.html

猜你在找的Bash相关文章