#ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } int C(int n, int k) { if (!(0 <= k and k <= n)) { return 0; } int result = 1; for (int i = 0; i < k; i++) { result *= (n - i); result /= (i + 1); } return result; } constexpr int nax = 45; int n; int tab[nax]; void Read() { cin >> n; for (int i = 0; i < n; i++) { cin >> tab[i]; tab[i]--; } } struct Dodawanko { int ja = 0; int nastepny = 0; }; struct Kubelek { int a = 0, b = 0; int ile_wpada = 0; vector<Dodawanko> dodaj; }; debug& operator<<(debug& deb, const Dodawanko& d) { return deb << "(" << d.ja << ", " << d.nastepny << ")"; } debug& operator<<(debug& deb, const Kubelek& k) { return deb << "K{[" << k.a << ", " << k.b << "], " << k.ile_wpada << ", dod = " << k.dodaj << "}"; } vector<Kubelek> kubelki[nax]; int dodawanko_elementu[nax]; int sposoby[nax]; void Kubelki() { for (int i = 0; i <= n; i++) { vector<int> liczby_na_prawo; for (int j = i; j < n; j++) { liczby_na_prawo.push_back(tab[j]); } liczby_na_prawo.push_back(numeric_limits<int>::min()); liczby_na_prawo.push_back(numeric_limits<int>::max()); sort(liczby_na_prawo.begin(), liczby_na_prawo.end()); vector<Kubelek>& kub = kubelki[i]; for (int j = 1; j < (int) liczby_na_prawo.size(); j++) { Kubelek k; k.a = max(0, liczby_na_prawo[j - 1] + 1); k.b = min(n - 1, liczby_na_prawo[j] - 1); if (k.a <= k.b) { kub.push_back(k); } } for (int j = 0; j < i; j++) { int do_ilu_wpadlo = 0; for (Kubelek& k : kub) { if (k.a <= tab[j] and tab[j] <= k.b) { do_ilu_wpadlo++; k.ile_wpada++; } } assert(do_ilu_wpadlo == 1); } int& spo = sposoby[i]; spo = 1; for (const Kubelek& k : kub) { spo *= (k.ile_wpada + 1); } } } void PrzejsciaKubelkow() { for (int i = 0; i <= n; i++) { int spo = 1; for (Kubelek& k : kubelki[i]) { k.dodaj.resize(k.ile_wpada + 1); for (int j = 0; j <= k.ile_wpada; j++) { k.dodaj[j].ja = j * spo; } spo *= (k.ile_wpada + 1); } } for (int i = 1; i <= n; i++) { for (Kubelek& ka : kubelki[i - 1]) { int ile_zawierajacych = 0; for (const Kubelek& kb : kubelki[i]) { if (kb.a <= ka.a and ka.b <= kb.b) { assert(ka.ile_wpada <= kb.ile_wpada); for (int j = 0; j <= ka.ile_wpada; j++) { ka.dodaj[j].nastepny += kb.dodaj[j].ja; } ile_zawierajacych++; } } assert(ile_zawierajacych == 1); } } for (int i = 0; i < n; i++) { int& dod = dodawanko_elementu[i]; int ile_zawierajacych = 0; for (const Kubelek& k : kubelki[i + 1]) { if (k.a <= tab[i] and tab[i] <= k.b) { ile_zawierajacych++; assert((int) k.dodaj.size() >= 1); dod += k.dodaj[1].ja; } } assert(ile_zawierajacych == 1); } #ifdef LOCAL for (int i = 0; i <= n; i++) { debug() << imie(i) imie(sposoby[i]) imie(dodawanko_elementu[i]); for (const Kubelek& k : kubelki[i]) { debug() << " " << k; } } #endif } struct DpValue { int size = 0; ll ile_sposobow = 0; void Dodaj(int new_size, ll new_ile_sposobow) { if (new_size > size) { size = new_size; ile_sposobow = new_ile_sposobow; } else if (new_size == size) { ile_sposobow += new_ile_sposobow; } } }; int which = 0; vector<DpValue> dp[2]; void Generuj(int i, int kub_id, int suma_ja, int suma_nastepny, int nowe) { if (kub_id == (int) kubelki[i].size()) { dp[which ^ 1][suma_nastepny].Dodaj( dp[which][suma_ja].size, dp[which][suma_ja].ile_sposobow); dp[which ^ 1][suma_nastepny + dodawanko_elementu[i]].Dodaj( dp[which][suma_ja].size + nowe, dp[which][suma_ja].ile_sposobow); return; } const Kubelek& k = kubelki[i][kub_id]; for (int j = 0; j <= k.ile_wpada; j++) { Generuj(i, kub_id + 1, suma_ja + k.dodaj[j].ja, suma_nastepny + k.dodaj[j].nastepny, nowe + (k.b < tab[i] ? j : 0)); } } void LiczDp() { int max_size = 0; for (int i = 0; i <= n; i++) { Maxi(max_size, sposoby[i]); } dp[0].resize(max_size); dp[1].resize(max_size); dp[0][0].size = 0; dp[0][0].ile_sposobow = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < sposoby[i + 1]; j++) { dp[which ^ 1][j].size = -1; } Generuj(i, 0, 0, 0, 0); which ^= 1; } } void Wypisz() { for (int k = 1; k <= n; k++) { const DpValue& depek = dp[which][k]; cout << C(k, 2) - depek.size << " " << depek.ile_sposobow << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); Read(); Kubelki(); PrzejsciaKubelkow(); LiczDp(); Wypisz(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } int C(int n, int k) { if (!(0 <= k and k <= n)) { return 0; } int result = 1; for (int i = 0; i < k; i++) { result *= (n - i); result /= (i + 1); } return result; } constexpr int nax = 45; int n; int tab[nax]; void Read() { cin >> n; for (int i = 0; i < n; i++) { cin >> tab[i]; tab[i]--; } } struct Dodawanko { int ja = 0; int nastepny = 0; }; struct Kubelek { int a = 0, b = 0; int ile_wpada = 0; vector<Dodawanko> dodaj; }; debug& operator<<(debug& deb, const Dodawanko& d) { return deb << "(" << d.ja << ", " << d.nastepny << ")"; } debug& operator<<(debug& deb, const Kubelek& k) { return deb << "K{[" << k.a << ", " << k.b << "], " << k.ile_wpada << ", dod = " << k.dodaj << "}"; } vector<Kubelek> kubelki[nax]; int dodawanko_elementu[nax]; int sposoby[nax]; void Kubelki() { for (int i = 0; i <= n; i++) { vector<int> liczby_na_prawo; for (int j = i; j < n; j++) { liczby_na_prawo.push_back(tab[j]); } liczby_na_prawo.push_back(numeric_limits<int>::min()); liczby_na_prawo.push_back(numeric_limits<int>::max()); sort(liczby_na_prawo.begin(), liczby_na_prawo.end()); vector<Kubelek>& kub = kubelki[i]; for (int j = 1; j < (int) liczby_na_prawo.size(); j++) { Kubelek k; k.a = max(0, liczby_na_prawo[j - 1] + 1); k.b = min(n - 1, liczby_na_prawo[j] - 1); if (k.a <= k.b) { kub.push_back(k); } } for (int j = 0; j < i; j++) { int do_ilu_wpadlo = 0; for (Kubelek& k : kub) { if (k.a <= tab[j] and tab[j] <= k.b) { do_ilu_wpadlo++; k.ile_wpada++; } } assert(do_ilu_wpadlo == 1); } int& spo = sposoby[i]; spo = 1; for (const Kubelek& k : kub) { spo *= (k.ile_wpada + 1); } } } void PrzejsciaKubelkow() { for (int i = 0; i <= n; i++) { int spo = 1; for (Kubelek& k : kubelki[i]) { k.dodaj.resize(k.ile_wpada + 1); for (int j = 0; j <= k.ile_wpada; j++) { k.dodaj[j].ja = j * spo; } spo *= (k.ile_wpada + 1); } } for (int i = 1; i <= n; i++) { for (Kubelek& ka : kubelki[i - 1]) { int ile_zawierajacych = 0; for (const Kubelek& kb : kubelki[i]) { if (kb.a <= ka.a and ka.b <= kb.b) { assert(ka.ile_wpada <= kb.ile_wpada); for (int j = 0; j <= ka.ile_wpada; j++) { ka.dodaj[j].nastepny += kb.dodaj[j].ja; } ile_zawierajacych++; } } assert(ile_zawierajacych == 1); } } for (int i = 0; i < n; i++) { int& dod = dodawanko_elementu[i]; int ile_zawierajacych = 0; for (const Kubelek& k : kubelki[i + 1]) { if (k.a <= tab[i] and tab[i] <= k.b) { ile_zawierajacych++; assert((int) k.dodaj.size() >= 1); dod += k.dodaj[1].ja; } } assert(ile_zawierajacych == 1); } #ifdef LOCAL for (int i = 0; i <= n; i++) { debug() << imie(i) imie(sposoby[i]) imie(dodawanko_elementu[i]); for (const Kubelek& k : kubelki[i]) { debug() << " " << k; } } #endif } struct DpValue { int size = 0; ll ile_sposobow = 0; void Dodaj(int new_size, ll new_ile_sposobow) { if (new_size > size) { size = new_size; ile_sposobow = new_ile_sposobow; } else if (new_size == size) { ile_sposobow += new_ile_sposobow; } } }; int which = 0; vector<DpValue> dp[2]; void Generuj(int i, int kub_id, int suma_ja, int suma_nastepny, int nowe) { if (kub_id == (int) kubelki[i].size()) { dp[which ^ 1][suma_nastepny].Dodaj( dp[which][suma_ja].size, dp[which][suma_ja].ile_sposobow); dp[which ^ 1][suma_nastepny + dodawanko_elementu[i]].Dodaj( dp[which][suma_ja].size + nowe, dp[which][suma_ja].ile_sposobow); return; } const Kubelek& k = kubelki[i][kub_id]; for (int j = 0; j <= k.ile_wpada; j++) { Generuj(i, kub_id + 1, suma_ja + k.dodaj[j].ja, suma_nastepny + k.dodaj[j].nastepny, nowe + (k.b < tab[i] ? j : 0)); } } void LiczDp() { int max_size = 0; for (int i = 0; i <= n; i++) { Maxi(max_size, sposoby[i]); } dp[0].resize(max_size); dp[1].resize(max_size); dp[0][0].size = 0; dp[0][0].ile_sposobow = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < sposoby[i + 1]; j++) { dp[which ^ 1][j].size = -1; } Generuj(i, 0, 0, 0, 0); which ^= 1; } } void Wypisz() { for (int k = 1; k <= n; k++) { const DpValue& depek = dp[which][k]; cout << C(k, 2) - depek.size << " " << depek.ile_sposobow << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); Read(); Kubelki(); PrzejsciaKubelkow(); LiczDp(); Wypisz(); return 0; } |