题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4349
数出n的二进制有x位1,再求2的x次幂就可以,数据不大不用优化
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 typedef long long LL; 8 9 LL solve(int x){ 10 int cnt = 0; 11 while(x){ 12 if(x % 2) 13 cnt++; 14 x >>= 1; 15 } 16 LL sum = 1; 17 for(int i = 1; i <= cnt; ++i) { 18 sum *= 2; 19 } 20 return sum; 21 } 22 23 int main(){ 24 int n; 25 while(scanf("%d",&n) != EOF){ 26 LL ans = solve(n); 27 printf("%d ", ans); 28 } 29 return 0; 30 }