### chokudai's blog

By chokudai, history, 12 months ago,

We will hold Toyota Programming Contest 2023 Spring Qual B（AtCoder Beginner Contest 290）.

The point values will be 100-200-300-400-500-500-600-600.

Since this contest is used as a qualification round for a local contest, the problems are adjusted accordingly. For the style / difficulty of problems, please refer to Qual A.

• +62

| Write comment?
 » 12 months ago, # |   +4 How to solve D? :sob:
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 My solution:link
•  » » 12 months ago, # ^ | ← Rev. 3 →   +2 Hint 1if gcd(n, d) = 1, how can you find the answer for any k? Hint 2look at the example n=6, d=3 Hint 2.1try to look for cycles, notice that they all have the same length. Solutionfind the lenght of the cycles, then use that to calculate in which cycle (0, 1, 2...) c you are, given k. The answer is (c + k*d)%n Codeint main(){ int t; cin >> t; while(t--){ ll n, d, k; cin >> n >> d >> k; k--; int cycle_len = n/__gcd(n, d); int c = k/cycle_len; cout << (c + k*d)%n << endl; } } 
•  » » » 12 months ago, # ^ |   0 Means we will mark the number which are between GCD(n , d)?
•  » » » » 12 months ago, # ^ | ← Rev. 2 →   0 I don't understand your question, are you asking for the answer of hint 1? If so, try to come up with many examples of n,d such that gcd(n,d) = 1 and check k = 1, 2, ..., n. I'm sure you'll find something common to all of them that differs from a test case where gcd(n,d) != 1
•  » » » 12 months ago, # ^ |   0 It seems like that the d%=n part is not necessary.
•  » » » » 12 months ago, # ^ |   0 yup, you're right, I edited it.
 » 12 months ago, # |   0 D was easy but I wasted a lot of time since python in my PC is more advanced than OJ.I had to manually compile using python3.8, when I found out that math.lcm does not work in 3.8 version. I still don't understand why do I have penalty even though I failed sample tests.My submissions
 » 12 months ago, # |   +10 Passed F 4 minutes later than the contest end...But still have $\Delta=+47$.
 » 12 months ago, # |   +5 How to F? (I would really appreciate if someone can start with hints, rather than direct answer, but direct answer also works)
•  » » 12 months ago, # ^ | ← Rev. 3 →   0 You can try Google Translate on this: https://atcoder.jp/contests/abc290/editorial/5768Firstly, note that the necessary and sufficient condition for a good tree is sum of X[i] must be 2N — 2.Secondly, the max diameter is equal to the (number of nodes with degree >= 2) + 1 in the good tree. Then, look at this diagram (https://atcoder.jp/contests/abc290/editorial/5792) to see examples (basically arrange all nodes with degree >= 2 in a line then try to attach all the nodes with degree 1).Lastly, use combinatorics to do counting. The implementation is straightforward once you get the formula right.
•  » » 12 months ago, # ^ |   +16 Hint 1Given a degree sequence, how do you construct the best possible tree? AnswerTake 2 nodes with degree 1, create a path with those two nodes as the ends of the diameter and nodes with degree $\geq 2$ in between. All the other nodes with degree 1 can be attached directly to a node on this diameter to create a valid tree. This is the best possible answer. Hint 2For a given $N$, how can we solve in $O(N)$? AnswerLet the number of nodes with degree 1 be $K$. Then, for the other $N-K$ nodes, we need to assign them degrees such that $D_i \geq 2$ and $\sum D_i = 2*(N-1) - K$. We can do this with stars and bars, and we get the formula: $\sum_{K = 2}^{N-1} (N-K+1) \binom{N}{K} \binom{N-3}{K-2}$The first multiplier is the diameter of such a degree sequence, the next is where we choose the nodes with degree 1, and the last part is the stars and bars. Hint 3Can we simplify that sum into some pretty mathematical formula? AnswerThere is probably an easier way to do this, but here is how I thought of it: $\sum_{K=2}^{N-1} (N-K+1) \binom{N}{K} \binom{N-3}{K-2} = \sum_K (N-K+1) \binom{N}{N-K} \binom{N-3}{K-2}$I want to express this as the product of two polynomials $F(x)$ and $G(x)$: $F(x) = \sum_{k} (k+1) \binom{n}{k} x^k$ $G(x) = \sum_{k} \binom{n-3}{k} x^k$Then our answer is $[x^{n-2}]F(x)G(x)$. From the binomial theorem, we know $G(x) = (1+x)^{n-3}$. For $F(x)$, we consider $(1+x)^n$, multiply by $x$, and then differentiate, to get that same sum. Hence, $F(x) = (1+x)^n + nx(1+x)^{n-1}$. Now, multiplying the two, we get: $(1+x)^{2n - 3} + nx(1+x)^{2n - 4}$The coefficient of $x^{n-2}$ would be: $\binom{2n-3}{n-2} + n \binom{2n-4}{n-3}$
 » 12 months ago, # |   +1 how to solve E?
