Advertisement
aayyk

Untitled

Sep 24th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. //#pragma GCC optimize("Ofast")
  2.  
  3. #ifdef LOCAL
  4. #include "debug.h"
  5. #else
  6. #include <bits/stdc++.h>
  7. #define debug(x...)
  8. #endif
  9.  
  10. //#define int ll
  11.  
  12. using namespace std;
  13. typedef long long ll;
  14. typedef long double ld;
  15. typedef pair <int, int> pii;
  16. #define sz(x) int((x).size())
  17.  
  18. #ifdef ONLINE_JUDGE
  19. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  20. #else
  21. mt19937 rng(1000 - 7);
  22. #endif
  23.  
  24. const int N = 3e3 + 10;
  25. const int M = 5e2 + 10;
  26. //const int MOD2 = 998244353;
  27. const int MOD = 1e9 + 7;
  28. const ld eps = 1e-6;
  29. const pii dir[] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
  30.  
  31. int n, a[N];
  32. ll dp[N][N], fact[N], C[N][N];
  33.  
  34. signed main() {
  35. #ifdef LOCAL
  36.     freopen("input.txt", "r", stdin);
  37.     freopen("output.txt", "w", stdout);
  38. #endif
  39.     cout << fixed << setprecision(9);
  40.     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  41.  
  42.     fact[0] = 1;
  43.     for (int i = 1; i < N; i++) {
  44.         fact[i] = (fact[i - 1] * i) % MOD;
  45.     }
  46.  
  47.     for (int i = 0; i < N; i++) {
  48.         C[i][0] = C[i][i] = 1;
  49.     }
  50.  
  51.     for (int i = 1; i < N; i++) {
  52.         for (int j = 1; j < i; j++) {
  53.             C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
  54.         }
  55.     }
  56.    
  57.     cin >> n;
  58.     for (int i = 0, x; i < n; i++) {
  59.         cin >> x;
  60.         a[x]++;
  61.     }
  62.  
  63.     auto sqr = [=] (ll x) {
  64.         return (x * x) % MOD;
  65.     };
  66.  
  67.     dp[0][0] = 1;
  68.     for (int i = 1; i <= n; i++) {
  69.         for (int j = 0; j <= n; j++) {
  70.             for (int k = 0; k <= min(a[i], j); k++) {
  71.                 dp[i][j] = (dp[i][j] +
  72.                             (dp[i - 1][j - k] * sqr(C[a[i]][k])) % MOD * fact[k]) % MOD;
  73.             }
  74.         }
  75.     }
  76.  
  77.     ll ans = (dp[n][0] * fact[n]) % MOD;
  78.     for (int i = 1; i <= n; i++) {
  79.         if (i % 2) {
  80.             ans = (ans - (dp[n][i] * fact[n - i]) % MOD + MOD) % MOD;
  81.         }
  82.         else {
  83.             ans = (ans + (dp[n][i] * fact[n - i]) % MOD) % MOD;
  84.         }
  85.     }
  86.  
  87.     cout << ans << endl;
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement