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 |
思路就是通过一个循环,不断更新你要求的点的位置,往上翻,翻了n次。找到你想要的。
只不过本博主栽倒了一个地方(因为水平有限),就是p=1<<n;这里的1是int型,而p是longlong 型,所以尽管每一组样例都可以过,但就是不能ac。
AC代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
long long n,m;
while(~scanf("%lld%lld",&n,&m))
{
m+=1;
long long p=(long long)1<<n,count=0;
for(int i=0; i<n; i++)
{
if(m>p/2)
p/=2,
m-=p;
else if(m<=p/2)
{
count++;
p/=2;
}
}
if(count&1)
printf("blue\n");
else
printf("red\n");
}
}