Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- signed main() {
- cin.tie(0)->sync_with_stdio(0);
- ll n; cin >> n;
- vector<int> a;
- // chuyen n thanh mang nhi phan
- while (n != 0) {
- a.push_back(n % 2);
- n /= 2;
- }
- ll ans = 0;
- // xet tung vi tri i
- for (int i = 0; i < (int) a.size(); i++) {
- // neu vi tri i la bit 1
- if (a[i] == 1) {
- // truong hop khong thay doi (tuc giu nguyen n) thi co 1 bit
- ans += 1;
- for (int j = 0; j < (int) a.size(); j++) {
- // minh dem so bit 1 o vi tri i nen khong xet truong hop thay doi bit i
- // thay doi bit thu j tu 1 thanh 0 thi moi thay doi cac bit truoc j duoc
- // nen minh bo qua truong hop a[j] == 0
- if (j == i || a[j] == 0) continue;
- // xet truong hop thay doi thay doi bit thu j va j bit dau (0 -> j - 1)
- // neu j > i thi phai tru bot di 1 vi khong thay doi vi tri i
- if (j < i) ans += (1ll << j);
- else ans += (1ll << (j - 1));
- }
- }
- // neu vi tri i la bit 0
- else {
- // minh chi xet nhung vi tri j lon hon i
- // de co the thay doi bit thu i thanh 1
- for (int j = i + 1; j < (int) a.size(); j++) {
- // thay doi bit thu j tu 1 thanh 0 thi moi thay doi cac bit truoc j duoc
- // nen minh bo qua truong hop a[j] == 0
- if (a[j] == 1) {
- // j > i nen phai tru bot di 1 vi khong thay doi vi tri i
- ans += (1ll << (j - 1));
- }
- }
- }
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement