Advertisement
Guest User

Untitled

a guest
Nov 6th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int mod;
  4. class Mint
  5. {
  6. public:
  7.     int val;
  8.     Mint(int _val = 0)
  9.     {
  10.         val = _val % mod;
  11.     }
  12.     Mint(long long _val)
  13.     {
  14.         val = _val % mod;
  15.     }
  16.     Mint operator+(Mint oth)
  17.     {
  18.         return val + oth.val;
  19.     }
  20.     Mint operator*(Mint oth)
  21.     {
  22.         return 1LL * val * oth.val;
  23.     }
  24.     Mint operator-(Mint oth)
  25.     {
  26.         return val - oth.val + mod;
  27.     }
  28.     void operator+=(Mint oth)
  29.     {
  30.         val = (Mint(val) + oth).val;
  31.     }
  32.     void operator-=(Mint oth)
  33.     {
  34.         val = (Mint(val) - oth).val;
  35.     }
  36.     void operator*=(Mint oth)
  37.     {
  38.         val = (Mint(val) * oth).val;
  39.     }
  40. };
  41. int main()
  42. {
  43.     cin.tie(nullptr)->sync_with_stdio(false);
  44.     int n;
  45.     cin >> n >> mod;
  46.     vector<int> a(n + 1);
  47.     for (int i = 1; i <= n; ++i)
  48.     {
  49.         cin >> a[i];
  50.     }
  51.     a[0] = -1;
  52.     vector<vector<Mint>> pas(n + 1, vector<Mint>(n + 1));
  53.     pas[0][0] = 1;
  54.     for (int i = 1; i <= n; ++i)
  55.     {
  56.         for (int j = 0; j <= i; ++j)
  57.         {
  58.             if (j == 0 || j == i)
  59.             {
  60.                 pas[i][j] = 1;
  61.                 continue;
  62.             }
  63.             pas[i][j] = pas[i - 1][j] + pas[i - 1][j - 1];
  64.         }
  65.     }
  66.     function<Mint(int, int)> combi = [&](int i, int j)
  67.     {
  68.         if (i < j)
  69.         {
  70.             return Mint(0);
  71.         }
  72.         return pas[i][j];
  73.     };
  74.     function<Mint(int, int)> sb = [&](int n, int k)
  75.     {
  76.         return combi(n - 1, n - k);
  77.     };
  78.     vector<vector<vector<Mint>>> dp(n + 1, vector<vector<Mint>>(n + 1, vector<Mint>(2)));
  79.     vector<vector<vector<Mint>>> coef(n + 1, vector<vector<Mint>>(n + 1, vector<Mint>(2)));
  80.     dp[1][1][true] = 1;
  81.     coef[1][1][true] = 1;
  82.     for (int i = 2; i <= n; ++i)
  83.     {
  84.         dp[i][1][true] = dp[i - 1][1][false];
  85.         coef[i][1][true] = dp[i - 1][1][false];
  86.         dp[i][1][false] = (dp[i - 1][1][true] + dp[i - 1][1][false]) * (i - 1);
  87.         coef[i][1][false] = dp[i - 1][1][true] + dp[i - 1][1][false];
  88.         for (int j = 2; j <= i; ++j)
  89.         {
  90.             dp[i][j][true] = (dp[i - 1][j - 1][false] + dp[i - 1][j - 1][true]) * j;
  91.             coef[i][j][true] = dp[i - 1][j - 1][false] + dp[i - 1][j - 1][true];
  92.             dp[i][j][false] = (dp[i - 1][j][false] + dp[i - 1][j][true]) * (i - j);
  93.             coef[i][j][false] = dp[i - 1][j][false] + dp[i - 1][j][true];
  94.         }
  95.     }
  96.     vector<bool> exista(n + 2, true);
  97.     exista[0] = false;
  98.     exista[n + 1] = false;
  99.     int cnt = 0;
  100.     vector<Mint> ans(n + 1);
  101.     for (int i = 1; i < n; ++i)
  102.     {
  103.         int last = -1;
  104.         int m = 0;
  105.         int sz = 0;
  106.         for (int j = 1; j <= n; ++j)
  107.         {
  108.             if (exista[j])
  109.             {
  110.                 m += j != (last + 1);
  111.                 last = j;
  112.                 sz++;
  113.             }
  114.         }
  115.         function<void(int, int, int)> count = [&](int freq, int m, bool type)
  116.         {
  117.             if (freq == 0)
  118.             {
  119.                 return;
  120.             }
  121.             if (type)
  122.             {
  123.                 for (int k = 1; k <= n; ++k)
  124.                 {
  125.                     int need = k - cnt;
  126.                     if (need >= m)
  127.                     {
  128.                         ans[k] += combi(sz - m - 1, need - m) * (dp[need][m][false] + dp[need][m][true] - coef[need][m][true]) * freq;
  129.                     }
  130.                     if (need >= m - 1)
  131.                     {
  132.                         ans[k] += combi(sz - m - 1, need - m + 1) * coef[need + 1][m][true] * freq;
  133.                     }
  134.                 }
  135.             }
  136.             else
  137.             {
  138.                 for (int k = 1; k <= n; ++k)
  139.                 {
  140.                     int need = k - cnt;
  141.                     if (need >= m)
  142.                     {
  143.                         ans[k] += combi(sz - m - 1, need - m) * (dp[need][m][true] + dp[need][m][false]) * freq;
  144.                     }
  145.                 }
  146.             }
  147.         };
  148.         if (m)
  149.         {
  150.             if (exista[a[i - 1] + 1] && a[i - 1] + 1 < a[i])
  151.             {
  152.                 if (exista[a[i - 1] + 2])
  153.                 {
  154.                     count(1, m, true);
  155.                 }
  156.                 else
  157.                 {
  158.                     count(1, m - 1, false);
  159.                 }
  160.             }
  161.             cnt++;
  162.             int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
  163.             for (int j = 1; j < a[i]; ++j)
  164.             {
  165.                 if (j != (a[i - 1] + 1) && exista[j])
  166.                 {
  167.                     if (exista[j - 1] && exista[j + 1])
  168.                     {
  169.                         cnt1++;
  170.                         continue;
  171.                     }
  172.                     if (!exista[j - 1] && !exista[j + 1])
  173.                     {
  174.                         cnt2++;
  175.                         continue;
  176.                     }
  177.                     if (!exista[j - 1])
  178.                     {
  179.                         cnt3++;
  180.                         continue;
  181.                     }
  182.                     if (!exista[j + 1])
  183.                     {
  184.                         cnt4++;
  185.                         continue;
  186.                     }
  187.                 }
  188.             }
  189.             count(cnt1, m + 1, true);
  190.             count(cnt2, m - 1, false);
  191.             count(cnt3, m, true);
  192.             count(cnt4, m, false);
  193.             cnt--;
  194.         }
  195.         exista[a[i]] = false;
  196.         cnt += a[i] != (a[i - 1] + 1);
  197.     }
  198.     for (int i = 1; i <= n; ++i)
  199.     {
  200.         cout << ans[i].val << ' ';
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement