TopcoderArchive
Archive
Algorithm problem

BinarySequence

Problem statement, definition, constraints, and public examples.

Problem Statement

一个二进制序列由下面的伪代码生成:

String A = "0"
While (A的长度小于等于n)
	创建一个和A一样长度的字符串B
	For i=0,1,...length(A)-1
		If (i 是完å
¨å¹³æ–¹æ•°)
			B[i] = 1-A[i]
		Else
			B[i] = A[i]
	令A = A + B (即将B拼接在A后面)
End While
Return A

请注意,在上面的伪代码中,A[i]和B[i]分别表示字符串A和B中下标为i的字符(下标编号从0å¼€å§‹ï¼‰ã€‚å¯¹â€œå®Œå ¨å¹³æ–¹æ•°â€çš„å®šä¹‰æ˜¯ï¼Œå¯¹äºŽæ•´æ•°i,存在整数j,使得i= j *j,则称iä¸ºå®Œå ¨å¹³æ–¹æ•°ã€‚

ä¸‹é¢å ·ä½“è¯´æ˜Žåºåˆ—ç”Ÿæˆçš„è¿‡ç¨‹ï¼šå¦‚æžœn=7,则在每一轮迭代中,A的取值依次为:0, 01, 0110, 01101010,所以最终产生的二进制序列就是0,1,1,0,1,0,1,0

请返回上述序列中下标为n的数字(该序列的下标从0开始)

Definition

Class:
BinarySequence
Method:
getValue
Parameters:
int
Returns:
int
Method signature:
int getValue(int n)
(be sure your method is public)

Constraints

  • n 的取值大于等于0,小于等于2,000,000,000

Examples

  1. 7
    Returns: 0
    原因参见题目描述。
  2. 2
    Returns: 1
  3. 8
    Returns: 1
    这一次,生成的序列为0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0.
  4. 11
    Returns: 0
  5. 10
    Returns: 1
  6. 15
    Returns: 0
Back to all problems