[Fortuna OJ] Apr 19 Provincial Team Selection, Group A - Problem-Solving Report
A - Directory Traversal
Nothing much to say: a pretty easy rerooting DP.
// traversal.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 200;
typedef long long ll;
int n, siz[MAX_N], head[MAX_N], current, wi[MAX_N], tot;
ll dp[MAX_N], g[MAX_N];
char name[20];
bool tag[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void dfs(int u, int fa)
{
for (int i = head[u]; i != -1; i = edges[i].nxt)
{
dfs(edges[i].to, u), siz[u] += siz[edges[i].to];
dp[u] += dp[edges[i].to] + 1LL * siz[edges[i].to] * wi[edges[i].to] + siz[edges[i].to] - (tag[edges[i].to] == true);
}
}
void collect(int u, int fa)
{
if (fa != 0)
g[u] = g[fa] + dp[fa] - dp[u] - 1LL * siz[u] * wi[u] - (siz[u] - (tag[u] == true)) + ((tot - siz[u]) * 3LL);
for (int i = head[u]; i != -1; i = edges[i].nxt)
collect(edges[i].to, u);
}
void fileIO(string src)
{
freopen((src + ".in").c_str(), "r", stdin);
freopen((src + ".out").c_str(), "w", stdout);
}
int main()
{
fileIO("traversal");
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1, m, val; i <= n; i++)
{
scanf("%s%d", name, &m), wi[i] = strlen(name);
if (m == 0)
siz[i] = 1, tag[i] = true, tot++;
else
while (m--)
scanf("%d", &val), addpath(i, val);
}
dfs(1, 0), collect(1, 0);
ll ans = 2e18;
for (int i = 1; i <= n; i++)
ans = min(ans, dp[i] + g[i]);
printf("%lld\n", ans);
return 0;
}
B - Persecuting DJ
In the exam you could actually tell that this problem was about finding the Fibonacci period, but I didn't really know how, so I wrote a brute force and left. Afterwards it turned out nobody knew how.
First, we know that this problem is essentially asking for \(g_x\) under several moduli. \(g_x\) can be rewritten as \(g_x = b \times Fib_{2n} - a \times Fib_{2(n - 1)}\). So the problem reduces to finding the values of the Fibonacci sequence under several moduli.
Finding the period takes a fair bit of human wisdom. Let \(mod = \prod p_i^{c_i}\); then:
\[ \begin{gathered} f(p) = \begin{cases} 3, p = 2 \\ 8, p = 3 \\ 5, p = 20 \\ p - 1, p \bmod 5 = 1, 4 \\ 2p + 2, \text{otherwise} \end{cases} \\ loop = \text{lcm} \{ f(p_i) \times p_i^{c_i - 1} \} \end{gathered} \]
The proof I don't know; if you're interested, go look it up.
Once we know this period, consider designing a subtask \(solve(n, k, mod)\) and just solve it recursively. To compute \(g_n\) quickly, plain matrix multiplication will do.
// hakugai.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 50;
int T, a, b, n, level, mod;
typedef long long ll;
ll loops[MAX_N], tot, cmod;
void fileIO(string src)
{
freopen((src + ".in").c_str(), "r", stdin);
freopen((src + ".out").c_str(), "w", stdout);
}
struct matrix
{
ll mat[2][2];
ll *operator[](const int &id) { return mat[id]; }
void clear() { memset(mat, 0, sizeof(mat)); };
void epsilon() { mat[0][0] = mat[1][1] = 1; }
matrix operator*(const matrix &rhs)
{
matrix ret;
ret.clear();
for (int i = 0; i < 2; i++)
for (int k = 0; k < 2; k++)
if (mat[i][k])
for (int j = 0; j < 2; j++)
if (rhs.mat[k][j])
ret[i][j] = (0LL + ret[i][j] + 1LL * mat[i][k] * rhs.mat[k][j] % cmod) % cmod;
return ret;
}
matrix operator^(const ll &rhs)
{
matrix ret, bas = *this;
ret.clear(), ret.epsilon();
ll tim = rhs;
while (tim)
{
if (tim & 1LL)
ret = ret * bas;
bas = bas * bas;
tim >>= 1;
}
return ret;
}
} init, trans;
ll getGn(ll x, ll cm)
{
cmod = cm;
if (x == 0)
return a;
if (x == 1)
return b;
return (1LL * ((init * (trans ^ (2 * x - 1)))[0][1]) * b % cmod + cmod - 1LL * ((init * (trans ^ (2 * (x - 1) - 1)))[0][1]) * a % cmod) % cmod;
}
ll getFn(ll pbase)
{
if (pbase == 2)
return 3;
if (pbase == 3)
return 8;
if (pbase == 5)
return 20;
if (pbase % 5 == 1 || pbase % 5 == 4)
return pbase - 1;
return 2 * pbase + 2;
}
ll getLoopLen(ll x)
{
tot = 0;
ll acc = x;
for (int i = 2; 1LL * i * i <= acc; i++)
if (acc % i == 0)
{
loops[++tot] = getFn(i);
ll cnt = 1;
while (acc % i == 0)
acc /= i, cnt *= i;
cnt /= i, loops[tot] *= cnt;
}
if (acc != 1)
loops[++tot] = getFn(acc);
ll loop = loops[1];
for (int i = 2; i <= tot; i++)
loop = (loop / __gcd(loop, loops[i])) * loops[i];
return loop;
}
ll solve(ll n_, ll k_, ll cm)
{
if (k_ == 0)
return n_ % cm;
ll nxt_loop = getLoopLen(cm), now = solve(n_, k_ - 1, nxt_loop);
ll res = getGn(now, cm);
return res;
}
int main()
{
// fileIO("hakugai");
scanf("%d", &T);
trans.clear(), trans[1][0] = 1, trans[1][1] = 1, trans[0][1] = 1;
init.clear(), init[0][0] = 0, init[0][1] = 1;
while (T--)
{
scanf("%d%d%d%d%d", &a, &b, &n, &level, &mod);
// get loop;
printf("%lld\n", solve(n, level, mod));
}
return 0;
}
B - Yubari's Remodel
This one is rather godlike.
Consider the matrix-tree theorem; applied directly, there's no way to enforce the \(k\) thing. Human wisdom tells us that if we give the edges that are not yet added a weight of \(x\), then what the matrix-tree theorem gives us in the end must be a polynomial in \(x\), and the coefficients of the first \(k + 1\) terms are the respective numbers of ways.
We let \(x = 1, 2, \dots, n\) to get the point values, then interpolate the polynomial with Gaussian elimination, and then just add up the coefficients of the first \(k + 1\) terms. Personally, I like this problem a lot.
// kaisou.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 55, mod = 998244353;
int n, limit, mat[MAX_N][MAX_N], poly_val[MAX_N], poly_coeff[MAX_N];
bool mp[MAX_N][MAX_N];
void fileIO(string src)
{
freopen((src + ".in").c_str(), "r", stdin);
freopen((src + ".out").c_str(), "w", stdout);
}
int fpow(int bas, int tim)
{
int ret = 1;
while (tim)
{
if (tim & 1)
ret = 1LL * ret * bas % mod;
bas = 1LL * bas * bas % mod;
tim >>= 1;
}
return ret;
}
int gauss()
{
int res = 1;
for (int i = 0; i < n - 1; i++)
{
int key = i;
for (int j = i; j < n - 1; j++)
if (mat[j][i] != 0)
{
key = j;
break;
}
if (key != i)
{
res = mod - res;
for (int j = i; j < n - 1; j++)
swap(mat[i][j], mat[key][j]);
}
int inv = fpow(mat[i][i], mod - 2);
for (int j = i + 1; j < n - 1; j++)
{
int rate = 1LL * mat[j][i] * inv % mod;
for (int k = i; k < n - 1; k++)
mat[j][k] = (0LL + mat[j][k] + mod - 1LL * rate * mat[i][k] % mod) % mod;
}
}
for (int i = 0; i < n - 1; i++)
res = 1LL * res * mat[i][i] % mod;
return res;
}
int main()
{
// fileIO("kaisou");
scanf("%d%d", &n, &limit);
for (int i = 1, fa; i < n; i++)
scanf("%d", &fa), mp[i][fa] = mp[fa][i] = true;
for (int x = 1; x <= n; x++)
{
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
if (!mp[i][j])
mat[i][j] = mod - x, mat[i][i] += x;
else
mat[i][j] = mod - 1, mat[i][i]++;
poly_val[x] = gauss();
}
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++)
{
mat[i][0] = 1;
for (int j = 1; j < n; j++)
mat[i][j] = 1LL * mat[i][j - 1] * (i + 1) % mod;
mat[i][n] = poly_val[i + 1];
}
for (int i = 0; i < n; i++)
{
int key = i;
for (int j = i; j < n; j++)
if (mat[j][i] > 0)
{
key = j;
break;
}
if (key != i)
for (int j = i; j <= n; j++)
swap(mat[i][j], mat[key][j]);
int inv = fpow(mat[i][i], mod - 2);
for (int j = 0; j < n; j++)
if (i != j)
{
int rate = 1LL * mat[j][i] * inv % mod;
for (int k = i + 1; k <= n; k++)
mat[j][k] = (0LL + mat[j][k] + mod - 1LL * rate * mat[i][k] % mod) % mod;
}
}
for (int i = 0; i < n; i++)
poly_coeff[i] = 1LL * mat[i][n] * fpow(mat[i][i], mod - 2) % mod;
int ans = 0;
for (int i = 0; i <= limit; i++)
ans = (0LL + ans + poly_coeff[i]) % mod;
printf("%d\n", ans);
return 0;
}
D - Atlanta
During the contest I used a rather sneaky method on this one and got the 70 points whose data were weak. The intended solution is really quite interesting, though.
We split every divisor of an edge off into an independent edge of its own, then enumerate every divisor \(d\) that appears; writing \(f(d)\) for the number of connected vertex pairs, the answer is \(\sum_{d = 1} \mu(d) f(d)\). This step is really badass.
With this, we can get computing. Since \(q\) is small, we take the queries offline and decompose them as well, but we have to tag them with their times.
We first handle the edges that are never touched, with a DSU. Once those are done, we need to handle the queried edges. At this point we need a DSU with rollback, implemented with union by size. Finally, add it all into the answer.
That afternoon I spent half an hour thinking through the implementation and still couldn't write it, so in the end I went and looked at the code in a solution (I'm so weak).
// atoranta.cpp
// #pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 200;
typedef long long ll;
int n, q, mu[MAX_N], primes[MAX_N], tot, mem[MAX_N], size[MAX_N], last_time[MAX_N], visit[MAX_N];
ll ans[MAX_N], sum;
bool vis[MAX_N];
stack<int> stk;
struct node
{
int id, time_frame;
};
vector<node> frames[MAX_N], qframes[MAX_N];
vector<int> facts[MAX_N];
struct segment
{
int x, y, z;
} org[MAX_N], qseg[MAX_N];
void fileIO(string src)
{
freopen((src + ".in").c_str(), "r", stdin);
freopen((src + ".out").c_str(), "w", stdout);
}
void sieve()
{
mu[1] = 1;
for (int i = 2; i < MAX_N; i++)
{
if (!vis[i])
primes[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot && 1LL * i * primes[j] < MAX_N; j++)
{
vis[i * primes[j]] = true, mu[i * primes[j]] = -mu[i];
if (i % primes[j] == 0)
{
mu[i * primes[j]] = 0;
break;
}
}
}
for (int i = 2; i < MAX_N; i++)
if (!vis[i])
for (int j = i; j <= 1e6; j += i)
facts[j].push_back(i);
}
int find(int x)
{
while (mem[x])
x = mem[x];
return x;
}
void merge(int x, int y)
{
int fx = find(x), fy = find(y);
if (size[fx] < size[fy])
swap(fx, fy);
mem[fy] = fx, sum += 1LL * size[fx] * size[fy];
size[fx] += size[fy], stk.push(fy);
}
void undo()
{
int fy = stk.top(), fx = mem[fy];
size[fx] -= size[fy], sum -= 1LL * size[fx] * size[fy], mem[fy] = 0;
stk.pop();
}
void insert(int w, int id, int time_frame)
{
node u = node{id, time_frame};
int m = facts[w].size();
for (int stat = 0; stat < (1 << m); stat++)
{
int pans = 1;
for (int i = 0; i < m; i++)
if (stat & (1 << i))
pans *= facts[w][i];
frames[pans].push_back(u);
}
}
void solve(int x)
{
sum = 0;
for (int i = 0, gx, siz = frames[x].size(); i < siz; i = gx + 1)
{
gx = i - 1;
int ctime = frames[x][i].time_frame;
while (gx < siz - 1 && frames[x][gx + 1].time_frame == ctime)
gx++, merge(org[frames[x][gx].id].x, org[frames[x][gx].id].y);
ll pans = 1LL * mu[x] * sum;
if (ctime != -1)
{
ans[ctime] += pans;
for (int tot = i; tot <= gx; tot++)
undo();
}
else
{
for (int k = gx + 1; k < siz; k++)
visit[frames[x][k].time_frame] = x;
for (int k = 0; k <= q; k++)
if (visit[k] != x)
ans[k] += pans;
}
}
while (!stk.empty())
undo();
}
int main()
{
fileIO("atoranta");
scanf("%d", &n), sieve();
for (int i = 1; i <= n - 1; i++)
scanf("%d%d%d", &org[i].x, &org[i].y, &org[i].z);
scanf("%d", &q);
for (int i = 1; i <= q; i++)
scanf("%d%d", &qseg[i].x, &qseg[i].y), last_time[qseg[i].x] = i;
for (int i = 1; i < n; i++)
if (last_time[i] == 0)
insert(org[i].z, i, -1);
for (int i = 1; i <= q; i++)
{
node u = node{qseg[i].x, qseg[i].y};
// marks;
for (int j = i; j <= q; j++)
if (j == i || qseg[i].x != qseg[j].x)
qframes[j].push_back(u);
else
break;
bool flag = false;
for (int j = 1; j < i; j++)
if (qseg[i].x == qseg[j].x)
{
flag = true;
break;
}
// first;
if (!flag)
{
node v = node{qseg[i].x, org[qseg[i].x].z};
for (int j = 0; j < i; j++)
qframes[j].push_back(v);
}
}
for (int i = 0; i <= q; i++)
for (int j = 0, siz = qframes[i].size(); j < siz; j++)
insert(qframes[i][j].time_frame, qframes[i][j].id, i);
for (int i = 1; i <= n; i++)
size[i] = 1;
for (int i = 1; i <= 1e6; i++)
if (!frames[i].empty())
solve(i);
for (int i = 0; i <= q; i++)
printf("%lld\n", ans[i]);
return 0;
}
Comments
Mentions