QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404170#6317. XOR Tree Pathcomeintocalm#TL 0ms0kbC++208.2kb2024-05-03 14:50:042024-05-03 14:50:05

Judging History

你现在查看的是最新测评结果

  • [2024-05-03 14:50:05]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-05-03 14:50:04]
  • 提交

answer

#include<bits/stdc++.h>
const int p = 998244353;
typedef long long LL;
using namespace std;
const int mod = p;

template<int mod>
struct ModInt {
#define T (*this)
	int x;
	ModInt() : x(0) {}
	ModInt(int y) : x(y >= 0 ? y : y + mod) {}
	ModInt(LL y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	inline int inc(const int &v) {
		return v >= mod ? v - mod : v;
	}
	inline int dec(const int &v) {
		return v < 0 ? v + mod : v;
	}
	inline ModInt &operator+=(const ModInt &p) {
		x = inc(x + p.x);
		return T;
	}
	inline ModInt &operator-=(const ModInt &p) {
		x = dec(x - p.x);
		return T;
	}
	inline ModInt &operator*=(const ModInt &p) {
		x = (int)((LL)x * p.x % mod);
		return T;
	}
	inline ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0)t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
		return u;
	}
	inline ModInt &operator/=(const ModInt &p) {
		T *= p.inverse();
		return T;
	}
	inline ModInt operator-() const {
		return -x;
	}
	inline friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {
		return ModInt(lhs) += rhs;
	}
	inline friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {
		return ModInt(lhs) -= rhs;
	}
	inline friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {
		return ModInt(lhs) *= rhs;
	}
	inline friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {
		return ModInt(lhs) /= rhs;
	}
	inline bool operator==(const ModInt &p) const {
		return x == p.x;
	}
	inline bool operator!=(const ModInt &p) const {
		return x != p.x;
	}
	ModInt qpow(LL n) const {
		ModInt ret(1), mul(x);
		while (n > 0) {
			if (n & 1)ret *= mul;
			mul *= mul, n >>= 1;
		}
		return ret;
	}
	inline friend ostream &operator<<(ostream &os, const ModInt &p) {
		return os << p.x;
	}
	inline friend istream &operator>>(istream &is, ModInt &a) {
		LL t;
		is >> t, a = ModInt<mod>(t);
		return is;
	}
	static int get_mod() {
		return mod;
	}
#undef T
};


using Z = ModInt<mod>;
namespace NTT {
	vector<int> rev;
	vector<Z> roots{0, 1};
	inline void dft(vector<Z> &a) {
		int n = (int)(a.size());
		if (rev.size() != n) {
			int k = __builtin_ctz(n) - 1;
			rev.resize(n);
			for (int i = 0; i < n; i++)rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
		}
		for (int i = 0; i < n; i++)if (rev[i] < i)swap(a[i], a[rev[i]]);
		if (roots.size() < n) {
			int k = __builtin_ctz(roots.size());
			roots.resize(n);
			while ((1 << k) < n) {
				Z e = Z(3).qpow((mod - 1) >> (k + 1));
				for (int i = 1 << (k - 1); i < (1 << k); i++)
					roots[i << 1] = roots[i], roots[i << 1 | 1] = roots[i] * e;
				k++;
			}
		}
		for (int k = 1; k < n; k <<= 1) {
			for (int i = 0; i < n; i += k << 1) {
				for (int j = 0; j < k; j++) {
					Z u = a[i + j], v = a[i + j + k] * roots[k + j];
					a[i + j] = u + v, a[i + j + k] = u - v;
				}
			}
		}
	}
	inline void idft(vector<Z> &a) {
		int n = (int)(a.size());
		reverse(a.begin() + 1, a.end()), dft(a);
		Z inv = Z(n).inverse();
		for (int i = 0; i < n; i++)a[i] = a[i] * inv;
	}
}


