QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19442#1810. Generate the SequencesYaoBIG#WA 24ms3704kbC++201.9kb2022-01-31 02:47:332022-05-06 05:22:26

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-06 05:22:26]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:3704kb
  • [2022-01-31 02:47:33]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;

constexpr ll modPow(ll a, ll b, ll c) {
	return (b&1) ? (a * modPow(a, b-1, c) % c)
		: (b ? modPow(a*a % c, b/2, c) : 1);
}

struct FactPrecalc {
	private:
		const int P;
		vector<int> fact, inv_fact;
	public:
		FactPrecalc(int n, int p) : P(p), fact(n+1, 1), inv_fact(n+1, 1) {
			for (int i = 2; i <= n; ++i) fact[i] = (ll)fact[i-1] * i % P;
			inv_fact[n] = modPow(fact[n], P-2, P);
			for (int i = n-1; i >= 0; --i) inv_fact[i] = (ll)inv_fact[i+1] * (i+1) % P;
		}

		ll operator()(ll a, ll b) const {
			if (a < b) return 0;
			ll div = (ll)inv_fact[a-b] * inv_fact[b] % P;
			return div * fact[a] % P;
		}
		ll operator[](int i) const { return fact[i]; }
		ll inv(int i) const { return inv_fact[i]; }
};

// Assume first we cannot put 1 after a 1, or a m after a m.
// DP[s][i][k]: ways to get to a sequence of size s, where last value is {1, m}[i] and there are k gaps [1, m]
//	-> Since there are k gaps, there are also s - (k+1) elements in them. The total size of the gaps is (m-2) * k.
//		-> DP[s+1][i][k] += DP[s][i][k] * ((m-2) * k - (s - (k+1)))
//	-> We may create a new gap, there is a unique way of doing this
//		-> DP[s+1][i ^ 1][k + 1] += DP[s][i][k]

const int N = 3000;
ll dp[N];
ll nxt[N];

int main() {
	int n;
	ll m;
	cin >> n >> m;
	++n;

	FactPrecalc binom(n, MOD);
	
	dp[0] = 1;
	ll res = 0;
	for (int s = 1; s < n; ++s) {
		for (int k = 0; k < s; ++k) {
			ll mult = (m-2) * k - (s - (k+1));
			if (mult > 0) nxt[k] = (nxt[k] + dp[k] * (mult % MOD)) % MOD;
			nxt[k+1] = (nxt[k+1] + dp[k]) % MOD;
		}
		for (int k = 0; k <= s; ++k) {
			res = (res + binom(n-1, n-s) * dp[k]) % MOD;
			dp[k] = nxt[k];
			nxt[k] = 0;
		}
	}
	for (int k = 0; k < n; ++k) res = (res + dp[k]) % MOD;

	cout << res << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3648kb

input:

2 3

output:

5

result:

ok answer is '5'

Test #2:

score: 0
Accepted
time: 3ms
memory: 3628kb

input:

1024 52689658

output:

654836147

result:

ok answer is '654836147'

Test #3:

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

input:

1 2

output:

2

result:

ok answer is '2'

Test #4:

score: 0
Accepted
time: 3ms
memory: 3636kb

input:

1 3

output:

2

result:

ok answer is '2'

Test #5:

score: 0
Accepted
time: 3ms
memory: 3704kb

input:

1 100000000

output:

2

result:

ok answer is '2'

Test #6:

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

input:

2 2

output:

4

result:

ok answer is '4'

Test #7:

score: 0
Accepted
time: 3ms
memory: 3632kb

input:

2 4

output:

6

result:

ok answer is '6'

Test #8:

score: 0
Accepted
time: 3ms
memory: 3624kb

input:

2 5

output:

7

result:

ok answer is '7'

Test #9:

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

input:

2 100000000

output:

100000002

result:

ok answer is '100000002'

Test #10:

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

input:

3 2

output:

8

result:

ok answer is '8'

Test #11:

score: 0
Accepted
time: 3ms
memory: 3620kb

input:

3 3

output:

14

result:

ok answer is '14'

Test #12:

score: 0
Accepted
time: 3ms
memory: 3652kb

input:

3 4

output:

22

result:

ok answer is '22'

Test #13:

score: 0
Accepted
time: 3ms
memory: 3704kb

input:

3 5

output:

32

result:

ok answer is '32'

Test #14:

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

input:

3 100000000

output:

446563791

result:

ok answer is '446563791'

Test #15:

score: -100
Wrong Answer
time: 24ms
memory: 3704kb

input:

3000 2

output:

21292721

result:

wrong answer expected '21292722', found '21292721'