Jekyll2020-11-14T21:59:09+09:00http://kazune-lab.net/feed.xmlUnhappy Go Lucky!Documents by Kazune Takahashi.Kazune TakahashiAtCoder Library Practice Contest2020-09-07T00:00:00+09:002020-09-07T00:00:00+09:00http://kazune-lab.net/contest/2020/09/07/practice2<p><a href="https://atcoder.jp/contests/practice2">AtCoder Library Practice Contest</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/practice2/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/1007_practice2">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---disjoint-set-union"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_a">A - Disjoint Set Union</a></h2> <h2 id="b---fenwick-tree"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_b">B - Fenwick Tree</a></h2> <h2 id="c---floor-sum"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_c">C - Floor Sum</a></h2> <h2 id="d---maxflow"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_d">D - Maxflow</a></h2> <h2 id="e---mincostflow"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_e">E - MinCostFlow</a></h2> <h2 id="f---convolution"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_f">F - Convolution</a></h2> <h2 id="g---scc"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_g">G - SCC</a></h2> <h2 id="h---two-sat"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_h">H - Two SAT</a></h2> <h2 id="i---number-of-substrings"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_i">I - Number of Substrings</a></h2> <h2 id="j---segment-tree"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_j">J - Segment Tree</a></h2> <h2 id="k---range-affine-range-sum"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_k">K - Range Affine Range Sum</a></h2> <h2 id="l---lazy-segment-tree"><a href="https://atcoder.jp/contests/practice2/tasks/practice2_l">L - Lazy Segment Tree</a></h2> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Library Practice ContestAtCoder Beginner Contest 1742020-08-02T00:00:00+09:002020-08-02T00:00:00+09:00http://kazune-lab.net/contest/2020/08/02/abc174<p><a href="https://atcoder.jp/contests/abc174">AtCoder Beginner Contest 174</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/abc174/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0803_ABC174">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---air-conditioner"><a href="https://atcoder.jp/contests/abc174/tasks/abc174_a">A - Air Conditioner</a></h2> <p>Flush whether $X \geq 30$ or not.</p> <h2 id="b---distance"><a href="https://atcoder.jp/contests/abc174/tasks/abc174_b">B - Distance</a></h2> <p>Count the number of such points $(x, y)$ that $x ^ 2 + y ^ 2 \leq d ^ 2$.</p> <h2 id="c---repsept"><a href="https://atcoder.jp/contests/abc174/tasks/abc174_c">C - Repsept</a></h2> <p>Let $a _ n = 7 (10 ^ n - 1) / 9$.</p> <h3 id="solution">Solution</h3> <p>Try $a _ n$ for $n = 1, \dots, 2 \times 10 ^ 7$.</p> <h3 id="verification">Verification</h3> <p>Let $L = 9K$. If $L$ divides $10 ^ n - 1$, then $K$ divides $a _ n$. We see that, if $K = 2$ or $5$, the answer is <code class="language-plaintext highlighter-rouge">-1</code>. Hereinafter, we assume that $L$ and $10$ are coprime. Then, by well-known argument, it follows that $\{ 10 ^ n \in \mathbb{Z} / L \mathbb{Z} \mid 1 \leq n \leq L \} = \mathbb{Z} / L \mathbb{Z}$. Apply brute-force method for it.</p> <h2 id="d---alter-altar"><a href="https://atcoder.jp/contests/abc174/tasks/abc174_d">D - Alter Altar</a></h2> <p>Assume $1$-indexed.</p> <h3 id="solution-1">Solution</h3> <p>The final state will be $RRR \dots RWWW \dots W$. Let $0 \leq k \leq N$ be the index of the last $R$. We fix $k$. Then, the minimum cost is the maximum number of the followings.</p> <ul> <li>The number of whites in the red area.</li> <li>The number of reds in the white area.</li> </ul> <p>This is calculated by cumulated sum in $O(1)$-time for each $k$. The total time complexity is $O(N + N \cdot 1) = O(N)$.</p> <h2 id="e---logs"><a href="https://atcoder.jp/contests/abc174/tasks/abc174_e">E - Logs</a></h2> <h3 id="solution-2">Solution</h3> <p>This is done by binary search for the targeted answer.</p> <p>We consider how many times we have to cut the logs to have an situation where all of them are the length of less than $X$. This is calculated by the following. $f(X) = \sum _ {i \in N} \left( \frac{A _ i + (X - 1)}{X} - 1 \right).$ We determine $f(X) \leq K$ or not.</p> <h2 id="f---range-set-query"><a href="https://atcoder.jp/contests/abc174/tasks/abc174_f">F - Range Set Query</a></h2> <p>Assume $0$-indexed.</p> <h3 id="solution-3">Solution</h3> <p>We sort the queries in $r$’s ascending order and we consider $r = 0, 1, \dots, N - 1$. Then, <strong>we just keep the rightmost balls for each color</strong> and just count up for those balls for $[l, r]$.</p> <p>We use $memo[color]$ to denote the index of the rightmost ball of the color $color$ so far. We define $tree[index] = 1$ if the ball of $index$ is of the balls we are now considering or $0$ if not. We maintain $tree$ with updates of $memo$ and take the sum in $tree$ for the range of $[l, r]$ for each query. Thus, we use $tree$ for segment tree or binary indexed tree.</p> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Beginner Contest 174AtCoder Beginner Contest 1732020-07-05T00:00:00+09:002020-07-05T00:00:00+09:00http://kazune-lab.net/contest/2020/07/05/abc173<p><a href="https://atcoder.jp/contests/abc173">AtCoder Beginner Contest 173</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/abc173/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0705_ABC173">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---payment"><a href="https://atcoder.jp/contests/abc173/tasks/abc173_a">A - Payment</a></h2> <p>If you pay by $10,000$-yen bill, the shopper gives back the change by coins and 1,000 yen bills. Flush that sum of value of the coins, i.e., $(10000 - n) \% 1000$.</p> <h2 id="b---judge-status-summary"><a href="https://atcoder.jp/contests/abc173/tasks/abc173_b">B - Judge Status Summary</a></h2> <p>It is better to use <code class="language-plaintext highlighter-rouge">map&lt;string, ll&gt; m;</code>.</p> <h2 id="c---h-and-v"><a href="https://atcoder.jp/contests/abc173/tasks/abc173_c">C - H and V</a></h2> <p>Apply bit-wise brute-force method.</p> <h2 id="d---chat-in-a-circle"><a href="https://atcoder.jp/contests/abc173/tasks/abc173_d">D - Chat in a Circle</a></h2> <p>Assume $0$-indexed. We pick $a _ 0, a _ 1, \dots$ and insert in the rounded table.</p> <h3 id="observation">Observation</h3> <p>First, we prepare lemmas to assert that $\{ a _ i \}$ is in descending order.</p> <blockquote> <p><strong>Lemma D.1</strong>: Assume that $\{ a _ i \}$ has two adjacent terms $a _ i$ and $a _ {i + 1}$ with $a _ i &lt; a _ {i + 1}$. Fix the inserting points. Let $X$ be the score of it and $Y$ be the score of the sequence where we swap <strong>the value with the place</strong> of $a _ {i}$ and $a _ {i + 1}$. Then, it follows that $X \leq Y$.</p> </blockquote> <p>Proof: If the $i$-th element does not effect the $(i + 1)$-th element, it follows that $X = Y$. If it does, it means the $i$-th element is adjacent to the $(i + 1)$-th element. Since $a _ {i + 1} &gt; a _ {i}$, its comfort point of the swapped version is greater than or equal to that of the original one. Therefore, we have $X \leq Y$.</p> <blockquote> <p><strong>Lemma D.2</strong>: Assume that $\{ a _ i \}$ attains the maximum score. We can assume that $\{ a _ i \}$ is sorted in descending order.</p> </blockquote> <p>Proof: We keep applying Lemma D.1 to the extent that $\{ a _ i \}$ in descending order.</p> <p>Next, we apply greedy method. For this purpose, we resolve duplicated numbers.</p> <blockquote> <p><strong>Definition D.3</strong>: For $a _ i$s, we define $(a _ i, i) &gt; (a _ j, j)$ if and only if $a _ i &gt; a _ j$ or ($a _ i = a _ j$ and $i &lt; j$). Hereinafter, we just use $a _ i$ to denote $(a _ i, i)$. The inequality $&lt;$ is defined in the same way.</p> </blockquote> <p>Hereinafter, whenever we compare $a _ i$s, we use this $&lt;$. Note that for any two elements $a _ i$ and $a _ j$ with $i \neq j$, there is no possibility of $a _ i = a _ j$. Hence, $\{ a _ i \}$ is strictly decreasing. In addition, even if we replace $\min$ generated by this $&lt;$ in the definition of the comfort point, we see that the result is the same.</p> <blockquote> <p><strong>Definition D.4</strong>: An element $a _ i$ is <em>touched</em> if, after it is inserted, it is adopted for the comfort point of other elements.</p> </blockquote> <p>Note that this is well-defined since we have replaced the definition of $&lt;$ for $\{ a _ i \}$ in Definition D.3.</p> <blockquote> <p><strong>Lemma D.5</strong>: Assume that $\{ a _ i \}$ is in descending order.</p> <ol> <li>The first element $a _ 0$ is touched just once.</li> <li>For $i = 1, \dots, n - 1$, The $i$-th element $a _ i$ is touched at most twice.</li> </ol> </blockquote> <p>Proof: For 1., since $a _ 0$ is the largest element of $\{ a _ i \}$, it is touched only by $a _ 1$ and never after that. For 2., fix $i = 1, \dots, n - 1$. Suppose that $a _ i$ has been touched by $a _ j$. We see that $i &lt; j$ and $a _ i \geq a _ j$, and thus $a _ i &gt; a _ j$. Therefore, if we insert any element between $a _ i$ and $a _ j$, $a _ i$ is never touched by it and, letting its element be $a _ k$, we have $a _ i &gt; a _ k$ and $a _ k$ has replaced $a _ j$ as $a _ i$’s new adjacent. Hence, $a _ i$ can be touched by just twice; from its left hand side or its right hand side.</p> <p>By Lemma D.2 and Lemma D.5, the upper bound of the score is the first $(n - 1)$ elements of the queue $\{ a _ 0, a _ 1, a _ 1, a _ 2, a _ 2, \dots \}$, where $\{ a _ i \}$ is sorted in descending order.</p> <blockquote> <p><strong>Lemma D.6</strong>: The upper bound above is attained.</p> </blockquote> <p>Proof: Let $v$ be the queue above. When we insert $a _ i$ and $a _ {i + 1}$ with $i = 1, 3, 5, \dots$, we just put $a _ i$ in the left hand side of $b = v[i - 1] = v[i]$ and $a _ {i + 1}$ in the other side. Before this moment, $b$ is surrounded by the elements greater than $b$. Therefore, their comfort points is both $b$. This completes the proof.</p> <p><img src="/images/2020-07-05-D.png" alt="D example" /></p> <h3 id="solution">Solution</h3> <p>We sort $\{ a _ i \}$ in descending order. We sum up the first $(n - 1)$ elements of $\{ a _ 0, a _ 1, a _ 1, a _ 2, a _ 2, \dots \}$ and flush its sum.</p> <h2 id="e---multiplication-4"><a href="https://atcoder.jp/contests/abc173/tasks/abc173_e">E - Multiplication 4</a></h2> <p>Assume $0$-indexed. If $n = k$, the answer is deterministic. <strong>Hereinafter, we assume $n &gt; k$.</strong></p> <h3 id="observation-1">Observation</h3> <blockquote> <p><strong>Lemma E.1</strong>: The answer is negative if and only if $k \% 2 = 1$ and all the value of $a _ i$ are positive.</p> </blockquote> <p>Proof: Suppose that there exists a nonnegative $b$ in the multiset $\{ a _ i \}$. We choose arbitrary $k$ elements from $\{ a _ i \} \setminus \{ b \}$. If the product of them is negative, there is at least a negative element. We eliminate it and we multiply $b$ instead of it to gain a nonnegative product. Then, suppose that all of $\{ a _ i \}$ are negative. In this case, the condition that the answer is negative is that $k \% 2 = 1$.</p> <p>Hereinafter, we assume that $\{ a _ i \}$ is sorted in descending order.</p> <blockquote> <p><strong>Lemma E.2</strong>: If the answer is nonnegative, the answer is the product of the first $k$ terms of $\{ a _ i \}$.</p> </blockquote> <p>Proof: By Lemma E.1, $\{ a _ i \}$ consists of nonnegatives, from which the conclusion follows.</p> <h3 id="implementation">Implementation</h3> <p>Hereinafter, we assume that the answer is nonnegative. We use two pointers $l = 0$ and $r = n - 1$.</p> <p>If $k \% 2 = 1$, we use $a[l {+ +}]$, regardless of its value being positive or not.</p> <p>We compare $a[l]a[l + 1]$ and $a[r]a[r - 1]$. We choose the greater one and multiply the result with it and proceed the pointer we have used by two. We do this manipulation for $k / 2$ times. This cannot run in out-of-range.</p> <p>We prove that this manipulation is valid.</p> <blockquote> <p><strong>Lemma E.3</strong>: There is no possibility that the result is negative.</p> </blockquote> <p>Proof: Assume to the contrary that the answer is negative. Then, there exists $l \leq r$ so that $a[l]a[l + 1]$ and $a[r]a[r - 1]$ are negative. Since $\{ a _ i \}$ is in descending order, it follows that $\{ a _ i \}$ has no zeros and $a[l] = a[r - 1] &gt; 0$ and $a[l + 1] = a[r] &lt; 0$ and picks their product, which contradicts the assumption that $n &gt; k$. We complete the proof.</p> <blockquote> <p><strong>Lemma E.4</strong>: If the result is zero, the answer is zero.</p> </blockquote> <p>Proof: There exists $l \leq r$ so that $a[l]a[l + 1]$ and $a[r]a[r - 1]$ are non-positive and we have picked either of those pairs. If either of them were negative, it would follow that there is no zeros. Therefore, we see $a[l]a[l + 1] = a[r]a[r - 1] = 0$. Since $\{ a _ i \}$ is in descending order, it follows that $a[l + 1] = a[r] = 0$. From the fact that we have picked either of those pairs, it follows that there are at most $k - 1$ nonzero elements in \{ a _ i \}. Therefore, the answer is $0$.</p> <blockquote> <p><strong>Lemma E.5</strong>: If the result is positive, the answer is its result.</p> </blockquote> <p>Let $X$ be the set of the elements we have chosen. Let $Y$ be a set of the elements that attains the maximum. Let $h _ X, h _ Y$ be the product of $X, Y$, respectively. By definition, it follows that $0 &lt; h _ X \leq h _ Y$. Suppose that $X \neq Y$. Then, $X$ and $Y$ do not contain any zeros. Let $p$ and $q$ be the number of positives and negatives of $Y$, respectively. Note that $q \% 2 = 0$. Let $Z = \{ a _ 0, \dots, a _ {p - 1} \} \cup \{ a _ {n - q}, \dots, a _ {n - 1} \}$ and $h _ Z$ be its product. Since the definition of $p$ and $q$ and the fact that $\{ a _ i \}$ is descending order, the former and the latter part of $Z$ are composed of positives and negatives, respectively and, considering the absolute value, It follows that $h _ Y \leq h _ Z$. Moreover, we consider the product of each pair of adjacent terms. Seeing the construction of $X$, it follows that $h _ Z \leq h _ X$. Hence, we conclude that $h _ X = h _ Y$.</p> <h3 id="references">References</h3> <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">E通らなかったけど<br />aソート<br />a0が負ならそのまま前からK個終わり<br />Kが奇ならa0とってスタート<br />以下Kが0になるまでaの頭2個の積と尻2個の積を比べて大きい方を取る<br />でやばいパターンが見つからん</p>&mdash; みやこ (@MiKo_IZUMI) <a href="https://twitter.com/MiKo_IZUMI/status/1279775911231885312?ref_src=twsrc%5Etfw">July 5, 2020</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">この実装に加えて答えが0未満になったときは前からk個とる、でいけた<br /><br />場合分け一つだけで済むし、めちゃくちゃ軽くてビックリしてる(嘘だったらごめん<a href="https://t.co/o6hmJDRaJp">https://t.co/o6hmJDRaJp</a></p>&mdash; かいとccs (@tran0826) <a href="https://twitter.com/tran0826/status/1280086560281554946?ref_src=twsrc%5Etfw">July 6, 2020</a></blockquote> <script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> <h2 id="f---intervals-on-tree"><a href="https://atcoder.jp/contests/abc173/tasks/abc173_f">F - Intervals on Tree</a></h2> <p>Assume $0$-indexed.</p> <h3 id="solution-1">Solution</h3> <p>Let $c(G)$ be the number of the connected components of the forest $G$. Let $e(G)$ the number of the edges of $G$ and let $v(e)$ be that of the vertexes of $G$. Then, it follows that $c(G) = v(G) - e(e).$ Let $G$ be the original graph. Let $G(L, R)$ be the forest where its vertexes consist of $[L, R]$ and its edges consists of the edges whose endpoints both belongs to $[L, R]$. Then, what we are required to calculate is $\sum _ {0 \leq L \leq R &lt; N} c(G(L, R)) = \sum _ {0 \leq L \leq R &lt; N} v(G(L, R)) - \sum _ {0 \leq L \leq R &lt; N} e(G(L, R)). \tag{F.1}$</p> <p>First, we calculate the first term of (F.1). It follows that \begin{align} N _ v = \sum _ {0 \leq L \leq R &lt; N} v(G(L, R)) &amp;= \sum _ {0 \leq L \leq R &lt; N} \sum _ {i = 0} ^ {N - 1} \delta _ {L \leq i \leq R} \\<br /> &amp;= \sum _ {i = 0} ^ {N - 1} \sum _ {0 \leq L \leq R &lt; N} \delta _ {L \leq i \leq R} \\<br /> &amp;= \sum _ {i = 0} ^ {N - 1} (i + 1)(N - i). \end{align} This is calculated in $O(n)$. We don’t have to determine its closed form.</p> <p>Next, we calculate the second term of (F.1). We write $e _ i = (u _ i, v _ i)$ being the $i$-th edges. Without loss of generality, we can assume that $u _ i &lt; v _ i$. Then, using the same idea, it follows that \begin{align} N _ e = \sum _ {0 \leq L \leq R &lt; N} e(G(L, R)) &amp;= \sum _ {i = 0} ^ {N - 2} \sum _ {0 \leq L \leq R &lt; N} \delta _ {L \leq u _ i} \delta _ {v _ i \leq R} \\<br /> &amp;= \sum _ {i = 0} ^ {N - 2} (u _ i + 1)(n - v _ i). \end{align} This is also done in $O(n)$-time. The answer is $N _ v - N _ e$.</p> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Beginner Contest 173Introduction to Heuristics Contest2020-06-28T00:00:00+09:002020-06-28T00:00:00+09:00http://kazune-lab.net/contest/2020/06/28/intro-heuristics<p><a href="https://atcoder.jp/contests/intro-heuristics">Introduction to Heuristics Contest</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/intro-heuristics/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0628_intro-heuristics">My source codes (GitHub)</a></li> </ul> <p>I participated in this contest and got score $2$.</p>Kazune TakahashiIntroduction to Heuristics ContestAtCoder Beginner Contest 1722020-06-27T00:00:00+09:002020-06-27T00:00:00+09:00http://kazune-lab.net/contest/2020/06/27/abc172<p><a href="https://atcoder.jp/contests/abc172">AtCoder Beginner Contest 172</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/abc172/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0627_ABC172">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---calc"><a href="https://atcoder.jp/contests/abc172/tasks/abc172_a">A - Calc</a></h2> <p>Flush $a + a ^ 2 + a ^ 3$.</p> <h2 id="b---minor-change"><a href="https://atcoder.jp/contests/abc172/tasks/abc172_b">B - Minor Change</a></h2> <p>Let $N$ be the length of $S$ or $T$. For $i = 0, \dots, N - 1$, if $S[i] = T[i]$, increment the count. Flush the result.</p> <h2 id="c---tsundoku"><a href="https://atcoder.jp/contests/abc172/tasks/abc172_c">C - Tsundoku</a></h2> <p>Assume $0$-indexed. This is not a greedy simulation problem, but a cumulated sum problem.</p> <p>Let $sumA$ and $sumB$ be the cumulated sum of $A$ and $B$, respectively. For $i = 0, \dots, N$, let $L = K - sumA[i]$. If $L &lt; 0$, <code class="language-plaintext highlighter-rouge">continue</code>. Let $j =$ <code class="language-plaintext highlighter-rouge">upper_bound(sumB.begin(), sumB.end(), L) - sum.begin()</code> $- 1$. Here, $i + j$ is a possible number of books we can read. This is valid because $sumB$ is strictly increasing since $B _ i &gt; 0$.</p> <h2 id="d---sum-of-divisors"><a href="https://atcoder.jp/contests/abc172/tasks/abc172_d">D - Sum of Divisors</a></h2> <h3 id="solution-1-on-log-n">Solution #1: $O(N \log N)$</h3> <p>Let $cnt[i] =$ be the number of divisors of $i$. We can obtain the result in the same way of Sieve of Eratosthenes. Then, calculate $\sum _ {i = 1} ^ N i \times cnt[i]$. The time complexity is $O(N \log N)$.</p> <h3 id="solution-2-on">Solution #2: $O(N)$</h3> <p>This problem can be directly implemented as follows.</p> <div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="n">ll</span> <span class="n">ans</span><span class="p">{</span><span class="mi">0</span><span class="p">};</span> <span class="k">for</span> <span class="p">(</span><span class="k">auto</span> <span class="n">i</span><span class="p">{</span><span class="mi">1</span><span class="p">};</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">N</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="k">auto</span> <span class="n">j</span><span class="p">{</span><span class="mi">1</span><span class="p">};</span> <span class="n">j</span> <span class="o">&lt;=</span> <span class="n">N</span><span class="p">;</span> <span class="o">++</span><span class="n">j</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">ans</span> <span class="o">+=</span> <span class="n">i</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">ans</span><span class="p">;</span> </code></pre></div></div> <p><strong>We can swap the loop.</strong> If we swap $i$’s loop and $j$’s loop, we see that this problem is equivalent to the task that for each $j = 1, \dots, N$, we add up the sum $j, 2j, \dots, (N / j) * j$. This sum is calculated as follows. $j \times \frac{(N / j) ((N / j) + 1)}{2}.$ Hence, the total complexity is $O(N)$.</p> <h2 id="e---neq"><a href="https://atcoder.jp/contests/abc172/tasks/abc172_e">E - NEQ</a></h2> <p>Assume $0$-indexed.</p> <h3 id="solution">Solution</h3> <p>This is an inclusive-exclusive principle problem.</p> <p>The second condition is equivalent to that we don’t use each number more than twice. If $M &lt; N$, the answer is $0$.</p> <p>For $x \subset N$, we think of the number of placement for $(A, B)$ where it holds that $A _ i = B _ i$ for $i \in x$ (It may or may not hold $A _ i \neq B _ i$ for each $i \in N \setminus x$). This number is effected only by $\lvert x \rvert$. Thus, let $K = 0, \dots, N$ and let $f(K)$ be the number of placement for $(A, B)$ with this condition. The answer is $\sum _ {K = 0} ^ N (-1) ^ K \begin{pmatrix} N \\ K \end{pmatrix} f(K).$</p> <p>We choose $K$ elements from $N$ for the $i \in N$ with $A _ i = B _ i$. We choose $K$ numbers from $M$ for $A _ i = B _ i$. This is $M! / (M - K)!$. From the remaining elements, we choose $(N - K)$ elements for $A$ and $B$ independently. This is $((M - K)! / (M - N)!) ^ 2$. Then, we have $f(K) = \frac{M!}{(M - K)!} \left( \frac{(M - K)!}{(M - N)!} \right) ^ 2.$</p> <h2 id="f---unfair-nim"><a href="https://atcoder.jp/contests/abc172/tasks/abc172_f">F - Unfair Nim</a></h2> <h3 id="solution-1">Solution</h3> <p>Let $A’ = A _ 1$, $B’ = A _ 2$ and $C = \bigoplus _ {i = 3} ^ N A _ i$. Let $A$ and $B$ be the results. Let $S = A’ + B’$. The condition is \begin{align} A + B &amp;= S, \tag{F.1} \\<br /> A \oplus B &amp;= C, \tag{F.2} \\<br /> 0 &lt; A &amp;\leq A’. \tag{F.3} \end{align} Here $A + B = A \oplus B + 2 (A \land B)$. Letting $D = (S - C) / 2$, we see that (F.1) is equivalent to the following condition. $A \land B = D. \tag{F.4}$ Of course, we have to check $S - C \geq 0$ and $(S - C) \% 2 = 0$ before that.</p> <p>To consider (F.2) and (F.4), we think of $a \oplus b$ and $a \land b$ with $0 \leq a, b \leq 1$ as follows.</p> <table> <thead> <tr> <th>$(a, b)$</th> <th>$a \oplus b$</th> <th>$a \land b$</th> </tr> </thead> <tbody> <tr> <td>$(0, 0)$</td> <td>$0$</td> <td>$0$</td> </tr> <tr> <td>$(1, 0)$</td> <td>$1$</td> <td>$0$</td> </tr> <tr> <td>$(0, 1)$</td> <td>$1$</td> <td>$0$</td> </tr> <tr> <td>$(1, 1)$</td> <td>$0$</td> <td>$1$</td> </tr> </tbody> </table> <p>There is no possibility for $(a, b)$ with $a \oplus b = a \land b = 1$. Therefore, if $C \land D \neq 0$, the answer is negative. Hereinafter, assume $C \land D = 0$. Then, for each $i$ with $D[i] = 1$, we must have $A[i] = 1$. For each $i$ with $C[i] = 1$, we have choice that we let $A[i] = 0$ or $1$. Thus, temporality, we let $A = D$.</p> <p>We maximize $A$ with (F.3). We apply a greedy algorithm for this. We try $i$ with $C[i] = 1$ in descending order. If $A + (C[i] \mathbin{&lt; &lt;} i) \leq K$, we execute $A \gets A + (C[i] \mathbin{&lt; &lt;} i)$. Finally, we confirm (F.3) and flush $A’ - A$.</p> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Beginner Contest 172AtCoder Beginner Contest 1712020-06-21T00:00:00+09:002020-06-21T00:00:00+09:00http://kazune-lab.net/contest/2020/06/21/abc171<p><a href="https://atcoder.jp/contests/abc171">AtCoder Beginner Contest 171</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/abc171/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0621_ABC171">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---alphabet"><a href="https://atcoder.jp/contests/abc171/tasks/abc171_a">A - alphabet</a></h2> <p>We use the following if-clause <code class="language-plaintext highlighter-rouge">if ('A' &lt;= x &amp;&amp; x &lt;= 'Z')</code>.</p> <h2 id="b---mix-juice"><a href="https://atcoder.jp/contests/abc171/tasks/abc171_b">B - Mix Juice</a></h2> <p>Sort and pick $K$ fruits in ascending order.</p> <h2 id="c---one-quadrillion-and-one-dalmatians"><a href="https://atcoder.jp/contests/abc171/tasks/abc171_c">C - One Quadrillion and One Dalmatians</a></h2> <h3 id="observation">Observation</h3> <p>This is not $26$-based numbers. If we write these numbers in $10$-based, we have the followings. $0, 1, 2, 3, \dots, 9, 01, 02, 03, \dots, 09, 10, 11, \dots, 99, 001, 002, \dots$ But for each blocks, it is $26$-based numbers. The $K$-th block gathers $26 ^ K$ numbers whose digit is $K$.</p> <p><img src="/images/2020-06-21-C.png" alt="C" /></p> <h3 id="determine-the-block">Determine the block</h3> <p>This is just subtract $26, 26 ^ 2, \dots$ from $N$ as long as $N$ is positive.</p> <h3 id="determine-the-string">Determine the string</h3> <p>We convert $N$ into $0$-indexed. Then, we can determine its string by <code class="language-plaintext highlighter-rouge">/</code> and <code class="language-plaintext highlighter-rouge">%</code> in the normal way.</p> <h2 id="d---replacing"><a href="https://atcoder.jp/contests/abc171/tasks/abc171_d">D - Replacing</a></h2> <p>Let $H$ be the histogram of $A$ and $S$ be its sum. For each query $(B, C)$, we execute the following. \begin{align} H[C] &amp;\mathbin{ {+} {=} } H[B], \\<br /> S &amp;\mathbin{ {+} {=} } (C - B) H[B], \\<br /> H[B] &amp;\gets 0. \end{align}</p> <h2 id="e---red-scarf"><a href="https://atcoder.jp/contests/abc171/tasks/abc171_e">E - Red Scarf</a></h2> <p>Let $x _ i$ be the number that $i$-th person has. Let $X = \oplus _ {i \in N} x _ i$. It holds that $a _ i = \bigoplus _ {j \in N \setminus \{ i \} } x _ j = X \oplus x _ i$ Therefore, we have the following relation. $\bigoplus _ {i \in N} a _ i = (X \oplus \dots \oplus X) \oplus \bigoplus _ {i \in N} x _ i = 0 \oplus X = X,$ where $(X \oplus \dots \oplus X) = 0$ since $N$ is even. We can calculate $x _ i$ by the following relation. $x _ i = X \oplus a _ i.$</p> <h2 id="f---strivore"><a href="https://atcoder.jp/contests/abc171/tasks/abc171_f">F - Strivore</a></h2> <h3 id="observation-1">Observation</h3> <p>We count the number of $T$s of which $S$ is a subsequence. Let $N = \lvert S \rvert$.</p> <h3 id="solution-1">Solution #1</h3> <p>$T$ is formed as follows.</p> <p>$T = \overline{S} \dots \overline{S} S \overline{S} \dots \overline{S} S \dots S[N - 1] * \dots * \tag{F.1}$</p> <p>Here, $\bar{S[i]}$ means a character except $S[i]$ and $*$ means an arbitrary character.</p> <p>Let $k$ be the length of $* \dots *$. We will try all $k$s with $0 \leq k \leq K$. The number of this possible frame of (F.1) is $\begin{pmatrix} N - 1 + K - k \\ N - 1 \end{pmatrix}.$ For each frame, the number of the string within that frame is $(26 - 1) ^ {K - k} 26 ^ {K}.$ Therefore, the answer is $\sum _ {k = 0} ^ K \begin{pmatrix} N - 1 + K - k \\ N - 1 \end{pmatrix} (26 - 1) ^ {K - k} 26 ^ {K}.$</p> <h3 id="solution-2">Solution #2</h3> <p>Thinking of the above argument, we come up with the string $S$ itself does not effect the answer, we just use the length $N$. Hence, it suffice to fix $S = aaa \dots aaa$.</p> <p>Then, the condition for $T$ is that $\lvert T \rvert = N + K$ and that $T$ has more than $N$ ‘a’s. Therefore, the answer is $\sum _ {a = N} ^ {N + K} \begin{pmatrix} N + K \\ a \end{pmatrix} (26 - 1) ^ {N + K - a}.$</p> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Beginner Contest 171AtCoder Grand Contest 0462020-06-20T00:00:00+09:002020-06-20T00:00:00+09:00http://kazune-lab.net/contest/2020/06/20/agc046<p><a href="https://atcoder.jp/contests/agc046">AtCoder Grand Contest 046</a></p> <p><em>The explanation will be written after the series of AGC/ARC on May and July 2020.</em></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/agc046/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0620_AGC046">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---takahashikun-the-strider"><a href="https://atcoder.jp/contests/agc046/tasks/agc046_a">A - Takahashikun, The Strider</a></h2> <h2 id="b---extension"><a href="https://atcoder.jp/contests/agc046/tasks/agc046_b">B - Extension</a></h2> <h2 id="c---shift"><a href="https://atcoder.jp/contests/agc046/tasks/agc046_c">C - Shift</a></h2> <h2 id="d---secret-passage"><a href="https://atcoder.jp/contests/agc046/tasks/agc046_d">D - Secret Passage</a></h2> <h2 id="e---permutation-cover"><a href="https://atcoder.jp/contests/agc046/tasks/agc046_e">E - Permutation Cover</a></h2> <h2 id="f---forbidden-tournament"><a href="https://atcoder.jp/contests/agc046/tasks/agc046_f">F - Forbidden Tournament</a></h2> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Grand Contest 046AtCoder Beginner Contest 1702020-06-14T00:00:00+09:002020-06-14T00:00:00+09:00http://kazune-lab.net/contest/2020/06/14/abc170<p><a href="https://atcoder.jp/contests/abc170">AtCoder Beginner Contest 170</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/abc170/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0615_ABC170">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---five-variables"><a href="https://atcoder.jp/contests/abc170/tasks/abc170_a">A - Five Variables</a></h2> <p>Find such $i$ that $A[i] = 0$.</p> <h2 id="b---crane-and-turtle"><a href="https://atcoder.jp/contests/abc170/tasks/abc170_b">B - Crane and Turtle</a></h2> <p>Just try all possibilities. The head of crane $i$ runs in $[0, X]$ and let $j = X - i$. Then, we check $2i + 4j = Y$ or not.</p> <h2 id="c---forbidden-list"><a href="https://atcoder.jp/contests/abc170/tasks/abc170_c">C - Forbidden List</a></h2> <p>Just try all possibilities for $x \in [-100, 200]$.</p> <h2 id="d---not-divisible"><a href="https://atcoder.jp/contests/abc170/tasks/abc170_d">D - Not Divisible</a></h2> <p>Let $L = 10 ^ 6 + 10$.</p> <h3 id="solution">Solution</h3> <p>The main concept is the way of sieve of Eratosthenes.</p> <p>We create two tables.</p> <blockquote> <p>$cnt[i] = \sharp \{ j \in N \mid A _ j = i \}$, <br /> $valid[i] =$ <code class="language-plaintext highlighter-rouge">true</code> if the number $i$ cannot be divided by other $A[j] \neq i$.</p> </blockquote> <p>Then, the answer is $\sharp \{ 1 \leq i \leq L \mid valid[i] \land cnt[i] = 1 \}.$</p> <p>We try $i \in [1, L]$ with $cnt[i] &gt; 0$. For $j = 2, 3, \dots$, $valid[i \times j] \gets$ <code class="language-plaintext highlighter-rouge">false</code>.</p> <p>The total time complexity is $O(L \log L)$.</p> <h2 id="e---smart-infants"><a href="https://atcoder.jp/contests/abc170/tasks/abc170_e">E - Smart Infants</a></h2> <p>Let $L = 10 ^ 5 + 10$. Assume $0$-indexed.</p> <h3 id="my-solution">My Solution</h3> <p>Let $tree$ be the RangeMinQuery-type segment tree. We update $tree$ and ask it the answer for each query. By each transfer, only two gardens will be updated.</p> <p>We define as follows.</p> <blockquote> <p><code class="language-plaintext highlighter-rouge">vector&lt;multiset&lt;int, greater&lt;int&gt;&gt;&gt;</code> $garden[i] =$ the multiset of the scores of the infants belonging to the garden $i$,<br /> <code class="language-plaintext highlighter-rouge">SegTree&lt;int, int&gt;</code> $tree[i] =$ the maximum score of the infants belonging to the garden $i$ if the garden is not empty; otherwise $\infty$.</p> </blockquote> <p>For each transfer, we regard $(score, src, dst)$ as a query. We erase $score$ from $garden[src]$ and insert it into $garden[dst]$. We take the maximum elements from each garden (or $\infty$ if it is empty) and update $tree[src]$ and $tree[dst]$. Then, ask $tree$ the answer.</p> <h4 id="time-complexity">Time complexity</h4> <p>The initial setting takes $O(N \log N + L \log L)$-time. Each update takes $O(\log L + \log N)$-time. The total time complexity will be $O(N \log N + L \log L + Q \log L + Q \log N)$-time.</p> <h3 id="suggested-solution">Suggested Solution</h3> <p>We don’t have to use RangeMinQuery-type segment tree for $tree$. We use <code class="language-plaintext highlighter-rouge">multiset&lt;int&gt;</code> for it. too.</p> <h2 id="f---pond-skater"><a href="https://atcoder.jp/contests/abc170/tasks/abc170_f">F - Pond Skater</a></h2> <h3 id="observation">Observation</h3> <p>The number of the maximal steps $K$ is so large. We have to reduce the possibilities to solve this problem. In this kind of the problems, the key idea is to decompose the transition into more fundamental steps. We keep how many times the frog has continued the same direction. Then, we just try $4$ possibilities for each query.</p> <p>This problem is about the extended version of Dijkstra’s algorithm.</p> <h3 id="my-solution-extended-dijkstra">My solution (extended Dijkstra)</h3> <p>We regard $(x, y, k)$ as an extended point, with $(x, y) \in H \times W$ being the point, $k \in 4$ being the direction.</p> <p>We regard $(d, md)$ as an extended distance, where $d$ is the actual distance from the starting point, and $ms$ is the step where the frog has continued the same direction.</p> <h3 id="dp-part">DP part</h3> <h4 id="definition">Definition</h4> <p>Type definitions:</p> <blockquote> <p><code class="language-plaintext highlighter-rouge">using Score = tuple&lt;int, int&gt;;</code> $(s, ms)$ for the value of $dp$, <br /> <code class="language-plaintext highlighter-rouge">using Info = tuple&lt;int, int, int, int, int&gt;;</code> $(s, ms, x, y, k)$ for the value of <code class="language-plaintext highlighter-rouge">min_heap</code>.</p> </blockquote> <p>DP definitions:</p> <blockquote> <p><code class="language-plaintext highlighter-rouge">vector&lt;vector&lt;vector&lt;Score&gt;&gt;&gt;</code> $dp[x][y][k] =$ the minimum score in $(x, y, k)$, <br /> <code class="language-plaintext highlighter-rouge">min_heap&lt;Info&gt;</code> $H$.</p> </blockquote> <h4 id="initial-state">Initial State</h4> <p>$H \gets (0, 0, sx, sy, k)$ for $k \in 4$. We set $dp[x][y][k] =$ <code class="language-plaintext highlighter-rouge">Empty</code> or <code class="language-plaintext highlighter-rouge">Invalid</code>.</p> <h4 id="answer">Answer</h4> <p>We try $dp[gx][gy][k]$ for all $k \in 4$. If they all are empty, the answer is <code class="language-plaintext highlighter-rouge">-1</code>. Otherwise, it is the minimum value of them. We regard $(s, ms) \mapsto s$ if $ms = 0$ or $(s, ms) \mapsto s + 1$ if $ms &gt; 0$.</p> <h4 id="transition">Transition</h4> <p>We get $(s, ms; x _ s, y _ s, k _ s) \gets H$ and execute <code class="language-plaintext highlighter-rouge">H.pop();</code>. If $dp[x _ s][y _ s][k _ s] =$ <code class="language-plaintext highlighter-rouge">Empty</code>, execute <code class="language-plaintext highlighter-rouge">continue;</code>.</p> <p>First, we think of the change of the direction. For $k _ d \in 4$ with $k _ d \neq k _ s$, the new point is $(x _ s, y _ s, k _ d)$. If it is valid and $dp[x _ s][y _ s][k _ d]$ is <code class="language-plaintext highlighter-rouge">Empty</code>, we execute the following. $H \gets \begin{cases} (s + 1, 0; x _ s, y _ s, k _ d) &amp; ms &gt; 0, \<br /> (s, 0; x _ s, y _ s, k _ d) &amp; ms = 0. \end{cases}$</p> <p>Second, we think of the proceeding. The new point is $(x _ d, y _ d, k _ d) = (x _ s + dx[k _ s], y _ s + dy[k _ s], k _ s)$. If it is valid and $dp[x _ d][y _ d][k _ d]$ is <code class="language-plaintext highlighter-rouge">Empty</code>, we execute the following. $H \gets \begin{cases} (s + 1, 0; x _ d, y _ d, k _ d) &amp; ms = K - 1, \<br /> (s, ms; x _ d, y _ d, k _ d) &amp; ms &lt; K - 1. \end{cases}$</p> <h3 id="another-solution-1-using-set-for-pre-visited-points">Another Solution #1 (Using set for pre-visited points)</h3> <p>We reduce $O(HWK)$-BFS to $O(HW (\log H + \log W)))$-BFS by using <code class="language-plaintext highlighter-rouge">vector&lt;set&lt;Point&gt;&gt; Sx, Sy;</code>. By <code class="language-plaintext highlighter-rouge">lower_bound</code> and <code class="language-plaintext highlighter-rouge">upper_bound</code>, we can avoid visiting the point which we have visited before. Be careful for <code class="language-plaintext highlighter-rouge">@</code>.</p> <p>I made an attempt to implement this solution, but I felt that this was a difficult solution since I had to care empty sets.</p> <h3 id="another-solution-2-break-if-we-meet-less-than-distance-grid">Another Solution #2 (Break if we meet less-than-distance grid)</h3> <p>We reduce $O(HWK)$-BFS to $O(HW)$-BFS. First, we implement normal BFS.</p> <div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">Q</span><span class="p">.</span><span class="n">empty</span><span class="p">())</span> <span class="p">{</span> <span class="k">auto</span> <span class="n">p</span><span class="p">{</span><span class="n">Q</span><span class="p">.</span><span class="n">front</span><span class="p">()};</span> <span class="k">auto</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">]</span> <span class="o">=</span> <span class="n">p</span><span class="p">;</span> <span class="n">Q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span> <span class="k">for</span> <span class="p">(</span><span class="k">auto</span> <span class="n">k</span><span class="p">{</span><span class="mi">0</span><span class="p">};</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">;</span> <span class="o">++</span><span class="n">k</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="k">auto</span> <span class="n">i</span><span class="p">{</span><span class="mi">1</span><span class="p">};</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">K</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="k">auto</span> <span class="n">nx</span><span class="p">{</span><span class="n">x</span> <span class="o">+</span> <span class="n">dx</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">*</span> <span class="n">i</span><span class="p">};</span> <span class="k">auto</span> <span class="n">ny</span><span class="p">{</span><span class="n">y</span> <span class="o">+</span> <span class="n">dy</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">*</span> <span class="n">i</span><span class="p">};</span> <span class="c1">// we want to discuss how we can make transition here.</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>Transition is summarized as follows.</p> <ul> <li>Of course, if we meet an <code class="language-plaintext highlighter-rouge">@</code> grid or out-of-range, we put <code class="language-plaintext highlighter-rouge">break;</code>.</li> <li>If we meet an empty grid, we write the answer in $V$ and go further.</li> <li>If we meet <strong>a visited grid whose value is equal to $V[x][y]$</strong>, we <code class="language-plaintext highlighter-rouge">continue;</code> to go further since we may reach an empty grid beyond that.</li> <li>If we meet <strong>a visited grid whose value is less than $V[x][y]$</strong>, we put <code class="language-plaintext highlighter-rouge">break;</code> since it suffice to try BFS by another chance from that point. Thus, even if we stop there, the final result will be the same.</li> </ul> <div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">valid</span><span class="p">(</span><span class="n">nx</span><span class="p">,</span> <span class="n">ny</span><span class="p">))</span> <span class="p">{</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="nf">if</span> <span class="p">(</span><span class="n">V</span><span class="p">[</span><span class="n">nx</span><span class="p">][</span><span class="n">ny</span><span class="p">]</span> <span class="o">==</span> <span class="n">Infty</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">())</span> <span class="p">{</span> <span class="n">V</span><span class="p">[</span><span class="n">nx</span><span class="p">][</span><span class="n">ny</span><span class="p">]</span> <span class="o">=</span> <span class="n">V</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="n">Q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">Point</span><span class="p">{</span><span class="n">nx</span><span class="p">,</span> <span class="n">ny</span><span class="p">});</span> <span class="p">}</span> <span class="k">else</span> <span class="nf">if</span> <span class="p">(</span><span class="n">V</span><span class="p">[</span><span class="n">nx</span><span class="p">][</span><span class="n">ny</span><span class="p">]</span> <span class="o">==</span> <span class="n">V</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> <span class="k">continue</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> </code></pre></div></div> <p>We can estimate the maximal visit for each grid. It will be less than or equal to $4$ since the kinds of directions is less than $4$. If we think more carefully, the upper bound $4$ becomes $2$.</p> <h4 id="caution">Caution</h4> <p>This solution works because we use BFS. By BFS, we determine the distance of each grid before we visit it. This is a key to make a break. For example, if we apply Dijkstra method for this problem, we cannot use this trick.</p> <h1 id="others">Others</h1> <ul> <li>Review: 2020-06-20</li> </ul> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>A - sample: 2, tle: 2.000, time: 04:46, from_submit: 48:53 B - sample: 3, tle: 2.000, time: 01:26, from_submit: 47:27 C - sample: 3, tle: 2.000, time: 03:48, from_submit: 43:39 D - sample: 3, tle: 2.000, time: 04:23, from_submit: 39:16 E - sample: 2, tle: 3.500, time: 16:48, from_submit: 22:29 F - sample: 3, tle: 3.000, time: 22:29, from_submit: 00:00 </code></pre></div></div>Kazune TakahashiAtCoder Beginner Contest 170Tokio Marine &amp; Nichido Fire Insurance Programming Contest 20202020-06-13T00:00:00+09:002020-06-13T00:00:00+09:00http://kazune-lab.net/contest/2020/06/13/tokiomarine2020<p><a href="https://atcoder.jp/contests/tokiomarine2020">東京海上日動 プログラミングコンテスト2020</a></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/tokiomarine2020/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0613_tokiomarine2020">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---nickname"><a href="https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_a">A - Nickname</a></h2> <p>Flush <code class="language-plaintext highlighter-rouge">S.substr(0, 3)</code>.</p> <h2 id="b---tag"><a href="https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_b">B - Tag</a></h2> <p>If $V \leq W$, the answer is <code class="language-plaintext highlighter-rouge">NO</code>.</p> <p>Suppose that $V &gt; W$. Let $D = \lvert A - B \rvert$ and $U = \lvert V - W \rvert$. If $UT \geq D$, the answer is <code class="language-plaintext highlighter-rouge">YES</code>; otherwise, <code class="language-plaintext highlighter-rouge">NO</code>.</p> <h2 id="c---lamps"><a href="https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_c">C - Lamps</a></h2> <p>Honest implementation takes $O(N)$-time for each manipulation by cumulated sum.</p> <p>We see that we can stop when $A _ i = N$ holds for all $i$. The worst case appears $N = 2 \times 10 ^ 5$ and $A _ i = 0$ in the initial state. But it takes just $41$ times.</p> <h2 id="d---knapsack-queries-on-a-tree"><a href="https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_d">D - Knapsack Queries on a tree</a></h2> <p>Assume $1$-indexed for vertexes.</p> <h2 id="solution">Solution</h2> <p>We construct the following DP table for each vertex $v \leq 2 ^ C$ where $C = 9$ since $2 ^ C \approx \sqrt{N}$.</p> <blockquote> <p>$dp[v][i] =$ the largest value where the total weight of the items is less than or equal to $i$.</p> </blockquote> <p>We construct this table in $O(\sqrt{N} L)$-time, where $L \approx 10 ^ 5$.</p> <p>For each query $(v, l)$, we divide the items we are considering into two blocks. Let $w$ be the largest ancestor of $v$ which satisfies $w \leq 2 ^ C$. In fact, $w$ may or may not be $v$ itself. Let $S = \{ (v _ k, l _ k) \}$ be the set of the items we have not considered to create $dp[v]$. We apply brute-force method for $S$ to try all the possibilities of the subset of $S$ and ask the remaining weight to $dp[v]$ to obtain the maximum value.</p> <p>Each query takes $O(\sqrt{N})$-time. Therefore, the total time complexities is $O(\sqrt{N} L + \sqrt{N} Q)$.</p> <h2 id="e---orand"><a href="https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_e">E - O(rand)</a></h2> <h3 id="observation">Observation</h3> <h4 id="reduction">Reduction</h4> <p>At first, we work on each bit. Note that $S$ is bitwise-and and $T$ is bitwise-or.</p> <ul> <li>If $S = 0$ and $T = 1$, the answer is $0$.</li> <li>If $S = T = 1$, we delete such $A _ i$ that its bit is $0$.</li> <li>If $S = T = 0$, we delete such $A _ i$ that its bit is $1$.</li> </ul> <p>Therefore, we can reduce the problem so that $S = 1$ and $T = 0$ and the bit runs on $[0, M)$. We rescale $A$ and $N$.</p> <h4 id="inclusive-exclusive-principle">Inclusive-exclusive principle</h4> <p>The problem becomes as follows.</p> <blockquote> <p><strong>Problem E.1</strong>: Find the number of ways to choose a subset of $\{ A _ i \}$ whose size is less than or equal to $K$ so that, for each bit in $[0, M)$, there is at least one $1$ and one $0$.</p> </blockquote> <p>Obviously, we use the inclusive-exclusive principle to write the answer as follows. $A = \sum _ {mask \in 2 ^ M} (-1) ^ {popcount(mask)} f(mask),$ where</p> <blockquote> <p>$f(mask) =$ the number of choices where, for each bit $i$ where $mask _ i = 1$, the condition is not satisfied. For the bit $mask _ i = 0$, arbitrary.</p> </blockquote> <h3 id="solution-1">Solution</h3> <p>The condition that “the condition is not satisfied” is equivalent to that the $A _ i$’s is equal in the viewpoint of masked by $mask$. Therefore, for each $mask$, we define <code class="language-plaintext highlighter-rouge">map&lt;int, ll&gt; count;</code> and we execute $count[A[i] \&amp; mask] \mathbin{ {+} {+} }$. For each $[mask, cnt] \in count$, we execute $res \mathbin{ {+} {=} } \begin{pmatrix} cnt \\ 1 \end{pmatrix} + \dots + \begin{pmatrix} cnt \\ K \end{pmatrix}.$ And don’t forget to add $1$ to $res$ as the empty set.</p> <h2 id="f---triangles"><a href="https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_f">F - Triangles</a></h2> <h3 id="initial-setting">Initial setting</h3> <p>We solve two problems $solve(H, W, K)$ and $solve(W, H, K)$. In addition, by symmetry of left and right, we fix that one vertex is on the left edge, one vertex is on the bottom edge and the other vertex is on the top edge. We set $x$, $y$ and $s$ as described in the following picture.</p> <p><img src="/images/2020-06-13-F.png" alt="Picture F.1" /></p> <p>Here, we assume $s \geq 0$ as long as we double the result when $s &gt; 0$ by the symmetry of bottom and top. The variables runs in $0 &lt; x &lt; W - 1$, $0 \leq s &lt; W - 1$, $0 \leq s &lt; W - x$ and $0 &lt; y &lt; H$.</p> <h3 id="observation-1">Observation</h3> <p>Let $i$ be the number of the interior grid points. Let $b$ the number of the grid points on the edges including the vertexes. Let $S$ be the area of the triangle. By Pick’s theorem, we have the following relation. $2S = 2i + b - 2.$ By direct calculation, we have the following equation. \begin{align} 2S &amp;= H(2x + s) - (H - y)x - y(x + s) \\<br /> &amp;= Hx + s(H - y). \end{align} It is well-known that we can count the grid points on the edges by $\gcd$ as follows. $b = \gcd(H - y, s) + \gcd(y, x + s) + \gcd(s, H).$ Hence, the condition $i \leq K$ is equivalent to the following. $2K \geq Hx + s(H - y) - \gcd(H - y, x) - \gcd(y, x + s) - \gcd(s, H) + 2 \tag{F.1}$</p> <h3 id="solution-2">Solution</h3> <p>We fix $(s, y)$. The equation (F.1) is equivalent to the following. $Hx - \gcd(H - y, x) - \gcd(y, x + s) \leq 2K - s(H - y) + \gcd(s, H) - 2. \tag{F.2}$ Let $R(s, y) = 2K - s(H - y) + \gcd(s, H) - 2$ and $L(x, s, y) = Hx - \gcd(H - y, x) - \gcd(y, x + s)$.</p> <p>Here, we see that $\gcd(H - y, x) + \gcd(y, x + s) \leq H$ since the left hand side is the sum of the number of the grid point on the two edge, which cannot exceed $H$. Hence,, (F.2) surely holds when $x \leq R(s, y) / H$ and does not when $x &gt; R(s, y) / H + 1$. Thus we check (F.2) for just one possibility $x = R(s, y) / H + 1$ (or zero possibility if it is out-of-range) to decide the result is $\min(E / H, W - 1)$ or its plus one.</p> <p>Then, we run $(s, y)$. Let $R _ {prep}(s, y) = 2K - s(H - y) + H - 2$. Then, it holds that</p> <ul> <li>$R(s, y) \leq R _ {prep}(s, y)$,</li> <li>$R _ {prep}$ is increasing for $y$ and decreasing for $s$.</li> </ul> <p>We consider only for $(s, y)$ where $R _ {prep} (s, y) \geq 0$. Therefore, we run $y = 1, \dots, H - 1$ and $s = W - 2, \dots, 0$ as long as $R _ {prep} (s, y) \geq 0$.</p> <h1 id="others">Others</h1>Kazune Takahashi東京海上日動 プログラミングコンテスト2020AtCoder Grand Contest 0452020-06-07T00:00:00+09:002020-06-07T00:00:00+09:00http://kazune-lab.net/contest/2020/06/07/agc045<p><a href="https://atcoder.jp/contests/agc045">AtCoder Grand Contest 045</a></p> <p><em>The explanation will be written after the series of AGC/ARC on May and July 2020.</em></p> <h1 id="source-codes">Source codes</h1> <ul> <li><a href="https://atcoder.jp/contests/agc045/submissions?f.User=kazunetakahashi">My submissions (AtCoder)</a></li> <li><a href="https://github.com/kazunetakahashi/atcoder/tree/master/2020/0607_AGC045">My source codes (GitHub)</a></li> </ul> <h1 id="solutions">Solutions</h1> <h2 id="a---xor-battle"><a href="https://atcoder.jp/contests/agc045/tasks/agc045_a">A - Xor Battle</a></h2> <h2 id="b---01-unbalanced"><a href="https://atcoder.jp/contests/agc045/tasks/agc045_b">B - 01 Unbalanced</a></h2> <h2 id="c---range-set"><a href="https://atcoder.jp/contests/agc045/tasks/agc045_c">C - Range Set</a></h2> <h2 id="d---lamps-and-buttons"><a href="https://atcoder.jp/contests/agc045/tasks/agc045_d">D - Lamps and Buttons</a></h2> <h2 id="e---fragile-balls"><a href="https://atcoder.jp/contests/agc045/tasks/agc045_e">E - Fragile Balls</a></h2> <h2 id="f---division-into-multiples"><a href="https://atcoder.jp/contests/agc045/tasks/agc045_f">F - Division into Multiples</a></h2> <h1 id="others">Others</h1>Kazune TakahashiAtCoder Grand Contest 045