struct Poly : public vector<Z> {
#define T (*this)
	using vector<Z>::vector;
	inline int deg() const {
		return (int)(size());
	}
	inline Z operator[](const int &idx) const {
		if (idx < 0 || idx >= deg())return Z(0);
		return at(idx);
	}
	inline Z &operator[](const int &idx) {
		return at(idx);
	}
	inline Poly &operator^=(const Poly &b) {
		if (b.deg() < deg())resize(b.deg());
		for (int i = 0, sz = deg(); i < sz; i++)T[i] *= b[i];
		return T;
	}
	inline Poly &operator<<=(const int &k) {
		return insert(begin(), k, Z(0)), T;
	}
	inline Poly operator<<(const int &r) const {
		return Poly(T) <<= r;
	}
	inline Poly operator>>(const int &r) const {
		return r >= deg() ? Poly() : Poly(begin() + r, end());
	}
	inline Poly &operator>>=(const int &r) {
		return T = T >> r;
	}
	inline Poly mod(const int &k) const {
		return k < deg() ? Poly(begin(), begin() + k) : T;
	}
	inline friend Poly operator*(const Z &a, Poly b) {
		for (auto &x: b)x *= a;
		return b;
	}
	inline friend Poly operator*(Poly b, const Z &a) {
		for (auto &x: b)x *= a;
		return b;
	}
	inline friend Poly operator*(Poly a, Poly b) {
		if (a.empty() || b.empty())return {};
		int sz = 1, tot = a.deg() + b.deg() - 1;
		while (sz < tot)sz <<= 1;
		a.resize(sz), b.resize(sz);
		NTT::dft(a), NTT::dft(b);
		for (int i = 0; i < sz; i++)a[i] *= b[i];
		NTT::idft(a), a.resize(tot);
		return a;
	}
	inline Poly &operator*=(const Poly &b) {
		return T = T * b;
	}
	inline friend Poly operator+(const Poly &a, const Poly &b) {
		int n = (int)max(a.size(), b.size());
		Poly c;
		c.resize(n);
		for (int i = 0, sz = (int)a.size(); i < sz; i++)c[i] = a[i];
		for (int i = 0, sz = (int)b.size(); i < sz; i++)c[i] += b[i];
		return c;
	}
	inline friend Poly operator-(const Poly &a, const Poly &b) {
		int n = (int)max(a.size(), b.size());
		Poly c;
		c.resize(n);
		for (int i = 0, sz = (int)a.size(); i < sz; i++)c[i] = a[i];
		for (int i = 0, sz = (int)b.size(); i < sz; i++)c[i] -= b[i];
		return c;
	}
	inline Poly derivation() const {
		if (T.empty())return {};
		int n = (int)(T.size());
		Poly c;
		c.resize(n - 1);
		for (int i = 0; i < n - 1; i++)c[i] = T[i + 1] * (i + 1);
		return c;
	}
	inline Poly integration() const {
		int n = (int)(T.size());
		Poly c;
		c.resize(n + 1);
		for (int i = 0; i < n; i++)c[i + 1] = T[i] * Z(i + 1).inverse();
		return c;
	}
	inline Poly inv(const int &m) const {
		Poly c{T[0].inverse()};
		int k = 1;
		while (k < m)k <<= 1, c = (c * (Poly{2} - T.mod(k) * c)).mod(k);
		return c.mod(m);
	}
	inline Poly log(const int &m) const {
		return (derivation() * inv(m)).integration().mod(m);
	}
	inline Poly exp(const int &m) const {
		Poly x{1};
		int k = 1;
		while (k < m)k <<= 1, x = (x * (Poly{1} - x.log(k) + mod(k))).mod(k);
		return x.mod(m);
	}
	inline Poly pow(const int &k, const int &m) const {
		int i = 0;
		while (i < T.size() && T[i] == Z(0))i++;
		if (i == T.size() || (LL)i * k >= m)return Poly(m);
		Z v = T[i];
		auto g = (T >> i) * (v.inverse());
		return ((g.log(m - i * k) * Z(k)).exp(m - i * k) << (i * k)) * v.qpow(k);
	}
	inline Poly sqrt(const int &m) const {
		Poly x{1};
		int k = 1;
		while (k < m)k <<= 1, x = (x + (mod(k) * x.inv(k)).mod(k)) * Z(2).inverse();
		return x.mod(m);
	}
	inline Poly rev() const {
		return Poly(rbegin(), rend());
	}
	inline Poly mulT(const Poly &b) const {
		return T * b.rev() >> (b.deg() - 1);
	}
	inline vector<Z> eval(vector<Z> x) const {
		if (T.empty())return vector<Z>(x.size(), Z(0));
		int n = max((int)(x.size()), (int)(T.size()));
		vector<Poly> q(4 * n);
		vector<Z> ans(x.size());
		x.resize(n);
		std::function<void(int, int, int)> build = [&](int rt, int l, int r) {
			if (l == r) {
				q[rt] = {Z(1), -x[l]};
				return;
			}
			int mid = (l + r) >> 1;
			build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
			q[rt] = q[rt << 1] * q[rt << 1 | 1];
		};
		build(1, 0, n - 1);
		std::function<void(int, int, int, const Poly &)> work = [&](int rt, int l, int r, const Poly &num) {
			if (l == r) {
				if (l < (int)(ans.size()))ans[l] = num[0];
				return;
			}
			int mid = (l + r) >> 1;
			work(rt << 1, l, mid, num.mulT(q[rt << 1 | 1]).mod(mid - l + 1));
			work(rt << 1 | 1, mid + 1, r, num.mulT(q[rt << 1]).mod(r - mid));
		};
		work(1, 0, n - 1, mulT(q[1].inv(n)));
		return ans;
	}
#undef T
};


const int N = 1e6+7;
Z ifac[N], fac[N];

Z binom(int n, int m) {
  if(!m) return 1;
  if(!n || n < m) return 0;
  return fac[n] * ifac[m] * ifac[n - m];
}
Z iep(int x) {
  if(x & 1) return Z(998244352);
  return Z(1);
}

int main() {
  int n, m;
  cin >> n >> m;
  int up = (n * m);
  fac[0] = ifac[0] = Z(1);
  const int M = 1e6;
  for(int i = 1; i <= M; i++) {
    fac[i] = fac[i - 1] * i;
  }
  ifac[M] = fac[M].inverse();
  for(int i = M - 1; i >= 1; i--) {
    ifac[i] = ifac[i + 1] * (i + 1);
  }

  //cout << binom(6, 3).x;

  Poly F;
  for(int i = 0; i <= m; i++) {
    Z res = binom(m, i) * iep(i) * ifac[m - i];
    F.push_back(res);
  }

  Poly G = F.pow(n,  1000007);

  Z ans(0);
  for(int i = 0; i <= up; i++) {
    ans += fac[up - i] * G[i];
  }

  cout << ans.x << "\n";
return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

5
1 0 0 1 0
1 2
1 3
3 4
3 5

output:


result: