QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290434#117. AsceticismMoRanSky100 ✓191ms19076kbC++203.9kb2023-12-24 23:58:112023-12-24 23:58:11

Judging History

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

  • [2023-12-24 23:58:11]
  • 评测
  • 测评结果:100
  • 用时:191ms
  • 内存:19076kb
  • [2023-12-24 23:58:11]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef vector<int> Poly;

const int N = 4e5 + 5;
int mod = 1e9 + 7;

int P = 1e9 + 7, G = 3;

int MOD[3] = { 998244353, 1004535809, 469762049 };

int n, k, A[N], rev[N], inv[N], fact[N], infact[N];
int lim = 1, len = 0, W[20][N];

int inline power(int a, int b, int Mod = P) {
	int res = 1;
	while (b) {
		if (b & 1) res = (LL)res * a % Mod;
		a = (LL)a * a % Mod;
		b >>= 1;
	}
	return res;
}

int Gi = power(G, P - 2, P), inv2 = power(2, P - 2, P);

void inline NTT(int c[], int lim, int o) {
	for (int i = 0; i < lim; i++)
		if (i < rev[i]) swap(c[i], c[rev[i]]);
	for (int k = 1, t = 0; k < lim; k <<= 1, t++) {
		for (int i = 0; i < lim; i += (k << 1)) {
			for (int j = 0; j < k; j++) {
				int u = c[i + j], v = (LL)c[i + k + j] * W[t][j] % P;
				c[i + j] = u + v >= P ? u + v - P : u + v;
				c[i + j + k] = u - v < 0 ? u - v + P : u - v; 
			}
		}
	}
	if (o == -1) {
		reverse(c + 1, c + lim);
		int inv = power(lim, P - 2, P);
		for (int i = 0; i < lim; i++)
			c[i] = 1ll * c[i] * inv % P;
	}
}


int inline C(int a, int b) {
	if (a < b) return 0;
	return (LL)fact[a] * infact[b] % P * infact[a - b] % P;
}

void inline setN(int n) {
	lim = 1, len = 0;
	while (lim < n) lim <<= 1, len++;
	for (int i = 0; i < lim; i++)
		rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
}

Poly inline NTT(Poly a, int o) {
	int n = a.size();
	for (int i = 0; i < n; i++) A[i] = a[i];
	NTT(A, lim, o);
	a.clear();
	for (int i = 0; i < lim; i++) a.push_back(A[i]), A[i] = 0;
	return a;
}

int Inv1 = power(MOD[0], MOD[1] - 2, MOD[1]);
int Inv2 = power((LL)MOD[0] * MOD[1] % MOD[2], MOD[2] - 2, MOD[2]);

int inline get(int a, int b, int c) {
	LL x = ((LL)b - a + MOD[1]) % MOD[1] * Inv1 % MOD[1] * MOD[0] + a;
	return ((c - x % MOD[2] + MOD[2]) * Inv2 % MOD[2] * MOD[0] % mod * MOD[1] + x) % mod;
}


void reset(int o) {
	P = o; Gi = power(G, P - 2, P), inv2 = power(2, P - 2, P);
}

Poly inline mul (Poly a, Poly b, int newn = -1) {
	if (newn == -1) newn = a.size() + b.size() - 1;
	setN(a.size() + b.size() - 1);
	Poly c = NTT(a, 1), d = NTT(b, 1);
	for (int i = 0; i < lim; i++) c[i] = (LL)c[i] * d[i] % P;
	d = NTT(c, -1); d.resize(newn);
	return d;
}

void inline init(int n) {
	setN(2 * n);
	for (int k = 1, t = 0; k < lim; k <<= 1, t++) {
		int wn = power(G, (P - 1) / (k << 1));
		W[t][0] = 1;
		for (int j = 1; j < k; j++) W[t][j] = (LL)W[t][j - 1] * wn % P;
	}
}

Poly inline Mul(Poly f, Poly g) {
	Poly a[3], b[3], c[3];
	for (int i = 0; i < 3; i++) {
		reset(MOD[i]);
		init(n * 2);
		a[i] = f, b[i] = g;
		c[i] = mul(a[i], b[i], n + 1);
	}
	Poly res;
	for (int i = 0; i < c[0].size(); i++) {
	//	cout << c[0][i] << " " << c[1][i] << " " << c[2][i] << endl;
		res.pb(get(c[0][i], c[1][i], c[2][i]));
	}
	P = 1e9 + 7;
	return res;
}

void inline factPrework(int n) {
	fact[0] = infact[0] = 1;
	for (int i = 1; i <= n; i++) fact[i] = (LL)fact[i - 1] * i % P;
	infact[n] = power(fact[n], P - 2);
	for (int i = n - 1; i; i--) infact[i] = infact[i + 1] * (i + 1ll) % P;
}

void inline preInv(int n) {
	inv[1] = 1;
	for (int i = 2; i <= n; i++)
		inv[i] = ((LL)P - P / i) * inv[P % i] % P;
}

Poly get() {
	Poly a(n + 1, 0), b(n + 1, 0);
	for (int i = 0; i <= n; i++) {
		a[i] = (LL)infact[i] * ((i & 1) ? P - 1 : 1) % P;
		b[i] = (LL)power(i, n) * infact[i] % P;
		//cout << a[i] << " " << b[i] << endl;
	}
	return Mul(a, b);
}


