Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

0ms水之。好不容易找到的规律(code)

Posted by dinysirius at 2009-04-08 13:25:16 on Problem 2309
//发现一个数n,假如在树的倒数第floor层,则它的根节点为
//left = n - b[floor] + 1;
//right = b[floor] + n - 1;
//计算层数的话,就看这个数的因数里面的2的个数。
#include <iostream>
using namespace std;

int main(){
	
	int t,n;
	scanf("%d",&t);
	
	long long b[33] = {1};
	
	for(int i = 1 ; i <= 32 ; i ++ ){
		
		b[i] = b[i - 1] << 1;	
		b[i];
	}
	
	while(t -- ){
		
		scanf("%d",&n);
		
		int m = n;
		
		int floor = 0;
		
		while(!(m & 1)){//所在层数(倒数) 
			
			m >>= 1;
			
			++ floor;	
		}
		
		int left = n - b[floor] + 1;
		int right = b[floor] + n - 1;
		
		if(!floor)
			printf("%d %d\n",n,n);	

		else			
			printf("%d %d\n",left,right);			
	}		
	return 0;	
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator