T1:
给定一个序列,对于所有 询问所有此长度的区间的最大值的最小值。
考虑每个数值的贡献,显然对于其操控的长度(前面第一个比它大的数字和后面第一个大于等于它的数字之间),小于等于该长度的位置都会得到它的贡献,因此每个数字标记一下该位置,倒着扫一下做完了,时间复杂度 。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e7 + 5;
int n, x, y, z, a[N], ans[N], g[N], q[N], h[N];
signed main() {
freopen("value.in","r",stdin);
freopen("value.out","w",stdout);
scanf("%lld%lld%lld%lld", &n, &x, &y, &z);
for (int i = 1; i <= n; ++ i ) a[i] = (a[i - 1] * x + y) % z;
memset(ans, 0x3f, sizeof(ans));
stack <int> s;
for (int i = 1; i <= n; ++ i ) {
while (s.size() && a[s.top()] < a[i]) s.pop();
q[i] = s.size() ? s.top() : 0;
s.push(i);
}
while (s.size()) s.pop();
for (int i = n; i >= 1; -- i ) {
while (s.size() && a[s.top()] <= a[i]) s.pop();
h[i] = s.size() ? s.top() : n + 1;
s.push(i);
}
for (int i = 1; i <= n; ++ i ) ans[h[i] - q[i] - 1] = min(ans[h[i] - q[i] - 1], a[i]);
for (int i = n; i >= 1; -- i ) ans[i] = min(ans[i], ans[i + 1]);
for (int i = 1; i <= n; ++ i ) g[i] = (g[i - 1] * x + ans[i] + y) % z;
printf("%lld\n", g[n]);
return 0;
}
T2:
给定一个图,第 个点点权为 ,除了给定边外,如果 则 向 连边,边权都为 ,问 到任意点的最短路。
首先建立 个辅助点,直接每个辅助点向子集连边是不优的,应该向只改变一个 的点连边(二进制为 的向 ,, 等连边),边权为 ,然后每个位置向自己 对应的辅助点连边,反向边也有,随便钦定一种边权为 ,跑 做完了,复杂度大概 。
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 5;
int n, m, a[N], d[N];
vector <pair <int, bool> > g[N];
bool is[N];
signed main() {
// system("fc travel.out ans.out");
freopen("travel.in","r",stdin);
freopen("travel.out","w",stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++ i ) scanf("%d", a + i);
while (m -- ) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back({v, 1});
g[v].push_back({u, 1});
}
for (int i = 1; i <= n; ++ i ) {
g[i].push_back({n + 1 + a[i], 1});
g[n + 1 + a[i]].push_back({i, 0});
}
for (int i = 1; i < (1 << 20); ++ i )
for (int j = 0; j < 20; ++ j )
if (i >> j & 1)
g[n + 1 + i].push_back({n + 1 + (i ^ (1 << j)), 0});
deque <pair <int, int> > q;
memset(d, 0x3f, sizeof(d));
d[1] = 0;
q.push_back({0, 1});
while (q.size()) {
int u = q.front().second, w = q.front().first;
q.pop_front();
if (is[u]) continue;
is[u] = true;
for (auto j : g[u]) {
int v = j.first, ww = j.second;
if (d[v] > w + ww) {
d[v] = w + ww;
if (!ww) q.push_front({d[v], v});
else q.push_back({d[v], v});
}
}
}
for (int i = 1; i <= n; ++ i ) printf("%d\n", d[i]);
return 0;
}
T3:
有 种数字, 出现了 次,问这样形成的序列至少需要划分成 个连续严格上升子段的有多少,对 分别求,答案对 取模。
设 表示填了 的数字,形成了 个段了,那么转移的时候增加了 个 的时候,会发现 如果放在某个段正后方(就那一个)不会增加段数,否则会增加段数,我们可以枚举其中几个段后面被放了,然后发现其他段后面不能再放了,其他位置都能放,而且随便放,注意这些段时顺序的,放一个增加一段,组合数计算转移系数做完了,时间复杂度 。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7;
int n, K, t[N], f[55][2505], C[2505][2505];
int solve (int n, int m) {
if (m < 1) return 0;
return C[n + m - 1][m - 1];
}
signed main() {
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
C[0][0] = 1;
for (int i = 1; i <= 2500; ++ i ) {
C[i][0] = 1;
for (int j = 1; j <= i; ++ j ) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
scanf("%lld%lld", &n, &K);
for (int i = 1; i <= n; ++ i ) scanf("%lld", t + i);
f[0][0] = 1;
int sum = 0;
for (int i = 0; i < n; ++ i ) {
sum += t[i];
for (int j = 0; j <= sum; ++ j ) {
for (int k = 0; k <= min(t[i + 1], j); ++ k ) {
int c = C[j][k];
(c *= solve(t[i + 1] - k, sum + 1 - (j - k))) %= mod;
(f[i + 1][j + t[i + 1] - k] += f[i][j] * c) %= mod;
}
}
}
for (int k = 1; k <= K; ++ k ) printf("%lld ", f[n][k]);
return 0;
}
T4:
给定 ,设 表示 的约数中和 除这个约数的结果互质的约数个数,求 对 取模。
首先其中较小的约数肯定不超过 ,我们枚举它,设它为 ,思考它做出的贡献,即 内所有与 互质的 的数量乘 (因为还有对应的约数),特判 ,然后其他的数字进行质因数分解,问题转化为求这个范围内有多少个数字不包含这些质因数,发现质因数不超过 个,直接 容斥做完了。
复杂度大概 ,具体看实现方法。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5, mod = 998244353;
int n, a[N], cnt, len, sum, ans;
void dfs (int i, int ct, int now) {
if (i > cnt) {
if (ct & 1) sum -= len / now;
else sum += len / now;
return;
}
dfs (i + 1, ct + 1, now * a[i]);
dfs (i + 1, ct, now);
return;
}
int solve (int x) {
len = x;
sum = 0;
dfs (1, 0, 1);
return sum;
}
signed main() {
freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);
scanf("%lld", &n);
ans = (2 * n - 1) % mod;
for (int i = 2; n / i > i; ++ i ) {
int x = i;
cnt = 0;
for (int j = 2; j * j <= x; ++ j ) {
if (x % j == 0) {
a[ ++ cnt] = j;
while (x % j == 0) x /= j;
}
}
if (x > 1) a[ ++ cnt] = x;
(ans += (solve(n / i) - solve(i)) * 2) %= mod;
}
printf("%lld\n", ans);
return 0;
}