先贴代码,以后再写题解。。。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 7 typedef long long ll; 8 9 ll s[100]; 10 11 ll init (int n){ 12 if (s[n]) 13 return s[n]; 14 s[n]=init (n-1)*2-(1<<(n-1))+(1<<n);//cout<<s[n]<<" "; 15 return s[n]; 16 } 17 18 int main (){ 19 int n; //while (cin>>n&&n) cout<<(1024>>n)<<endl; 20 ll ans=1; 21 int temp=2; 22 memset (s,0,sizeof s); 23 s[0]=1; 24 init (30); //for (int i=0;i<=30;i++) cout<<s[i]<<" "; 25 while (~scanf ("%d",&n)){ 26 ans=0; 27 for (int i=0;n&&i<30;i++) { 28 if (n&1) 29 ans+=s[i];//cout<<ans<<endl; 30 n>>=1; 31 //n/=2; 32 } 33 printf ("%lld\n",ans); 34 } 35 return 0; 36 }
acdream 1154 Lowbit Sum,布布扣,bubuko.com
原文:http://www.cnblogs.com/gfc-g/p/3881293.html