标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 239 Accepted Submission(s): 156
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int _pow(int a,int n){ int ans = 1; while(n){ if(n&1) ans = ans*a; a=a*a; n>>=1; } return ans; } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ int n,m; scanf("%d%d",&n,&m); int k = n; int cnt = 0,ans=0; while(n){ if(n%2==1) ans++; cnt++; n/=2; } if(cnt<=m+1) printf("%d\n",ans); else{ int t = _pow(2,m); ans = 0; while(k&&t){ ans+=k/t; k = k%t; t/=2; } printf("%d\n",ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5700125.html