本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:http://uoj.ac/problem/66
正解:结论+数学归纳法证明
解题报告:
看到这题第一想法就是猜一波结论,开始猜每次/2,然而过不了样例。
想了一想,似乎每次找到<=n的2^t,显然2^t贡献就是2^t-1,再把剩下的部分递归算就好了。结果就这么AC辣...
UOJ上vfleaking用数学归纳法给出了证明:http://vfleaking.blog.uoj.ac/blog/100
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <complex>
using namespace std;
typedef long long LL;
int n,ans;
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
inline void dfs(int m){
if(m<=1) return ;
int t=1; for(;t<=m;t<<=1) ; t>>=1;
ans+=t-1;
dfs(m-t);
}
inline void work(){
int T=getint();
while(T--) {
n=getint(); ans=0;
dfs(n);
printf("%d\n",ans);
}
}
int main()
{
work();
return 0;
}