题解:AT_arc218_a [ARC218A] Many Sets
官方题解:
对于每个
对于固定的
因此,解决这个问题总共需要
看完题目用unordered_map写, 时间复杂度
后来用vector优化成了
对于每个
开一个 vector<int> vc[N*M] 用于存储整数 x 在各个序列中出现的次数(只存非零的). 遍历每个序列时, 先用临时的 map 统计该序列中每个数的出现次数,然后对每个出现过的数
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int n, m, tot = 1, ans, pwr[507];
vector<int> vc[250007];
signed main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
map<int, int> ct;
for (int j = 1, x; j <= m; j++)
cin >> x, ct[x]++;
for (auto [x, y] : ct)
vc[x].push_back(y);
}
pwr[0] = 1;
for (int i = 1; i <= n; i++)
(tot *= m) %= mod, pwr[i] = tot;
for (int i = 1; i <= n * m; i++)
{
int res = pwr[n - vc[i].size()];
for (auto x : vc[i])
(res *= (m - x)) %= mod;
(ans += tot - res + mod) %= mod;
}
cout << ans << endl;
return 0;
}
赛后发现空间限制是