QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#101339#5031. 核zhoukangyang30 1764ms22936kbC++173.5kb2023-04-29 10:41:192023-04-29 10:41:22

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-29 10:41:22]
  • 评测
  • 测评结果:30
  • 用时:1764ms
  • 内存:22936kb
  • [2023-04-29 10:41:19]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define ull unsigned long long 
#define bs bitset < N >
using namespace std;
const int N = 1e6 + 7;
int n, q, mod;
int pw[N], fac[N], ifac[N];
int qpow(int x, int y = mod - 2) {
	int res = 1;
	for(; y; x = (ll) x * x % mod, y >>= 1) if(y & 1) res = (ll) res * x % mod;
	return res;
}
void q_init(int x) {
	pw[0] = 1;
	L(i, 1, x) pw[i] = (ll) pw[i - 1] * q % mod;
	L(i, 1, x) fac[i] = pw[i] - 1;
	fac[0] = ifac[0] = 1;
	L(i, 1, x) 
		ifac[i] = (ll) qpow (fac[i]) * ifac[i - 1] % mod, 
		fac[i] = (ll) fac[i - 1] * fac[i] % mod;
} 
int q_C(int x, int y) {
	return x < y || y < 0 ? 0 : (ll) fac[x] * ifac[y] % mod * ifac[x - y] % mod;
}
inline int sgn(int x) {
	return (x & 1) ? mod - 1 : 1;
}

template < int L, int K > struct DBM {
	static const int R = (L + 1) * (K + 1);
	
	int lpos, a[R * 10 + 7];
	int G[R + 7][R + 7], cur[R + 7], ns[R + 7];
	int f[L + 2][K + 2], id[L + 2][K + 2], idt;
	inline void insert() {
		R(i, idt, 1) if(cur[i]) {
			if(!G[i][i]) {
				L(j, 1, i) 
					G[i][j] = cur[j];
			} 
			int iv = (ll) (mod - cur[i]) * qpow(G[i][i]) % mod;
			L(j, 1, i) 
				(cur[j] += (ll) iv * G[i][j] % mod) %= mod;
		}
	}
	void gauss(int *a, int n, int lpos = 0) {
		n = min(n, R * 10), idt = 0;
		L(i, 0, L) 
			L(j, 0, K) 
				id[i][j] = ++idt;
		L(i, L + 1, n) {
			L(j, 0, idt) 
				cur[j] = 0;
			L(x, 0, L) if(x < i) {
				int pw = a[i - x];
				L(y, 0, K) 
					cur[id[x][y]] = pw, 
					pw = (ll) pw * ::pw[lpos + i] % mod;
			}
			insert();
		}
		int pos = 1;
		while(G[pos][pos]) ++pos;
		ns[pos] = 1;
		if(pos > idt) {
			cout << "TAT" << endl;
			exit(0);
		}
		L(i, pos + 1, idt) {
			int s = 0;
			L(j, 1, i - 1) {
				(s += (ll) G[i][j] * ns[j] % mod) %= mod; 
			}
			ns[i] = (ll) (mod - s) * qpow(G[i][i]) % mod;
		}
		L(i, 0, L) 
			L(j, 0, K) 
				f[i][j] = ns[id[i][j]];
		L(i, L + 1, n) {
			L(j, 0, idt) 
				cur[j] = 0;
			int s = 0;
			L(x, 0, L) if(x < i) {
				int pw = a[i - x];
				L(y, 0, K) 
					(s += (ll) pw * f[x][y] % mod) %= mod, 
					pw = (ll) pw * ::pw[lpos + i] % mod;
			}
		}
	}
};

DBM < 2, 3 > S;

