QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#21429#2819. 摆家具gogoML 0ms0kbC++203.8kb2022-03-04 23:29:002022-05-08 03:15:46

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-08 03:15:46]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2022-03-04 23:29:00]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l); i <= (r); i ++)
#define per(i, r, l) for(int i = (r); i >= (l); i --)
#define trv(i, u, v) for(int i = head[u], v = e[i].to; i; v = e[i = e[i].nxt].to)
#define fi first
#define se second
#define all(s) s.begin(), s.end()
#define sz(s) (int)(s.size())
#define lb(s) ((s) & -(s))
#define pb push_back
using namespace std;

typedef long long ll;
typedef unsigned long long u64;
typedef pair<int, int> P;
mt19937_64 hua(time(0));
template<typename T> inline bool chkmx(T &x, T y) {return x < y ? x = y, 1 : 0;}
template<typename T> inline bool chkmn(T &x, T y) {return y < x ? x = y, 1 : 0;}
template<int T> using A = array<int, T>;

inline ll read() {
	ll x = 0, f = 1; char c = getchar();
	for(; !isdigit(c); c = getchar()) if(c == '-')  f = 0;
	for(; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	return f ? x : -x;
}
const int maxn = 1e6;
const int maxk = 20;
const int B = 1e5;
//const int B = 3e3; 
const int mod = 998244353;
int n, k, q, N, pw[maxk + 5], w[maxn + 5], val[maxk + 5];
int fac[maxk + 5], ifac[maxk + 5], inv[maxk + 5];
int f[maxk + 5][maxn + 5], g[maxn + 5], h[maxn + 5];
inline int add(int a, int b) {return (a += b) >= mod ? a - mod : a;}
inline int sub(int a, int b) {return (a -= b) < 0 ? a + mod : a;} 
inline int mul(int a, int b) {return 1ll * a * b % mod;}
inline void upd(int &a, int b) {a = add(a, b);}
inline int fpw(int a, int b) {
	int ans = 1;
	for(; b; b >>= 1, a = mul(a, a)) if(b & 1) ans = mul(ans, a);
	return ans;
}
struct Mat {
	u64 a[maxk + 1][maxk + 1];
	u64* operator [] (int x) {return a[x];}
	Mat() {memset(a, 0, sizeof a);}
	friend Mat operator * (Mat x, Mat y) {
		Mat z;
		rep(i, 0, k) rep(l, 0, k) rep(j, 0, k)  {
			z[i][j] += x[i][l] * y[l][j] % mod;
		}
		rep(i, 0, k) rep(j, 0, k) z[i][j] %= mod;
		return z;
	}
}mpw1[B + 5], mpw2[B + 5];
void init() {
	fac[0] = fac[1] = inv[0] = inv[1] = ifac[0] = ifac[1] = 1;
	rep(i, 2, maxk) fac[i] = mul(fac[i - 1], i);
	rep(i, 2, maxk) inv[i] = mul(inv[mod % i], mod - mod / i);
	rep(i, 2, maxk) ifac[i] = mul(ifac[i - 1], inv[i]); 
}
inline int c(int n, int m) {return mul(fac[n], mul(ifac[m], ifac[n - m]));} 
void write(int x) {
	if(x < 10) putchar(x + '0');
	else write(x / 10), putchar(x % 10 + '0');
}
int main() {
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	n = read(), k = read(), q = read();
	N = fpw(n, k);
	init();
	pw[0] = 1, val[0] = 1;
	rep(i, 1, k) pw[i] = pw[i - 1] * n, val[i] = val[i - 1] * (n - 1);
	rep(i, 0, k) val[i] = fpw(mul(c(k, i), val[i]), mod - 2);
	rep(i, 0, N - 1) f[0][i] = w[i] = read();
	rep(i, 0, k - 1) {
		per(j, k, 0) {	
			memcpy(g, f[j], sizeof g);
			memcpy(h, f[j], sizeof h);	
			per(s, N - 1, 0) {
				if(s / pw[i] % n != n - 1) {
					upd(g[s], g[s + pw[i]]);
				}
			}
			rep(s, 0, N - 1) {
				if(s / pw[i] % n != 0) {
					upd(h[s], h[s - pw[i]]);
				}
			}
			rep(s, 0, N - 1) {
				f[j][s] = 0;
				if(s / pw[i] % n != 0) upd(f[j][s], h[s - pw[i]]);
				if(s / pw[i] % n != n - 1) upd(f[j][s], g[s + pw[i]]);
				if(j) upd(f[j][s], f[j - 1][s]);
			}
		}
	}
	Mat bs;
	rep(i, 0, k) {
		bs[i][i] = mul(k - i, n - 2);
		mpw1[0][i][i] = mpw2[0][i][i] = 1;
	}
	rep(i, 1, k) {
		bs[i][i - 1] = mul(i, n - 1);
	}
	rep(i, 0, k - 1) {
		bs[i][i + 1] = k - i;
	}
	rep(i, 1, B) {
		mpw1[i] = mpw1[i - 1] * bs;
	}
	rep(i, 1, B) {
		mpw2[i] = mpw2[i - 1] * mpw1[B];
	}
//	return 0;
	ll lstans = 1;
	rep(i, 1, q) {
		ll a = read(), t = read() * lstans % mod;
		int x = t / B, y = t % B, ans = 0;
		rep(i, 0, k) {
			int ret = 0;
			rep(j, 0, k) {
				upd(ret, mul(mpw2[x][k][j], mpw1[y][j][i]));
			}
			upd(ans, mul(mul(ret, f[i][a]), val[k - i]));
		}
		cout << (lstans = ans) << '\n';
	}
	return 0;
}

详细

Test #1:

score: 0
Memory Limit Exceeded

input:

6 7 23333
691899807
511100503
969237388
938904792
350428599
320423686
227132681
288051232
833056445
128207711
81740114
850060462
194951182
130925016
505452669
304380595
703105541
640122112
75295756
128144693
482592240
835507949
68960843
807876145
6134269
259053259
386495729
193956365
370675670
12980...

output:


result: