跳到正文

剪贴板

剪贴板 p0661iv2

原文
剪贴板信息

剪贴板信息

收录
更新

返回上页\rule{120pt}{30pt}\kern{-85pt}\color{white}\raisebox{12pt}{\sf 返回上页}

状态压缩模板

C++
template <class T, int Size>
class StateNum {
protected:
	using state_t = StateNum<T, Size>;

public:
	T x;

	state_t& operator= (const T& y) {x = y; return *this;}
	state_t& operator= (const string& s) {
		if (s.empty() || s == "0") return x = 0, *this;
		x = 0; int n = s.size();
		for (int i = 0; i < n; i++) x += (s[i] == '1') * (T(1) << (n - i - 1));
		return *this;
	}

	StateNum() {*this = 0;}
	StateNum(const T& y) : x(y) {}
	StateNum(const string& s) {*this = s;}

	operator T() const {return x;}

	int size() const {return Size;}

	int count() const {return __builtin_popcountll(x);}
	state_t lowbit() const {return x & (-x);}

	friend istream& operator>> (istream& in, state_t& s) {
		in >> s.x;
		return in;
	}
	friend ostream& operator<< (ostream& out, const state_t& s) {
		out << s.x;
		return out;
	}

	bool operator[] (int k) const {return x >> (k - 1) & 1;}

	state_t set(int k, bool f) const {
		if (f) return x | (T(1) << (k - 1));
		return x & ~(T(1) << (k - 1));
	}
	state_t sub(int k) const {return x & (T(1) << (k - 1));}

	state_t right_set(int k, bool f) const {
		if (f) return x | (x + 1);
		return x & (x - 1);
	}

	string convert() const {
		if (x == 0) return "0";
		string s;
		for (T p = x; p > 0; p >>= 1) s.push_back((p & 1) ^ 48);
		reverse(s.begin(), s.end());
		return s;
	}
};

using state16 = StateNum<short, 16>;
using state32 = StateNum<long, 32>;
using state64 = StateNum<long long, 64>;
using state128 = StateNum<__int128, 128>;

int count128(const state128& s) {
	int cnt = 0;
	for (__int128 p = s.x; p > 0; p &= -p) cnt++;
	return cnt;
}

inline void input128(state128& s) {
	s.x = 0; int t = 1; char ch = getchar();
	for (; ch < '0' || ch > '9'; ch = getchar())
		if (ch == '-') t = -1;
	for (; '0' <= ch && ch <= '9'; ch = getchar())
		s.x = (s.x << 1) + (s.x << 3) + (ch ^ 48);
	s.x *= t;
}

inline void output128(const state128& s) {
	static int st[40];
	__int128 x = s.x;
	if (x == 0) return putchar('0'), void();
	if (x < 0) putchar('-'), x = -x;
	int top = 0;
	do {
		st[top++] = x % 10, x /= 10;
	} while (x);
	while (top) putchar(st[--top] ^ 48);
}