競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// minimum_set_cover を、覆える集合を個数の小さい順に広げていく愚直な実装と比べる
// 使ってよい集合族が部分集合について閉じている場合は minimum_set_partition とも比べる
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/dp/set-cover.hpp"
#include <iostream>
#include <random>
#include <vector>
#include "../../../src/dp/set-partition.hpp"
int main() {
std::mt19937_64 rng(314);
bool ok = true;
for (int rep = 0; rep < 3000 && ok; rep++) {
int n = rng() % (rep < 2000 ? 9 : 13);
const int N = 1 << n;
std::vector<bool> feasible(N);
// 使ってよい集合の多さを変える (少ないと覆えないことがある)
const int density = 1 + rng() % 20;
for (int S = 0; S < N; S++) feasible[S] = static_cast<int>(rng() % 100) < density;
const bool closed = rep % 2 == 0;
if (closed) {
for (int S = N - 1; S > 0; S--)
if (feasible[S])
for (int T = S; T > 0; T = (T - 1) & S) feasible[T] = true;
}
// 愚直: k 個で覆える集合 (の和集合) の全体を、k を 1 つずつ増やして広げる
long long expect = n == 0 ? 0 : -1;
std::vector<bool> reach(N, false);
reach[0] = true;
std::vector<int> sets;
for (int S = 0; S < N; S++)
if (feasible[S]) sets.push_back(S);
for (int k = 1; k <= n && expect == -1; k++) {
std::vector<bool> next = reach;
for (int U = 0; U < N; U++)
if (reach[U])
for (int T : sets) next[U | T] = true;
reach = next;
if (reach[N - 1]) expect = k;
}
long long res = minimum_set_cover(n, feasible);
if (res != expect) ok = false, std::cerr << "minimum_set_cover " << rep << ' ' << res << ' ' << expect << std::endl;
if (closed && res != minimum_set_partition(n, feasible)) ok = false, std::cerr << "minimum_set_partition との違い" << std::endl;
}
if (ok) std::cout << "Hello World" << std::endl;
}#line 1 "test/unittest/dp/set-cover.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// minimum_set_cover を、覆える集合を個数の小さい順に広げていく愚直な実装と比べる
// 使ってよい集合族が部分集合について閉じている場合は minimum_set_partition とも比べる
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/dp/set-cover.hpp"
/**
* @file set-cover.hpp
* @brief 集合を「使ってよい集合」で覆うときの最小個数 (包除原理、要素が 20 個程度まで)
*/
#include <cassert>
#include <vector>
/**
* @brief {0, ..., n-1} を、feasible[T] が true の集合 T いくつかで (重なりを許して) 覆うときの最小個数 (覆えなければ -1) O(2^n n)
* feasible の大きさは 2^n。f(S) を S に含まれる使ってよい集合の数とすると、全体を覆う k 個組の数は Σ_S (-1)^{n - |S|} f(S)^k。
* これが 0 でない最小の k を求める。数は 2 つの素数の mod で数えるので、0 でないのに 0 と誤る確率がわずかにある
*/
inline long long minimum_set_cover(int n, const std::vector<bool>& feasible) {
assert(0 <= n && n < 31);
assert(static_cast<long long>(feasible.size()) == (1LL << n));
if (n == 0) return 0;
const int N = 1 << n;
// f[S] = S に含まれる使ってよい集合の数 (部分集合についてのゼータ変換)。2^n 以下なので mod を取らずに持てる
std::vector<unsigned> f(N);
for (int S = 0; S < N; S++) f[S] = feasible[S];
for (int i = 0; i < n; i++) {
for (int S = 0; S < N; S++) {
if (S >> i & 1) f[S] += f[S ^ (1 << i)];
}
}
if (f[N - 1] == 0) return -1;
static constexpr unsigned long long P[2] = {998244353, 1000000007};
// pw[j][S] = f[S]^k mod P[j]
std::vector<unsigned> pw[2] = {std::vector<unsigned>(N, 1), std::vector<unsigned>(N, 1)};
// 覆えるなら、各要素を含む集合を 1 つずつ選べば n 個以下で覆える
for (int k = 1; k <= n; k++) {
bool nonzero = false;
for (int j = 0; j < 2; j++) {
unsigned long long sum = 0;
for (int S = 0; S < N; S++) {
pw[j][S] = static_cast<unsigned>(pw[j][S] * static_cast<unsigned long long>(f[S]) % P[j]);
// n - |S| が奇数なら引く
sum += (__builtin_parity(S) ^ (n & 1)) ? P[j] - pw[j][S] : pw[j][S];
}
nonzero |= sum % P[j] != 0;
}
if (nonzero) return k;
}
return -1;
}
#line 8 "test/unittest/dp/set-cover.test.cpp"
#include <iostream>
#include <random>
#line 12 "test/unittest/dp/set-cover.test.cpp"
#line 2 "src/dp/set-partition.hpp"
/**
* @file set-partition.hpp
* @brief 集合を「使ってよい集合」に分けるときの最小個数 (要素が 16 個程度まで)
*/
#line 10 "src/dp/set-partition.hpp"
/**
* @brief {0, ..., n-1} を、feasible[T] が true の集合 T いくつかに重ならないように分けるときの最小個数 (分けられなければ -1) O(3^n)
* feasible の大きさは 2^n。dp[S] = min (dp[S \ T] + 1) を、T を「S の最小の要素を含む S の部分集合」に限って求める
* 使ってよい集合族が部分集合について閉じているなら、重なりを許して覆うときの最小個数とも一致する
*/
inline long long minimum_set_partition(int n, const std::vector<bool>& feasible) {
assert(static_cast<long long>(feasible.size()) == (1LL << n));
const int INF = n + 1;
std::vector<int> dp(1 << n, INF);
dp[0] = 0;
for (int S = 1; S < (1 << n); S++) {
const int low = S & -S, rest = S ^ low;
// rest の部分集合 U について T = U | low を試す
for (int U = rest;; U = (U - 1) & rest) {
const int T = U | low;
if (feasible[T] && dp[S ^ T] + 1 < dp[S]) dp[S] = dp[S ^ T] + 1;
if (U == 0) break;
}
}
return dp[(1 << n) - 1] >= INF ? -1 : dp[(1 << n) - 1];
}
#line 14 "test/unittest/dp/set-cover.test.cpp"
int main() {
std::mt19937_64 rng(314);
bool ok = true;
for (int rep = 0; rep < 3000 && ok; rep++) {
int n = rng() % (rep < 2000 ? 9 : 13);
const int N = 1 << n;
std::vector<bool> feasible(N);
// 使ってよい集合の多さを変える (少ないと覆えないことがある)
const int density = 1 + rng() % 20;
for (int S = 0; S < N; S++) feasible[S] = static_cast<int>(rng() % 100) < density;
const bool closed = rep % 2 == 0;
if (closed) {
for (int S = N - 1; S > 0; S--)
if (feasible[S])
for (int T = S; T > 0; T = (T - 1) & S) feasible[T] = true;
}
// 愚直: k 個で覆える集合 (の和集合) の全体を、k を 1 つずつ増やして広げる
long long expect = n == 0 ? 0 : -1;
std::vector<bool> reach(N, false);
reach[0] = true;
std::vector<int> sets;
for (int S = 0; S < N; S++)
if (feasible[S]) sets.push_back(S);
for (int k = 1; k <= n && expect == -1; k++) {
std::vector<bool> next = reach;
for (int U = 0; U < N; U++)
if (reach[U])
for (int T : sets) next[U | T] = true;
reach = next;
if (reach[N - 1]) expect = k;
}
long long res = minimum_set_cover(n, feasible);
if (res != expect) ok = false, std::cerr << "minimum_set_cover " << rep << ' ' << res << ' ' << expect << std::endl;
if (closed && res != minimum_set_partition(n, feasible)) ok = false, std::cerr << "minimum_set_partition との違い" << std::endl;
}
if (ok) std::cout << "Hello World" << std::endl;
}