Description
Linearians are pecurliar creatures. They are odd in several ways:
- Every Linearian is either red or blue.
- A Linearian colony is a straight line, aligned N-S with the magentic field.
- A colony starts with single red Linearian.
- Every year, each Linearian produces an offspring of the opposite color. After birth, the parent moves just south of the offspring. (Since everyone is born at once, this does make for a lot of jostling, but everyone stays in order.)
So a colony grows as follows:
N ----------- S Year 0: R Year 1: BR Year 2: RBBR Year 3: BRRBRBBR Year 4: RBBRBRRBBRRBRBBR
Given a year and a position along the N-S axis, determine what the color of the Linearian there will be.
Input
The first line is the year Y (0 <= Y <= 51). The second line is the position P from north to south, 0-indexed (0 <= P < 2^Y).
Ouput
The color of the Linearian, either red or blue.
Input | Input |
---|---|
3 6 | 51 123456789012345 |
Output | Output |
blue | red |
把blue 和 red 化为0 和1
1001011001101001
输入n和m,序列总长度为2^n。n是偶数,序列是回文串,n是奇数,序列左半边与右半边相反。
只要把第m个,不断向右对半翻折就能得出答案。
三种情况
取len=2^(n-1)
Ⅰm>len -> m=m-len,len/=2,n--。(砍去左半边序列)
Ⅱm<len -> m=len-m+1.len/=2(①n-1奇数,翻折后原点改变一次,flag计数++。②n偶数,翻折后不改变。)n--。
n==1时结束,flag奇数,red,否则blue。
各种细节出错,听取WA声一片,无语了……
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,m;
while(~scanf("%lld%lld",&n,&m))
{
bool flag=1;
ll len=1;
len<<=(n-1);
while(n>1)
{
if(m>len)
{
m=m-len;
}
else if(m<=len)
{
m=len-m+1;
if((n-1)&1)
flag^=1;
}
len>>=1;
n--;
}
if(flag)
printf("red\n");
else
printf("blue\n");
}
}