int main() {
	scanf("%d%d", &n, &k);
	preInv(n); factPrework(n);
	Poly a = get();
	for (int i = 1; i <= k; i++) a[i] = 1ll * a[i] * fact[i] % P;
	for (int i = 1; i < k; i++) {
		int t = ((k - i) & 1) ? 1 : P - 1;
		a[k] = (a[k] - 1ll * a[i] * C(n - i, k - i) % P * t % P + P) % P;
	}
	printf("%d\n", a[k]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 4
Accepted

Test #1:

score: 4
Accepted
time: 1ms
memory: 3712kb

input:

1 1

output:

1

result:

ok single line: '1'

Test #2:

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

input:

10 10

output:

1

result:

ok single line: '1'

Test #3:

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

input:

10 2

output:

1013

result:

ok single line: '1013'

Test #4:

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

input:

10 1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3788kb

input:

10 7

output:

455192

result:

ok single line: '455192'

Test #6:

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

input:

8 3

output:

4293

result:

ok single line: '4293'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3784kb

input:

9 6

output:

88234

result:

ok single line: '88234'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

3 3

output:

1

result:

ok single line: '1'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3716kb

input:

5 2

output:

26

result:

ok single line: '26'

Test #10:

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

input:

7 5

output:

1191

result:

ok single line: '1191'

Subtask #2:

score: 20
Accepted

Dependency #1:

100%
Accepted

Test #11:

score: 20
Accepted
time: 0ms
memory: 3720kb

input:

100 48

output:

491709703

result:

ok single line: '491709703'

Test #12:

score: 0
Accepted
time: 1ms
memory: 3864kb

input:

300 1

output:

1

result:

ok single line: '1'

Test #13:

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

input:

300 2

output:

322050458

result:

ok single line: '322050458'

Test #14:

score: 0
Accepted
time: 1ms
memory: 3748kb

input:

300 123

output:

562526258

result:

ok single line: '562526258'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3888kb

input:

300 295

output:

713150320

result:

ok single line: '713150320'

Test #16:

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

input:

300 300

output:

1

result:

ok single line: '1'

Test #17:

score: 0
Accepted
time: 1ms
memory: 3788kb

input:

199 28

output:

168902681

result:

ok single line: '168902681'

Test #18:

score: 0
Accepted
time: 1ms
memory: 3900kb

input:

253 152

output:

68568956

result:

ok single line: '68568956'

Test #19:

score: 0
Accepted
time: 1ms
memory: 3780kb

input:

135 124

output:

736486204

result:

ok single line: '736486204'

Test #20:

score: 0
Accepted
time: 1ms
memory: 3736kb

input:

13 11

output:

1479726

result:

ok single line: '1479726'

Subtask #3:

score: 25
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #21:

score: 25
Accepted
time: 0ms
memory: 3908kb

input:

1000 1

output:

1

result:

ok single line: '1'

Test #22:

score: 0
Accepted
time: 1ms
memory: 3892kb

input:

1000 2

output:

688422209

result:

ok single line: '688422209'

Test #23:

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

input:

1000 452

output:

103920245

result:

ok single line: '103920245'

Test #24:

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

input:

1000 504

output:

359395606

result:

ok single line: '359395606'

Test #25:

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

input:

1000 952

output:

419943092

result:

ok single line: '419943092'

Test #26:

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

input:

1000 998

output:

945760313

result:

ok single line: '945760313'

Test #27:

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

input:

1000 1000

output:

1

result:

ok single line: '1'

Test #28:

score: 0
Accepted
time: 1ms
memory: 3876kb

input:

603 536

output:

797752199

result:

ok single line: '797752199'

Test #29:

score: 0
Accepted
time: 1ms
memory: 3784kb

input:

194 115

output:

19316534

result:

ok single line: '19316534'

Test #30:

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

input:

995 965

output:

79761626

result:

ok single line: '79761626'

Subtask #4:

score: 51
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #31:

score: 51
Accepted
time: 186ms
memory: 19076kb

input:

100000 1

output:

1

result:

ok single line: '1'

Test #32:

score: 0
Accepted
time: 183ms
memory: 19028kb

input:

100000 5

output:

979545239

result:

ok single line: '979545239'

Test #33:

score: 0
Accepted
time: 179ms
memory: 18956kb

input:

100000 4532

output:

464105997

result:

ok single line: '464105997'

Test #34:

score: 0
Accepted
time: 191ms
memory: 18956kb

input:

100000 15064

output:

85875203

result:

ok single line: '85875203'

Test #35:

score: 0
Accepted
time: 188ms
memory: 18956kb

input:

100000 82952

output:

171676250

result:

ok single line: '171676250'

Test #36:

score: 0
Accepted
time: 184ms
memory: 18972kb

input:

100000 92998

output:

252841334

result:

ok single line: '252841334'

Test #37:

score: 0
Accepted
time: 189ms
memory: 18976kb

input:

100000 100000

output:

1

result:

ok single line: '1'

Test #38:

score: 0
Accepted
time: 22ms
memory: 5848kb

input:

16203 12361

output:

474349653

result:

ok single line: '474349653'

Test #39:

score: 0
Accepted
time: 172ms
memory: 18824kb

input:

72194 31523

output:

417377353

result:

ok single line: '417377353'

Test #40:

score: 0
Accepted
time: 176ms
memory: 19004kb

input:

99254 39532

output:

188863122

result:

ok single line: '188863122'

Extra Test:

score: 0
Extra Test Passed