const int T = 80;
int f[N], g[N], ans[N];
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> q >> mod;
	q_init(n + 1);
	int mn = min(n, T);
	f[0] = 1;
	L(i, 1, mn) {
		f[i] = (ll) f[i - 1] * (pw[n] + mod - pw[n - i]) % mod;
	}
	L(i, 0, mn) {
		g[i] = (ll) sgn(i) * qpow(q, i * (i - 1) / 2) % mod;
	}
	L(i, 0, mn) {
		L(j, 0, mn - i) {
			(ans[i + j] += (ll) q_C(i + j, i) * f[i] % mod * g[j] % mod) %= mod; 
		}
	}
	S.gauss(ans, T);
	L(i, T + 1, n) {
		int s = 0;
		L(x, 1, 2) {
			int pw = ans[i - x];
			L(y, 0, 3) 
				(s += (ll) pw * S.f[x][y] % mod) %= mod, pw = (ll) pw * ::pw[i] % mod;
		}
		int fs = 0, pw = 1;
		L(y, 0, 3) 
			(fs += (ll) pw * S.f[0][y] % mod) %= mod, pw = (ll) pw * ::pw[i] % mod;
		ans[i] = (ll) (mod - s) * qpow(fs) % mod;
	}
	L(i, 0, n) {
		f[i] = ans[i];
	}
	L(i, 0, n) {
		f[i] = (ll) f[i] * q_C(n, i) % mod;
	}
	int pw = 1;
	L(i, 1, n) {
		pw = (ll) pw * q % (mod - 1);
	}
	int s = 1, ns = 0;
	R(i, n, 0) {
		(ns += (ll) f[i] * qpow(3, s) % mod) %= mod;
		s = (ll) s * pw % (mod - 1);
	}
	cout << ns << '\n';
	return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 3412kb

input:

1 2 540053233

output:

9

result:

ok 1 number(s): "9"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3536kb

input:

2 2 156542707

output:

43046970

result:

ok 1 number(s): "43046970"

Test #3:

score: 0
Accepted
time: 2ms
memory: 3440kb

input:

1 2 186225229

output:

9

result:

ok 1 number(s): "9"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

3 3 109884329

output:

100602209

result:

ok 1 number(s): "100602209"

Test #5:

score: 0
Accepted
time: 2ms
memory: 3488kb

input:

1 2 144802297

output:

9

result:

ok 1 number(s): "9"

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #6:

score: 0
Wrong Answer
time: 2ms
memory: 3488kb

input:

20 21992843 328859143

output:

TAT

result:

wrong output format Expected integer, but "TAT" found

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 20
Accepted

Test #26:

score: 20
Accepted
time: 1764ms
memory: 22936kb

input:

998818 198334853 998244353

output:

153251445

result:

ok 1 number(s): "153251445"

Test #27:

score: 0
Accepted
time: 1618ms
memory: 21348kb

input:

914379 128814383 998244353

output:

477606145

result:

ok 1 number(s): "477606145"

Test #28:

score: 0
Accepted
time: 1663ms
memory: 21824kb

input:

944474 478445339 998244353

output:

174204073

result:

ok 1 number(s): "174204073"

Test #29:

score: 0
Accepted
time: 1695ms
memory: 22008kb

input:

948637 711592127 998244353

output:

178256114

result:

ok 1 number(s): "178256114"

Test #30:

score: 0
Accepted
time: 1627ms
memory: 21492kb

input:

927564 14465663 998244353

output:

315244613

result:

ok 1 number(s): "315244613"

Test #31:

score: 0
Accepted
time: 1654ms
memory: 21684kb

input:

934615 392799073 998244353

output:

892700270

result:

ok 1 number(s): "892700270"

Test #32:

score: 0
Accepted
time: 1595ms
memory: 21296kb

input:

917196 124972031 998244353

output:

782017412

result:

ok 1 number(s): "782017412"

Test #33:

score: 0
Accepted
time: 1704ms
memory: 22140kb

input:

957149 392606173 998244353

output:

159348443

result:

ok 1 number(s): "159348443"

Test #34:

score: 0
Accepted
time: 1756ms
memory: 22852kb

input:

997042 184649453 998244353

output:

464643024

result:

ok 1 number(s): "464643024"

Test #35:

score: 0
Accepted
time: 1758ms
memory: 21968kb

input:

953353 14071961 998244353

output:

391688875

result:

ok 1 number(s): "391688875"

Subtask #5:

score: 0
Runtime Error

Test #36:

score: 0
Runtime Error

input:

9909956 720431399 720431401

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%