Skip to content

Commit d93560e

Browse files
authored
Create 1146_CountingBits.cpp
1 parent 84d18bf commit d93560e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain, give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
#define ll long long
13+
#define ull unsigned long long int
14+
15+
int main(){
16+
ios_base::sync_with_stdio(false);
17+
cin.tie(NULL);
18+
19+
ll n;
20+
cin>>n;
21+
22+
23+
vector<ll> vec;
24+
25+
ll temp=n;
26+
while(temp!=0){
27+
vec.push_back(temp%2);
28+
temp/=2;
29+
}
30+
31+
reverse(vec.begin(),vec.end());
32+
ll cnt=vec.size() -1;
33+
34+
ull ans=0;
35+
36+
ll val=0;
37+
38+
// ll ans1=0;
39+
// for(ll i=1;i<=n;i++){
40+
// temp=i;
41+
// while(temp!=0){
42+
// ans1+=temp%2;
43+
// temp/=2;
44+
// }
45+
// }
46+
47+
// cout<<ans1<<e
48+
ll vl=pow(2,cnt);
49+
for(auto i:vec){
50+
// cout<<i<<" ";
51+
52+
if(i==1){
53+
if(cnt>=0){
54+
ans+=(ull)((cnt)*(ull)(vl/2));
55+
ans+=(ull)((ull)(vl)*(val));
56+
}
57+
val++;
58+
}
59+
vl/=2;
60+
61+
62+
cnt--;
63+
}
64+
// cout<<endl;
65+
ans+=val;
66+
67+
68+
cout<<ans<<endl;
69+
70+
71+
72+
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)