結果

問題 No.3486 Draw a Rainbow
コンテスト
ユーザー C
提出日時 2026-12-02 16:08:42
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 24 ms / 4,000 ms
+ 485µs
コード長 5,726 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,899 ms
コンパイル使用メモリ 423,860 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-12-02 16:08:56
合計ジャッジ時間 12,501 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC optimize("O3")
#if defined(__x86_64__) || defined(__i386__)
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#endif
#include <bits/stdc++.h>
#include <atcoder/all>
#define each(x, v) for (auto&& x : v)
#define each2(x, y, v) for (auto&& [x, y] : v)
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
using namespace std;
using namespace atcoder;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vi>;
using pll=pair<ll,ll>;
using pi=pair<int,int>;

// using mint=modint998244353;
// using mint=modint1000000007;
// using mint2=static_modint<2>;

#line 2 "modint/montgomery-modint.hpp"

template <uint32_t mod>
struct LazyMontgomeryModInt {
  using mint = LazyMontgomeryModInt;
  using i32 = int32_t;
  using u32 = uint32_t;
  using u64 = uint64_t;

  static constexpr u32 get_r() {
    u32 ret = mod;
    for (i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret;
    return ret;
  }

  static constexpr u32 r = get_r();
  static constexpr u32 n2 = -u64(mod) % mod;
  static_assert(mod < (1 << 30), "invalid, mod >= 2 ^ 30");
  static_assert((mod & 1) == 1, "invalid, mod % 2 == 0");
  static_assert(r * mod == 1, "this code has bugs.");

  u32 a;

  constexpr LazyMontgomeryModInt() : a(0) {}
  constexpr LazyMontgomeryModInt(const int64_t &b)
      : a(reduce(u64(b % mod + mod) * n2)){};

  static constexpr u32 reduce(const u64 &b) {
    return (b + u64(u32(b) * u32(-r)) * mod) >> 32;
  }

  constexpr mint &operator+=(const mint &b) {
    if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod;
    return *this;
  }

  constexpr mint &operator-=(const mint &b) {
    if (i32(a -= b.a) < 0) a += 2 * mod;
    return *this;
  }

  constexpr mint &operator*=(const mint &b) {
    a = reduce(u64(a) * b.a);
    return *this;
  }

  constexpr mint &operator/=(const mint &b) {
    *this *= b.inverse();
    return *this;
  }

  constexpr mint operator+(const mint &b) const { return mint(*this) += b; }
  constexpr mint operator-(const mint &b) const { return mint(*this) -= b; }
  constexpr mint operator*(const mint &b) const { return mint(*this) *= b; }
  constexpr mint operator/(const mint &b) const { return mint(*this) /= b; }
  constexpr bool operator==(const mint &b) const {
    return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
  }
  constexpr bool operator!=(const mint &b) const {
    return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
  }
  constexpr mint operator-() const { return mint() - mint(*this); }
  constexpr mint operator+() const { return mint(*this); }

  constexpr mint pow(u64 n) const {
    mint ret(1), mul(*this);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  constexpr mint inverse() const {
    int x = get(), y = mod, u = 1, v = 0, t = 0, tmp = 0;
    while (y > 0) {
      t = x / y;
      x -= t * y, u -= t * v;
      tmp = x, x = y, y = tmp;
      tmp = u, u = v, v = tmp;
    }
    return mint{u};
  }

  friend ostream &operator<<(ostream &os, const mint &b) {
    return os << b.get();
  }

  friend istream &operator>>(istream &is, mint &b) {
    int64_t t;
    is >> t;
    b = LazyMontgomeryModInt<mod>(t);
    return (is);
  }

  constexpr u32 get() const {
    u32 ret = reduce(a);
    return ret >= mod ? ret - mod : ret;
  }

  static constexpr u32 get_mod() { return mod; }
};

using mint=LazyMontgomeryModInt<998244353>;
using vm=vector<mint>;
using vvm=vector<vm>;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	/**
	 * O(N+l*2^m) m = min(M,L), l = max(M,L)
	 * 
	 * 以下の多重二部グラフを考えます:
	 * 左の頂点は yuki 国の色(この集合をXとする)
	 * 右の頂点は coder 国の色(この集合をYとする)
	 * ペン i は (A_i,B_i) の辺とみなします
	 * 
	 * 答えは辺被覆の数となります。
	 *
	 * ここで S \subseteq X の時
	 * 
	 * G(S) =  # {C : C は(S,Y)の辺被覆である}
	 * 
	 * と定義する
	 * 
	 * 求める答えは G(X) です。
	 * 
	 * でも G(S) を式として直接表すは難しいので、G の ゼータ変換 F を考えます :
	 * 
	 * F(S) =  # {C : C はYの辺被覆であって、全て (u,v) \in C に対して u \in S}
	 * 
	 * 定義から 
	 * 
	 * F(S) = \sum_{T \subseteq S} G(T) (ゼータ変換)
	 * 
	 * G(S) = \sum_{T \subseteq S} (-1)^{|S|-|T|} F(T) (メビウス変換)
	 * 
	 * が成り立つ。
	 * 
	 * 実際、 F は G と違い、簡単な式で表せます:
	 * 
	 * 次数 d_j(S) = \sum_{i \in S} c_{ij} , i\in X, j\in Y (c_{ij} は i,j 間の辺数)の時
	 * 
	 * F(S) = \Prod_{j\in Y} (2^{d_j(S)} - 1)
	 * 
	 * となります。
	 * 
	 * なので、メビウス変換より答えは 
	 * 
	 * G(X) = \sum_{T \subseteq X} (-1)^{|X|-|T|} \Prod_{j\in Y} (2^{d_j(T)} - 1)
	 * 
	 * 残りは、d_j(S) を頑張って計算することのみ。
	 */
	int N,M,L;
	cin>>N>>M>>L;
	int m,l;
	vvi c;
	if(M<=L){
		m=M,l=L;
		c=vvi(m,vi(l));
		rep(k,0,N){
			int a,b;
			cin>>a>>b;
			--a;
			b-=M+1;
			c[a][b]++;
		}
	}else{
		m=L,l=M;
		c=vvi(m,vi(l));
		rep(k,0,N){
			int a,b;
			cin>>a>>b;
			--a;
			b-=M+1;
			c[b][a]++;
		}
	}

	vm P(N+1);
	rep(k,0,N)P[k+1]=P[k]+P[k]+1;

	int K=1<<m;
	vm F(K,1);
	vi d(K);
	rep(j,0,l){
		rep(i,0,m){
			int k=1<<i,x=c[i][j];
			rep(S,0,k)d[S+k]=d[S]+x;
		}
		rep(S,0,K)F[S]*=P[d[S]];
	}

	mint ans=0;
	rep(S,0,K){
		if((m-__builtin_popcount(S))&1)ans-=F[S];
		else ans+=F[S];
	}
	cout<<ans<<'\n';

}
0