[Fortuna OJ] Jul 8th - Group B Problem-Solving Report
Why were today's problems so nasty?
A - String
Actually, I should have been able to solve this one during the contest; it's a very simple counting problem. In the exam room, the moment I saw strings I froze. For some reason I'm traumatized by lexicographic order on strings, but looking at it now, it's just an idiotic problem.
Count it like this:
- Enumerate an \(i\), and consider the case where the first \(i-1\) characters are the same as the string \(T\) and the \(i\)-th character is less than \(T[i]\); this guarantees that the requirement is met however the letters after it are placed.
- With \(i\) enumerated, enumerate the character \(ch\) in the range \([a, T[i])\), and consider the contribution of the following two cases to the answer:
- If \(ch = S[i]\), then the number of characters after it that need to be changed is \(k\), and the contribution to the answer is:\[ {n - i \choose k} 25^{k} \]
- If it is not equal, then the number of characters after it that need to be changed is \(k-1\), because this position takes up one; the contribution to the answer is:\[ {n - i \choose k - 1} 25^{k - 1} \]
- After adding the contribution, if \(T[i] \neq S[i]\) at this position, then do \(k--\), meaning one more differing character has been fixed.
// string.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e5 + 2000, mod = 1e9 + 7;
ll n, k, level[MAX_N], level_inv[MAX_N], ans;
char S[MAX_N], T[MAX_N];
ll quick_pow(ll bas, ll tim)
{
if (tim < 0)
return 0;
ll ans = 1;
while (tim)
{
if (tim & 1)
ans = ans * bas % mod;
bas = bas * bas % mod;
tim >>= 1;
}
return ans;
}
ll comb(ll n_, ll k_) { return level[n_] * level_inv[n_ - k_] % mod * level_inv[k_] % mod; }
int main()
{
scanf("%lld%lld", &n, &k), level[0] = 1;
for (int i = 1; i <= n; i++)
level[i] = level[i - 1] * i % mod;
level_inv[n] = quick_pow(level[n], mod - 2);
for (int i = n - 1; i >= 1; i--)
level_inv[i] = level_inv[i + 1] * (i + 1) % mod;
level_inv[0] = 1;
scanf("%s", S + 1), scanf("%s", T + 1);
for (int i = 1; i <= n; i++)
{
for (int ch = 0; ch < T[i] - 'a'; ch++)
{
if (ch + 'a' == S[i])
(ans += comb(n - i, k) * quick_pow(25, k) % mod) %= mod;
else
(ans += comb(n - i, k - 1) * quick_pow(25, k - 1) % mod) %= mod;
}
if (S[i] != T[i])
k--;
}
printf("%lld", (ans + 1) % mod);
return 0;
}
B - Running
This is a good problem. Right now I know very little about the cyclic stuff in number theory, so this is a good chance to learn about it.
Working through the samples by hand reveals a pattern: for a person whose step size is \(step_i\), the indices of the cells they can reach must be multiples of \(gcd(step_i, n)\). Using this property, we can enumerate \(n\)'s divisors \(d\) and see whether we can find a \(gcd(a_i, n)|d\); if we find one, it means the multiples of \(d\) will be marked. After that, add \(\varphi(\frac{n}{d})\) to the answer.
But why does \(\varphi\) guarantee that nothing is counted twice? This question bothered me at first too, but then it suddenly clicked: consider a repeated case \(d, k_1 d, k_2 d, k_3 d \dots\) and write out:
\[ \begin{gathered} gcd(a_x, n) | d \\ gcd(a_x, n) | k_1 d \\ gcd(a_x, n) | k_2 d \\ \dots \end{gathered} \]
Now we derive:
\[ \begin{gathered} \sum_{i = 1}^r \varphi(\frac{n}{k_i d}), k_i d | n \\ \sum_{x|\frac{n}{d}} \varphi(\frac{n}{x}) = \frac{n}{d} \end{gathered} \]
where \(d\) is the smallest valid (\( \exists i, gcd(a_i, n) | d \)) divisor, and so the final contribution to the answer equals \(\frac{n}{d}\).
The proof is more or less done; I'll go and digest the other parts later.
// running.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 60, MAX_DOM = 1e7 + 200;
ll n, m, arr[MAX_N], phi[MAX_DOM], prime[MAX_DOM], tot, ans;
unordered_map<ll, ll> ump;
bool vis[MAX_DOM];
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll varphi(ll num)
{
if (num < MAX_DOM)
return phi[num];
if (ump.count(num))
return ump[num];
ll ans = (num * (num + 1)) >> 1;
for (ll d = 2, gx; d <= num; d = gx + 1)
{
gx = num / (num / d);
ans -= (gx - d + 1) * varphi(num / d);
}
return ump[num] = ans;
}
void sieve()
{
phi[1] = 1;
for (ll i = 2; i < MAX_DOM; i++)
{
if (!vis[i])
prime[++tot] = i, phi[i] = i - 1;
for (ll j = 1; j <= tot && i * prime[j] < MAX_DOM; j++)
{
vis[i * prime[j]] = true;
if (i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] * phi[prime[j]];
}
}
for (int i = 2; i < MAX_DOM; i++)
phi[i] += phi[i - 1];
}
void solve(int factor)
{
for (int i = 1; i <= m; i++)
if (factor % gcd(arr[i], n) == 0)
{
ans += varphi(n / factor) - varphi(n / factor - 1);
break;
}
}
int main()
{
freopen("running.in", "r", stdin);
freopen("running.out", "w", stdout);
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; i++)
scanf("%d", &arr[i]);
sieve();
for (int i = 1; i * i <= n; i++)
if (n % i == 0)
{
solve(i);
if (n / i != i)
solve(n / i);
}
printf("%lld", n - ans);
return 0;
}
C - Tree
This problem is so freaking annoying.
Normally, you can come up with an \(O(n^3)\) knapsack transition. But it will time out. To optimize, recording the subtree size and the upper-bound size brings it down to \(O(n^2)\), because the number of vertex pairs is on the order of \(O(n^2)\).
// tree.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3005;
int n, limit, head[MAX_N], current, siz[MAX_N], dp[MAX_N][MAX_N], val[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, int dep)
{
siz[u] = 1;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
dfs(edges[i].to, u, dep - 1), siz[u] += siz[edges[i].to];
int bound = min(siz[u], dep);
for (int i = 1; i <= bound; i++)
dp[u][i] = val[u];
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
for (int k = bound; k >= 1; k--)
for (int a = 0; a + k <= bound; a++)
dp[u][a + k] = max(dp[u][a + k], dp[u][k] + dp[edges[i].to][a]);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &limit);
for (int i = 1; i <= n; i++)
scanf("%d", &val[i]);
for (int i = 1, u, v; i <= n - 1; i++)
scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
dfs(1, 0, limit);
int ans = 0;
for (int i = 1; i <= limit; i++)
ans = max(ans, dp[1][i]);
printf("%d", ans);
return 0;
}