SPOJ - COLONY B - Linearian Colony 二分

Description

Linearians are pecurliar creatures. They are odd in several ways:
  1. Every Linearian is either red or blue.
  2. A Linearian colony is a straight line, aligned N-S with the magentic field.
  3. A colony starts with single red Linearian.
  4. 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.
InputInput
3
6
51
123456789012345
OutputOutput
blue
red

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
const int maxn=5e2+10;
using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull; 

ll y,p;

 
int main()
{
	ll sum =1;
	cin >> y>>p;
	p++;
	for(int i=1;i<=y;i++)
		sum*=2;
	int flag = 0; 
	ll l=1,r=sum;
	while(l<r)
	{
		ll mid = (l+r)/2;
		if(p<=mid)
		{
			flag ++;
			r = mid;
		}
		else
		{
			
			l = mid+1;
		}
	} 
	//cout <<l<<r<< flag <<endl;
	if(flag%2)
		cout <<"blue"<<endl;
	else
		cout <<"red"<<endl; 
	return 0;
	
} 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值