[Fortuna OJ] Jul 5th – Group B Problem-Solving Report
A - Rails
A stupid problem, not going to explain it. (Why would Group B have such an easy problem?)
B - Independent Sets
Translated, this problem is just counting independent sets, and it is also a stupid problem.
First, counting problems like this are surely all based on tree DP, so let's set up the states. At first, the state I set was: the number of independent sets in the subtree rooted at this node is \(dp[u]\). Later I found something was off: in the state transition, the counting roughly splits into two kinds, one being the product over the sons' subtrees (including the empty set, so that all the unions can be counted), and the other being the independent sets of the grandsons' subtrees together with this node. So this information has to be stored separately: let \(dp[u][1]\) be the number of independent sets, and \(dp[u][0]\) the number of independent sets of the grandson nodes. So we get the following equations:
\[ \begin{gathered} dp[u][0] = 1, dp[u][1] = 2, deg[u] = 1 \text{ and } u \text{ is not the root} \\ \begin{cases} dp[u][0] = \prod_{v \in son(u)} dp[v][1] - dp[v][0] \\ dp[u][1] = dp[u][0] + \prod_{v \in son(u)} dp[v][1] \end{cases} \end{gathered} \]
Remember to handle the root specially when checking degrees (lost 70 points in the contest because of this).
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100100, mod = 10081;
int head[MAX_N], current, dp[MAX_N][2], deg[MAX_N], 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)
{
if(deg[u] == 1)
{
dp[u][0] = 1, dp[u][1] = 2;
return;
}
int tmp_A = 1, tmp_B = 1;
for(int i = head[u];i!=-1;i=edges[i].nxt)
if(edges[i].to != fa)
{
dfs(edges[i].to, u);
tmp_A = tmp_A * dp[edges[i].to][1] % mod, tmp_B = tmp_B * (dp[edges[i].to][1] - dp[edges[i].to][0] + mod) % mod;
}
dp[u][0] = tmp_B % mod;
dp[u][1] = (tmp_A + tmp_B) % mod;
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d", &n);
for(int i = 1, tmpx, tmpy; i <= n - 1; i++)
scanf("%d%d", &tmpx, &tmpy), addpath(tmpx, tmpy), addpath(tmpy, tmpx), deg[tmpx]++, deg[tmpy]++;
int rt = 1;
deg[rt]++;
dfs(rt, 0);
printf("%d", dp[rt][1]);
return 0;
}
C - Stone Game
Seriously freaking nasty.
This problem is obviously game theory. First, turn our eyes to the case \(k=1\): a table of small cases shows that positions of the form \(n = 2^x\) are losing for the first player. Here is the explanation:
In the case \(k=1\), we break \(n\) down into a binary number. At the very start of the game, the first player has to decide the upper bound of the whole game, and the sequence of moves after that is certainly non-increasing. Consider taking some 1s away from the binary number, and you will find that, in the case of a power of 2, every time you take away a part, at least one 1 is left for the opponent, so the opponent can win the whole game in the last round.
Consider the case \(k = 2\). By a certain theorem, every positive integer can be decomposed into several Fibonacci numbers, and then you find that the ratio between any two non-adjacent Fibonacci numbers is always greater than \(k = 2\). So following this idea, if \(n\) is a Fibonacci number, then it is a losing position (made up of consecutive Fibonacci numbers). The explanation is as follows:
For the case where \(n\) is not a Fibonacci number, \(n\) can always be decomposed into the sum of a non-consecutive subsequence of the Fibonacci sequence. Then, at the places where it is not consecutive, the ratio is always greater than \(k=2\), so we can make the last stone-taking move right there, and that completes a winning sequence; while for the case where \(n\) is a Fibonacci number, every Fibonacci number can be decomposed again, so in the consecutive case, taking a number of stones within twice the last move is simply a piece of cake for the opponent! So this is a losing position.
Consider the general case: we can also construct a sequence similar to the Fibonacci sequence. To construct such an \( \{ a_i \} \), we can open an auxiliary sequence \( \{ b_i \} \), denoting the largest number that can be validly (the order of summation has to follow the order in which the stones are taken) summed from the first \(i\) terms. That way, when considering the next \(a_{i+1}\), we can just transition directly from \(b_i\), and for taking numbers validly, adding one more pointer to the last one that satisfies \(ka_{cursor} < a_i\) is all it takes.
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2000000;
int ai[MAX_N], bi[MAX_N], T, n, k;
int main()
{
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++)
{
scanf("%d%d", &n, &k);
int i = 0, j = 0;
ai[0] = bi[0] = 1;
while (ai[i] < n)
{
i++;
ai[i] = bi[i-1]+1;
while(ai[j + 1] * k < ai[i])
j++;
if(ai[j]* k < ai[i])
bi[i] = bi[j] + ai[i];
else
bi[i] = ai[i];
}
printf("Case %d: ", cas);
if (ai[i] == n)
printf("lose\n");
else
{
int ans = 0;
while (n != 0)
{
if(n >= ai[i])
n -= ai[i], ans = ai[i];
i--;
}
printf("%d\n", ans);
}
}
return 0;
}
膜拜神犇%%%%