Solutions of Array rearrangement - MarisaOJ: Marisa Online Judge

Solutions of Array rearrangement

Select solution language

Write solution here.


User Avatar Thanhmay2005    Created at    1 likes

**Gợi ý:** sử dụng hoán vị lặp, bạn có thể tìm hiểu về hoán vị lặp [tại đây](https://wiki.vnoi.info/translate/he/Number-Theory-5). Giả sử phần tử $x$ lặp lại $f_x$ lần thì ta có công thức tổng quát như sau: $$\text{Số mảng khác nhau} = \frac{n!}{\prod f_x!}$$ Bởi vì giới hạn có $10^6$ nên ta có thể tính ra được theo công thức này **Code:** ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6, MOD = 1e9 + 7; int64_t F[N + 1], F1[N + 1], a[N + 1]; int n; map <int, int> mp; int64_t binpow (int64_t a, int64_t b) { int64_t ans = 1; while (b) { if (b & 1) ans = ans * a % MOD; a = a * a % MOD; b /= 2; } return ans; } void init() { F[0] = 1; for (int i=1 ; i<=N ; i++) F[i] = F[i - 1] * i % MOD; F1[N] = binpow(F[N], MOD - 2); for (int i=N ; i>0 ; i--) F1[i - 1] = F1[i] * i % MOD; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); init(); cin >> n; for (int i=1 ; i<=n ; i++) { cin >> a[i]; mp[a[i]]++; } int64_t ans = F[n]; for (auto [val, cnt] : mp) { ans = (ans % MOD * F1[cnt] % MOD) % MOD; } cout << ans << '\n'; return 0; } ```