•  » » 12 months ago, # ^ | ← Rev. 3 →   0 Sorry my English is poor. Maybe we can think some easier problems. If we have a sequence B1,B2,B3,...,Bn , how to calculation f(B)? We can calculation how many i that i<=n/2 and B[i]!=B[n-i+1] If we want to find the sum of f(X), we can't find all contiguous subarrays of A So we can find two numbers (i,j) that A[i]!=A[j], we calculation how many contiguous subarrays of A have them. The answer is min(i,n-j+1). So now we can solve this problem by enumerating the two numbers. We can use a vector to solve this problem. Now, When we enumeration the 'i',we can find how many 'j' can contribute i to the answer,and how many 'j' can contribute 'n-j+1' to the answer,you can use binary search. and it is my code: https://atcoder.jp/contests/abc290/submissions/39026156
 » 12 months ago, # |   +1 When will the English Editorial be available?
 » 12 months ago, # |   0 G is simpler than E and F (if don't think how to prove the correctness of solution).
 » 12 months ago, # |   0 How to solve C?
•  » » 12 months ago, # ^ |   0 solution#include using namespace std; int main(){ ios::sync_with_stdio(0);cin.tie(0); int n,arr[300010],k,po=0; cin>>n>>k; for(int i=0;i>arr[i]; sort(arr,arr+n); for(int i=0;i
 » 12 months ago, # |   +5 Solution for $F$ (based on the Japanese editorial):An $x$ is valid if and only if $\sum_{i=1}^{N}{x_i}=2\cdot N-2$ (given that $x_i>0$). Since each edge contributes to the degree of $2$ nodes, i.e., $2\cdot (N-1)$.Also we can always construct a tree with maximal diameter by having all nodes $i$ with $x_i>1$ connected with each other in a chain, connect a leaf to the first node in the chain, another leaf at the end of the chain, and greedily connect the other leaves to the middle nodes in the chain as long as their degrees allow more connections.The previous construction will always work because if there are $y$ remaining leaves to be connected, exactly $y$ connections will be allowed by the middle nodes, since the degrees sum is $2\cdot N -2$ and each added edge reduces the available degrees sum by $2$.So the answer for every $x$ is (the number of $x_i>1$) $+1$. The sum across all valid $x$ is equivalent to $(N+1)\cdot$(number of all valid $x$) $-N\cdot$ (number of valid $x$ where $x_i=1$). By stars and bars theorem, (number of all valid $x$) $=$ ${2\cdot N-3}\choose {N-1}$, and (number of valid $x$ where $x_i=1$) $=$ ${2\cdot N-4}\choose {N-2}$. So the final answer is $(N+1)\cdot$ ${2\cdot N-3}\choose {N-1}$ $-$ $N\cdot$ ${2\cdot N-4}\choose {N-2}$.
 » 12 months ago, # |   0 https://atcoder.jp/contests/abc290/editorial/5813There is a typo in the fourth line of the formula: $|S|$ + (The number of $X \in S$ such that $X_i \ge 2$) $\times N$, $X_i$ should be $X_1$ .