题意:长度3n的排列,一次可以排序前2n个,也可以排序后2n个,求对于所有排列,求排好所有排列的总次数.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 4e6 + 10;
int n, MOD, inv[N], fact[N], infact[N], ans[5];
void init(int n) {
fact[0] = infact[0] = inv[0] = inv[1] = 1;
for (int i = 2; i <= n; ++i) {
inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
}
for (int i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i % MOD;
infact[i] = infact[i - 1] * inv[i] % MOD;
}
}
int C(int n, int m) {
if (n < m) return 0;
if (m == 0 || n == m) return 1;
return fact[n] * infact[m] % MOD * infact[n - m] % MOD;
}
signed main() {
IOS;
cin >> n >> MOD;
init(N - 10);
//0
ans[0] = 1;
//1
ans[1] = (fact[n * 2] * 2 % MOD - fact[n] % MOD) % MOD;
((ans[1] -= ans[0]) += MOD) %= MOD;
//2
ans[2] = 2 * C(n * 2, n) % MOD * fact[n] % MOD * fact[n * 2] % MOD;
for (int i = 0; i <= n; ++i) {
((ans[2] -= C(n, i) * C(n, n - i) % MOD * fact[n] % MOD * C(n * 2 - i, n) % MOD * fact[n] % MOD * fact[n] % MOD) += MOD) %= MOD;
}
((ans[2] -= ans[0]) += MOD) %= MOD;
((ans[2] -= ans[1]) += MOD) %= MOD;
//3
ans[3] = fact[n * 3] % MOD;
((ans[3] -= ans[0]) += MOD) %= MOD;
((ans[3] -= ans[1]) += MOD) %= MOD;
((ans[3] -= ans[2]) += MOD) %= MOD;
int res = 0;
for (int i = 0; i <= 3; ++i) {
(res += ans[i] * i % MOD) %= MOD;
}
cout << res << "\n";
}
F.
题意:长度n数组a[n],起点1,每次可以从i跳到j,min(a[i],a[i+1],...a[j])*(j-i)^2的代价,求跳到(1~n)的最小代价.
考虑dp,f[i]=min(f[j] + min(a[i],a[i+1],...a[j])*(j-i)^2),min(a[i],a[i+1],...a[j])*(j - i)^2 <= n*(j - i),即n / (j - i) >= min(a[i],a[i+1],...,a[j]),枚举a[i]作为min(a[i],a[i+1],...a[j]),得出步长.若min>=√n,那么步长不会超过√n.若min<√n,考虑所有以x为最小值的区间,大小是O(n),因为每个点最多被覆盖2次,最多有√n个数.综合起来,转移是O(n√n)的.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
const int N = 1e6 + 10;
int n, a[N], f[N];
signed main() {
IOS;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 0; i <= n; ++i) {
f[i] = INF;
}
f[1] = 0;
for (int i = 1; i <= n; ++i) {
int d = n / a[i];
for (int j = i - 1; j >= 1 && i - j <= d; --j) {
f[i] = min(f[i], f[j] + (i - j) * (i - j) * a[i]);
if (a[j] <= a[i]) {
break;
}
}
for (int j = i + 1; j <= n && j - i <= d; ++j) {
f[j] = min(f[j], f[i] + (i - j) * (i - j) * a[i]);
if (a[j] <= a[i]) {
break;
}
}
}
for (int i = 1; i <= n; ++i) {
cout << f[i] << " ";
}
}