/* 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; }
时间: 2024-10-22 14:14:47