标签:coder single comment multiple ref algorithm alt php input
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
就是如果左子树和右子树相同只需要遍历一颗子树即可;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=2e3+10,M=4e6+10,inf=2147483647; const ll INF=1e18+10,mod=1e9+7; /// 数组大小 set<ll>ans; map<ll,ll>si; ll n; void dfs(ll x) { if(x>n)return; ll l=x,r=x; while(l*2<=n)l*=2; while(r*2+1<=n)r=r*2+1; if(l==x&&r==x) { si[x]=1; ans.insert(1); return; } if(l<=r) { dfs(x*2); si[x]=2*si[x*2]+1; ans.insert(si[x]); //cout<<x<<" "<<si[x]<<endl; } else { dfs(x*2); dfs(x*2+1); si[x]=si[x*2]+si[x*2+1]+1; //cout<<x<<" "<<si[x]<<" "<<si[x*2]<<" "<<si[x*2+1]<<endl; ans.insert(si[x]); } } int main() { while(~scanf("%lld",&n)) { ans.clear(); si.clear(); dfs(1); printf("%d\n",ans.size()); } return 0; }
标签:coder single comment multiple ref algorithm alt php input
原文地址:http://www.cnblogs.com/jhz033/p/6684188.html