shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/aoj/itp2-10-c.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_10_C"

#include <iostream>

#include "../../src/base/bit.hpp"

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int q;
    std::cin >> q;
    unsigned long long s = 0;
    while (q--) {
        int op;
        std::cin >> op;
        if (op <= 3) {
            int i;
            std::cin >> i;
            unsigned long long b = 1ULL << i;
            if (op == 0) std::cout << ((s & b) ? 1 : 0) << '\n';
            if (op == 1) s |= b;
            if (op == 2) s &= ~b;
            if (op == 3) s ^= b;
        } else if (op == 4) {
            std::cout << (s == ~0ULL ? 1 : 0) << '\n';
        } else if (op == 5) {
            std::cout << (s != 0 ? 1 : 0) << '\n';
        } else if (op == 6) {
            std::cout << (s == 0 ? 1 : 0) << '\n';
        } else if (op == 7) {
            // popcount は非負の long long を受け取るので、上位 1 ビットと残りに分けて数える
            std::cout << popcount(static_cast<long long>(s >> 1)) + static_cast<int>(s & 1) << '\n';
        } else {
            std::cout << s << '\n';
        }
    }
}
#line 1 "test/aoj/itp2-10-c.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_10_C"

#include <iostream>

#line 2 "src/base/bit.hpp"

/**
 * @file bit.hpp
 * @brief ビット演算
 */

#include <cassert>
#include <string>
#include <vector>

/**
 * @brief 2進数の文字列をlong longにする
 */
inline long long btoll(std::string s, char one = '1') {
    long long res = 0;
    for (char c : s) {
        res <<= 1;
        if (c == one) ++res;
    }
    return res;
}

#if __cplusplus < 202000L

/**
 * @brief 立っているビットを数える
 * __builtin_popcountll
 */
inline int popcount(long long a) {
    assert(a >= 0);
    return __builtin_popcountll((unsigned long long)a);
}
/**
 * @brief 左から連続した0のビットを数える
 * __builtin_clzll
 */
inline int countl_zero(long long a) {
    assert(a >= 0);
    return __builtin_clzll((unsigned long long)a);
}
/**
 * @brief 右から連続した0のビットを数える
 * __builtin_ctzll
 */
inline int countr_zero(long long a) {
    assert(a >= 0);
    return __builtin_ctzll((unsigned long long)a);
}

#else

#include <bit>
#define BIT_FUNC_EXPAND(func)                      \
    inline constexpr long long func(long long a) { \
        assert(a >= 0);                            \
        return std::func((unsigned long long)a);   \
    }

/**
 * @brief 立っているビットを数える
 */
BIT_FUNC_EXPAND(popcount)
/**
 * @brief 左から連続した0のビットを数える
 */
BIT_FUNC_EXPAND(countl_zero)
/**
 * @brief 左から連続した1のビットを数える
 */
BIT_FUNC_EXPAND(countl_one)
/**
 * @brief 右から連続した0のビットを数える
 */
BIT_FUNC_EXPAND(countr_zero)
/**
 * @brief 右から連続した1のビットを数える
 */
BIT_FUNC_EXPAND(countr_one)
/**
 * @brief 整数値を2の累乗値に切り上げる
 */
BIT_FUNC_EXPAND(bit_ceil)
/**
 * @brief 整数値を2の累乗値に切り下げる
 */
BIT_FUNC_EXPAND(bit_floor)
/**
 * @brief 値を表現するために必要なビット幅を求める
 */
BIT_FUNC_EXPAND(bit_width)

#endif
#line 6 "test/aoj/itp2-10-c.test.cpp"

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int q;
    std::cin >> q;
    unsigned long long s = 0;
    while (q--) {
        int op;
        std::cin >> op;
        if (op <= 3) {
            int i;
            std::cin >> i;
            unsigned long long b = 1ULL << i;
            if (op == 0) std::cout << ((s & b) ? 1 : 0) << '\n';
            if (op == 1) s |= b;
            if (op == 2) s &= ~b;
            if (op == 3) s ^= b;
        } else if (op == 4) {
            std::cout << (s == ~0ULL ? 1 : 0) << '\n';
        } else if (op == 5) {
            std::cout << (s != 0 ? 1 : 0) << '\n';
        } else if (op == 6) {
            std::cout << (s == 0 ? 1 : 0) << '\n';
        } else if (op == 7) {
            // popcount は非負の long long を受け取るので、上位 1 ビットと残りに分けて数える
            std::cout << popcount(static_cast<long long>(s >> 1)) + static_cast<int>(s & 1) << '\n';
        } else {
            std::cout << s << '\n';
        }
    }
}
Back to top page