結果
問題 |
No.1482 Swap Many Permutations
|
ユーザー |
👑 |
提出日時 | 2025-05-31 11:08:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 35 ms / 2,000 ms |
コード長 | 10,004 bytes |
コンパイル時間 | 1,224 ms |
コンパイル使用メモリ | 117,560 KB |
実行使用メモリ | 7,848 KB |
最終ジャッジ日時 | 2025-05-31 11:09:02 |
合計ジャッジ時間 | 6,189 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 45 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:239:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 239 | scanf("%u%u", &B[i].x, &C[i].x); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <chrono> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") //////////////////////////////////////////////////////////////////////////////// template <unsigned M_> struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; //////////////////////////////////////////////////////////////////////////////// #define ModInt ModIntR //////////////////////////////////////////////////////////////////////////////// // Barrett struct ModInt { static unsigned M; static unsigned long long NEG_INV_M; static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; } unsigned x; ModInt() : x(0U) {} ModInt(unsigned x_) : x(x_ % M) {} ModInt(unsigned long long x_) : x(x_ % M) {} ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {} ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { const unsigned long long y = static_cast<unsigned long long>(x) * a.x; const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64); const unsigned long long r = y - M * q; x = r - M * (r >= M); return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; unsigned ModInt::M; unsigned long long ModInt::NEG_INV_M; // !!!Use ModInt::setM!!! //////////////////////////////////////////////////////////////////////////////// #undef ModInt using Mint998 = ModInt<998244353>; using MintM = ModIntR; constexpr int LIM_INV = 200'010; Mint998 inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV]; void prepare() { inv[1] = 1; for (int i = 2; i < LIM_INV; ++i) { inv[i] = -((Mint998::M / i) * inv[Mint998::M % i]); } fac[0] = invFac[0] = 1; for (int i = 1; i < LIM_INV; ++i) { fac[i] = fac[i - 1] * i; invFac[i] = invFac[i - 1] * inv[i]; } } Mint998 binom(Int n, Int k) { if (n < 0) { if (k >= 0) { return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k); } else if (n - k >= 0) { return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k); } else { return 0; } } else { if (0 <= k && k <= n) { assert(n < LIM_INV); return fac[n] * invFac[k] * invFac[n - k]; } else { return 0; } } } //////////////////////////////////////////////////////////////////////////////// // M: prime MintM primitiveRoot() { const unsigned M = MintM::M; vector<unsigned> ds; { unsigned m = M - 1; for (unsigned q = 2; q * q <= m; ++q) if (m % q == 0) { do { m /= q; } while (m % q == 0); ds.push_back((M - 1) / q); } if (m > 1) ds.push_back((M - 1) / m); } for (unsigned a = 1; a < M; ++a) { const MintM g = a; if ([&]() -> bool { for (const unsigned d : ds) if (g.pow(d).x == 1) return false; return true; }()) { return g; } } assert(false); } /* >= 3 perms p q p^-1 q^-1 p p^-1 q q^-1 generates A_M parity of (u -> b + c u) M = 2: (b, c) = (0, 1): even (b, c) = (1, 1): odd M != 2: c = 1: b = 0: M cycles; even b != 0: 1 cycle: even c != 1: 1 fixed point (M-1)/ord(c) cycles of size ord(c) even <=> c: quad. residue L := M(M-1)/2 all even candidates all odd candidates (1+x)^L (1+tx)^L mod (1-t^2) = (1/2) ((1+x)^2L +- (1+x)^L (1-x)^L) */ int N, M; vector<MintM> B, C; MintM G; vector<MintM> Exp; vector<int> Log; int parity(MintM b, MintM c) { if (M == 2) return b.x; return Log[c.x] & 1; } int main() { prepare(); for (; ~scanf("%d%d", &N, &M); ) { MintM::setM(M); B.resize(N); C.resize(N); for (int i = 0; i < N; ++i) { scanf("%u%u", &B[i].x, &C[i].x); } G = primitiveRoot(); Exp.resize(M); Exp[0] = 1; for (int i = 1; i < M; ++i) Exp[i] = Exp[i - 1] * G; Log.assign(M, -1); for (int i = 0; i < M - 1; ++i) Log[Exp[i].x] = i; // cerr<<"Exp = "<<Exp<<endl; const Int L = (Int)M*(M-1) / 2; vector<Mint998> fs(N + 1, 0), gs(N + 1, 0); { Mint998 bn = 1; for (int i = 0; i <= N; ++i) { fs[i] = bn; bn *= (2*L - i) * inv[1 + i]; } } { Mint998 bn = 1; for (int i = 0; i <= N/2; ++i) { gs[2*i] = bn * (i&1?-1:+1); bn *= (L - i) * inv[1 + i]; } } // cerr<<"fs = "<<fs<<endl; // cerr<<"gs = "<<gs<<endl; int now = 0; for (int n = 1; n <= N; ++n) { const int s = parity(B[n - 1], C[n - 1]); // cerr<<B[n-1]<<" "<<C[n-1]<<": "<<s<<endl; now ^= s; Mint998 ans; if (n == 1) { ans = 1; } else if (n == 2) { ans = (B[0] == B[1] && C[0] == C[1]) ? 0 : ((Int)M*(M-1)); } else { ans = now ? (fs[n] - gs[n]) : (fs[n] + gs[n]); ans /= 2; ans *= fac[n]; } printf("%u\n", ans.x); } } return 0; }