码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5524 Subtrees 递推

时间:2015-11-03 21:08:25      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

 

/*
Title :Subtrees
Status:AC
By wf,
*/
#pragma comment(linker, "/STACK:1024000000,1024000000")

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define FOR(i,s,t) for(int i = (s) ; i <= (t) ; ++i )

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e6+5;

ll n;
set<ll>s;

ll insertnum(ll rt){
    int c=0;
    while( rt<=n )
    {
        c++;
        s.insert( (1LL<<c)-1 );
        rt=(rt<<1)+1;
    }

	return ( (1LL<<c)-1 );


}
void up(ll rt,ll num,ll step){

	s.insert(num);
	//printf("insert::%d\n",num );
	if(rt==1)return;

	ll fa=rt/2;

	bool left=1;//当前结点为父节点的左孩子

	if(rt%2)left=0;

	ll other=0;
	if(left){
		other = rt+1;
	}else{
		other = rt-1;
	}

	ll othernum=0;
	if(other <= n) {
         othernum = insertnum(other);
	}

	up(fa,num+othernum+1,step+1 );
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%lld",&n)!=EOF){
		s.clear();
		up(n,1,1);
		printf("%d\n",s.size() );
	}

	return 0;
}

  

hdu 5524 Subtrees 递推

标签:

原文地址:http://www.cnblogs.com/bruce27/p/4934302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!