提出 #61439547


ソースコード 拡げる

// Problem: F - Count Arrays
// Contest: AtCoder - AtCoder Beginner Contest 387
// URL: https://atcoder.jp/contests/abc387/tasks/abc387_f
// 
// By Muaath Alqarni
// Blog: https://muaath.dev/blog
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 2027, MOD = 998244353;

ll binpow(ll h, ll y) {
	ll res = 1;
	while (y > 0) {
		if (y & 1)
		{
			res = res * h;
			if (res >= MOD)
				res %= MOD;
		}
		h = h * h;
		if (h >= MOD)
			h %= MOD;
		y >>= 1;
	}
	return res % MOD;
}

int n, m, a[N];
vector<int> adj[N], mdj[N];

ll dp[N][N];
ll pref[N];
int vis[N];

void dfs(int i) {
	for (int c : adj[i])
		dfs(c);
	
	
	for (int j = 1; j <= m; j++) {
		dp[i][j] = (j == 1 ? 1 : dp[i][j-1]);
		for (int c : adj[i]) {
			if (pref[c])
				(dp[i][j] *= binpow(pref[c], MOD-2)) %= MOD;
			(pref[c] += dp[c][j]) %= MOD;
			(dp[i][j] *= pref[c]) %= MOD;
		}
	}
}

int cyc[N];
int cmp = 0;
void visit(int x) {
	vis[x] = cmp;
	for (int c : mdj[x])
		if (!vis[c])
			visit(c);
}

ll solve_for(int x) {
	while (vis[x] != -cmp) { vis[x] = -cmp; x = a[x]; }
	int root = x;

	for (int _ = 0; _ < n; _++) {
		cyc[x] = cmp;
		x = a[x];
	}
	
	for (int i = 1; i <= n; i++) {
		if (abs(vis[i]) == cmp) {
			if (cyc[i] == cmp) continue;
			adj[cyc[a[i]] ? root : a[i]].push_back(i);
		}
	}
	dfs(root);
	ll sum = 0;
	for (int j = 1; j <= m; j++)
		(sum += dp[root][j]) %= MOD;
	return sum;
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; cin >> a[i++]);
	for (int i = 1; i <= n; i++) {
		mdj[i].push_back(a[i]);
		mdj[a[i]].push_back(i);
	}
	ll sol = 1;
	for (int i = 1; i <= n; i++) {
		if (!vis[i]) {
			cmp = i;
			visit(i);
			cmp = i;
			(sol *= solve_for(i)) %= MOD;
		}
	}
	cout << sol;
}

提出情報

提出日時
問題 F - Count Arrays
ユーザ Muaath_5
言語 C++ 20 (gcc 12.2)
得点 550
コード長 1943 Byte
結果 AC
実行時間 507 ms
メモリ 36012 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 550 / 550
結果
AC × 3
AC × 37
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3536 KiB
00_sample_01.txt AC 1 ms 3540 KiB
00_sample_02.txt AC 1 ms 3528 KiB
01_random_00.txt AC 42 ms 10188 KiB
01_random_01.txt AC 175 ms 22640 KiB
01_random_02.txt AC 3 ms 5612 KiB
01_random_03.txt AC 5 ms 3980 KiB
01_random_04.txt AC 10 ms 4824 KiB
01_random_05.txt AC 1 ms 3728 KiB
01_random_06.txt AC 34 ms 7036 KiB
01_random_07.txt AC 21 ms 5912 KiB
01_random_08.txt AC 23 ms 5636 KiB
01_random_09.txt AC 3 ms 4696 KiB
01_random_10.txt AC 18 ms 5920 KiB
01_random_11.txt AC 17 ms 7348 KiB
01_random_12.txt AC 60 ms 35780 KiB
01_random_13.txt AC 108 ms 35680 KiB
01_random_14.txt AC 468 ms 35396 KiB
01_random_15.txt AC 183 ms 25228 KiB
01_random_16.txt AC 167 ms 26876 KiB
01_random_17.txt AC 221 ms 23124 KiB
01_random_18.txt AC 464 ms 35068 KiB
01_random_19.txt AC 233 ms 20624 KiB
01_random_20.txt AC 470 ms 35496 KiB
01_random_21.txt AC 177 ms 17012 KiB
01_random_22.txt AC 190 ms 17580 KiB
01_random_23.txt AC 318 ms 26848 KiB
02_handmade_00.txt AC 486 ms 36012 KiB
02_handmade_01.txt AC 507 ms 35676 KiB
02_handmade_02.txt AC 44 ms 35652 KiB
02_handmade_03.txt AC 485 ms 35820 KiB
02_handmade_04.txt AC 506 ms 35720 KiB
02_handmade_05.txt AC 474 ms 35696 KiB
02_handmade_06.txt AC 1 ms 3448 KiB
02_handmade_07.txt AC 1 ms 3628 KiB
02_handmade_08.txt AC 1 ms 3672 KiB
02_handmade_09.txt AC 1 ms 3684 KiB