[Fortuna OJ] Aug 7th - Group A Problem-Solving Report
A - Little L's Sequence
This problem's formula is the same as the one in an F problem on Codeforces, which made me think it was BSGS or some such thing, but in the end it turned out to really be a matrix multiplication problem.
Consider that any answer is made up of a product of powers of \(\{f_n\}\). Since \(k\) is small, we can consider computing the exponent of each \(f\), and then we can use fast exponentiation to compute these things.
Let's derive the matrix. Consider a matrix of size \(k \times k\), where in column \(k\), row \(i\) is the exponent of \(f_i\). Then write down some constraints:
\[ A[i][k] = \sum_{s = 1}^k A'[i][s] \times A'[s][k] \]
After some thought, we find that every exponent comes from \(i - 1\), and:
\[ \begin{bmatrix} b_3 \\ b_2 \\ b_1 \end{bmatrix} \to \begin{bmatrix} b_1 b_3 \\ b_1 b_2 + b_1 \\ b_1^2 + b_2 \end{bmatrix} \]
We find that row \(i\) is multiplied by \(b_1\) and then \(b_{i - 1}\) is added. So:
\[ A[i][k] = A'[i][k] \times b_1 + b_{i - 1} \]
Let's fit the coefficients of this matrix as best we can: \( A'[k][k] = b_1, A'[i][i - 1] = 1 \).
Then we can happily write the code.
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 40000010, mod1 = 998244353, mod2 = 998244352;
int n, k, f[MAX_N];
int quick_pow(int bas, int tim, int mod)
{
int ans = 1;
bas %= mod;
while (tim)
{
if (tim & 1)
ans = 1LL * ans * bas % mod;
bas = 1LL * bas * bas % mod;
tim >>= 1;
}
return ans;
}
struct matrix
{
int mat[201][201];
matrix() { memset(mat, 0, sizeof(mat)); }
matrix operator*(const matrix &target)
{
matrix ans;
for (int i = 1; i <= k; i++)
for (int j = 1; j <= k; j++)
for (int s = 1; s <= k; s++)
ans.mat[i][j] = (1LL * ans.mat[i][j] + 1LL * mat[i][s] * target.mat[s][j] % mod2) % mod2;
return ans;
}
matrix operator^(const int &ti)
{
int tim = ti;
matrix ans, bas = *this;
for (int i = 1; i <= k; i++)
ans.mat[i][i] = 1;
while (tim)
{
if (tim & 1)
ans = ans * bas;
bas = bas * bas;
tim >>= 1;
}
return ans;
}
} A;
int main()
{
/*
freopen("seq.in", "r", stdin);
freopen("seq.out", "w", stdout);
*/
scanf("%d%d", &n, &k);
for (int i = 2; i <= k; i++)
A.mat[i][i - 1] = 1;
for (int i = k; i >= 1; i--)
scanf("%d", &A.mat[i][k]);
for (int i = 1; i <= k; i++)
scanf("%d", &f[i]);
if (n <= k)
printf("%d", f[n] % mod1), exit(0);
n -= k;
A = A ^ n;
int ans = 1;
for (int i = 1; i <= k; i++)
ans = 1LL * ans * quick_pow(f[i], A.mat[i][k], mod1) % mod1;
printf("%d", ans);
return 0;
}
B - Dream
Consider a direct greedy. Enumerate the turning points \(p_i\), push all intervals whose left endpoint is less than \(p_i\) into the priority queue, and then pop those whose right endpoint is less than \(p_i\). The priority queue is then the answer set, so we can greedily take the one with the nearest right endpoint to pair up.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 2000;
int n, m, pts[MAX_N];
struct interval
{
int l, r;
bool operator<(const interval &target) const { return l < target.l || (l == target.l && r < target.r); }
} ints[MAX_N];
struct compare
{
bool operator()(interval a, interval b) { return a.r > b.r || (a.r == b.r && a.l > b.l); }
};
priority_queue<interval, vector<interval>, compare> q;
int main()
{
/*
freopen("dream.in", "r", stdin);
freopen("dream.out", "w", stdout);
*/
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d%d", &ints[i].l, &ints[i].r);
for (int i = 1; i <= m; i++)
scanf("%d", &pts[i]);
sort(ints + 1, ints + 1 + m);
sort(pts + 1, pts + 1 + m);
int ans = 0;
for (int i = 1, cur = 1; i <= m; i++)
{
int u = pts[i];
while (cur <= n && ints[cur].l <= u)
q.push(ints[cur++]);
while (!q.empty() && q.top().r < u)
q.pop();
if (!q.empty())
ans++, q.pop();
}
printf("%d\n", ans);
return 0;
}
C - Tree
Now this problem is a really clever one.
Consider counting the invalid pairs, and then get the number of valid pairs indirectly. We find that for a pair of vertices \((x, y)\), the number of invalid pairs falls into two cases:
- When \(lca(x, y) != x, lca(x, y) != y\), the number of invalid pairs is \(size[x] \times size[y]\).
- When \(lca(x, y) = x\), the number of invalid pairs is \(size[y] \times (tot - size[x] + 1)\).
Both of these look rather like a product of two quantities, so consider abstracting them as rectangle areas. You'll find that it's really just taking the DFS order of \(x\) as the \(x\)-axis and the DFS order of \(y\) as the \(y\)-axis, and then placing one or two rectangles. So, as we can see, this is a sweep line problem.
// C.cpp
#include <bits/stdc++.h>
#define ll long long
#define lson (p << 1)
#define rson ((p << 1) | 1)
#define mid ((l + r) >> 1)
using namespace std;
const int MAX_N = 1e5 + 200;
int n, m, current, head[MAX_N], dfn[MAX_N], low[MAX_N], tot, rectot;
int fa[20][MAX_N], dep[MAX_N];
ll tree[MAX_N << 8], tag[MAX_N << 8];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
struct line_node
{
int x, y1, y2, aff;
bool operator<(const line_node &ln) const { return x < ln.x; }
} nodes[MAX_N << 2];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void dfs(int u)
{
dfn[u] = ++tot, dep[u] = dep[fa[0][u]] + 1;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa[0][u])
fa[0][edges[i].to] = u, dfs(edges[i].to);
low[u] = tot;
}
void update(int ql, int qr, int l, int r, int p, ll val)
{
if (ql <= l && r <= qr)
{
tag[p] += val;
if (tag[p])
tree[p] = r - l + 1;
else if (l == r)
tree[p] = 0;
else
tree[p] = tree[lson] + tree[rson];
return;
}
if (ql <= mid)
update(ql, qr, l, mid, lson, val);
if (mid < qr)
update(ql, qr, mid + 1, r, rson, val);
if (tag[p])
tree[p] = r - l + 1;
else if (l == r)
tree[p] = 0;
else
tree[p] = tree[lson] + tree[rson];
}
void create_rectangle(int x1, int x2, int y1, int y2)
{
nodes[++rectot] = line_node{x1, y1, y2, 1};
nodes[++rectot] = line_node{x2 + 1, y1, y2, -1};
}
int lca(int x, int y)
{
for (int i = 19; i >= 0; i--)
if (dep[fa[i][y]] > dep[x])
y = fa[i][y];
return y;
}
void process_pair()
{
for (int i = 1; i <= m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
if (dfn[x] > dfn[y])
swap(x, y);
if (dfn[x] < dfn[y] && dfn[y] <= low[x])
{
int son = lca(x, y);
if (dfn[son] > 1)
create_rectangle(1, dfn[son] - 1, dfn[y], low[y]);
if (low[son] < n)
create_rectangle(dfn[y], low[y], low[son] + 1, n);
}
else
create_rectangle(dfn[x], low[x], dfn[y], low[y]);
}
sort(nodes + 1, nodes + 1 + rectot);
long long answer = 1LL * n * (n - 1) / 2;
for (int i = 1, cur = 1; i <= n; i++)
{
while (cur <= rectot && nodes[cur].x <= i)
update(nodes[cur].y1, nodes[cur].y2, 1, n, 1, nodes[cur].aff), cur++;
answer -= tree[1];
}
printf("%lld", answer);
}
int main()
{
/*
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
*/
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1, x, y; i <= n - 1; i++)
scanf("%d%d", &x, &y), addpath(x, y), addpath(y, x);
dfs(1);
for (int i = 1; i < 20; i++)
for (int j = 1; j <= n; j++)
fa[i][j] = fa[i - 1][fa[i - 1][j]];
process_pair();
return 0;
}