tag:blogger.com,1999:blog-38377191252988247992017-03-29T04:33:57.298+09:00rng_58's blogMakoto Soejimanoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-3837719125298824799.post-12643548182583660332017-02-13T01:24:00.000+09:002017-02-13T01:24:17.723+09:00Best Performance and Worst Performance<div dir="ltr" style="text-align: left;" trbidi="on"><a href="http://3.bp.blogspot.com/-dj46NxSjTDQ/WKCLxwbdMII/AAAAAAAAAKo/DZPJVF8PmwwvgNmBGol13RKT0svHdQ8qACK4B/s1600/kabu_cry.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="276" src="https://3.bp.blogspot.com/-dj46NxSjTDQ/WKCLxwbdMII/AAAAAAAAAKo/DZPJVF8PmwwvgNmBGol13RKT0svHdQ8qACK4B/s320/kabu_cry.png" width="320" /></a>After the last blog, I participated in three contests: GP of Gomel (tourist's contest), SRM 708, and GP of China. The problems were very nice in all contests and I really enjoyed them. However, my performance was quite different: in SRM everything went very well and it was my best performance after the last TCO, but today I performed terribly (well, 17th in Open Cup is not bad, but given that the problems really suited me, it was terrible).<br /><br />In SRM I was in a very strange situation. A few minutes before the end of the coding phase, I found that my solution may access to out-of-memory and fail. However, it worked on random tests and I decided not to resubmit - and luckily, it passed the systest!<br /><br />In GP of Gomel, I solved seven tasks (B, D, F, G, H, J, K) quickly and I still had two hours to solve something else. In such situations, I usually read all problems and sort them by expected difficulties for me. The difficulty is estimated by the number of ACs (objective difficulty) and the topic (for example, I'm good at combinatorial/mathematical tasks, but bad at data structures). For me the topic is more important and after spending several minutes on each task I decided to concentrate on A and I because they looked the most interesting for me and no task was solved by many people - however I couldn't finish coding I in time and ended up with nothing.<br /><br />In GP of China, after solving five tasks (B, E, F, G, J), I had more than 3 hours. Among the remaining tasks, I liked C/D the most and decided to spend 3 hours on them, but again ended up with nothing.<br /><br />It seems I can quickly solve easy problems, but when the problems become harder I can't do anything. I seriously need to fix this tendency. Maybe I should try Div2 in next Open Cup? Anyway, I will upsolve at least four tasks (Gomel's AI, China's CD) in the near future and post here again. I believe I understood the solutions for these tasks now and they are indeed very nice.</div>Makoto Soejimahttps://plus.google.com/103129477659853988041noreply@blogger.com0tag:blogger.com,1999:blog-3837719125298824799.post-85903144291928987832017-02-03T19:50:00.000+09:002017-02-03T23:57:59.726+09:00Hashing and Probability of Collision<div dir="ltr" style="text-align: left;" trbidi="on"><a href="http://2.bp.blogspot.com/-PgS5_xzPEXI/WJRe0sWViHI/AAAAAAAAAJo/jLt2BqZHK5w2uOiDlt_r74x9J8OlO955ACK4B/s1600/jiko_denchu_butsukaru.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" src="https://2.bp.blogspot.com/-PgS5_xzPEXI/WJRe0sWViHI/AAAAAAAAAJo/jLt2BqZHK5w2uOiDlt_r74x9J8OlO955ACK4B/s320/jiko_denchu_butsukaru.png" width="320" /></a><a href="http://codeforces.com/contest/763/problem/D">Problem D</a> of Codeforces #395 required hashes of rooted trees. In this post I give some examples of good hashes and bad hashes, and then construct a good hash for rooted trees.<br /><br />The most well-known hash in competitive programming is probably the rolling-hash of strings. The following problem looks stupid, but let's solve it using rolling hash anyway.<br /><br /><b>Problem 1</b>. You are given two strings s, t of lengths N (<= 10^5) that consist of lowercase letters. Determine if they are the same.<br /><br />Let MOD=10^9+7. Choose an integer r uniformly at random from the interval [0, MOD). Compute the sum of r^i * s[i] over all i. This is called the rolling hash of s. We can consider it as a polynomial of r, let's denote it as S(r). Similarly, define T(r). The hash collision happens when s and t are different strings (thus S and T are different polynomials), but unluckily S(r) = T(r). Here we use <a href="https://en.wikipedia.org/wiki/Schwartz%E2%80%93Zippel_lemma">Schwartz-Zippel lemma</a>:<br /><br />Let P(x_1, ..., x_k) be a non-zero (multi-variable) polynomial on a finite field F_MOD. If the variables are chosen independently and uniformly at random, the probability that P(x_1, ..., x_k) = 0 is at most D/MOD, where D is the total degree of the polynomial.<br /><br />In the case of rolling hash, we are only interested in single-variable polynomials. From the lemma above, we can prove that the collision probability is at most N/MOD (<= 1/10^4). Thus I'd say this is a "good hash". My intuition tells that in practice the probability is as small as 1/MOD, but that looks at least very hard to prove. Let me give an interesting example that shows the intuition is not always correct. In the lemma F_MOD must be a field, and MOD must be a prime. What happens if the modulo is not prime, for example 2^64 (this would simplify the implementation). It turns out that the rolling hash doesn't work well for a certain case if the modulo is 2^64. <a href="http://codeforces.com/blog/entry/4898">Detailed description on Codeforces.</a><br /><br /><b>Problem 2</b>. You are given two graphs G, H with N (<= 10^3) vertices and M (<= 10^4) edges. Determine if they are isomorphic.<br /><br />For each vertex v in the graph and an integer d, define the level-d hash for v, h(d, v). When d = 0, define h(0, v) = 1. Otherwise, let x_1, ..., x_k be the sorted list of level-(d-1) hashes of vertices adjacent to v, and define h(d, v) as the sum of x_i * r^i modulo MOD. Compute the level-N hashes of the two graphs for each vertex, sort them, and compare them.<br /><br /><strike>It looks very hard to find a testcase against this hash.</strike> (Actually no - see Petr's comment below). However there's no theoretical guarantee under this hash and I think most people agree that this doesn't look valid as an intended solution of a problem in a contest (graph isomorphism is a well-known difficult problem). I'd say this is a "bad hash".<br /><br />OK, now we want to construct a good hash for rooted trees. Before that, let's try an easier version. Hashes for (multi)sets.<br /><br /><b>Problem 3</b>. You are given two (multi)sets A = {a_1, ...., a_n} and B = {b_1, ..., b_n}. n <= 10^5 and each element is in the interval [0, MOD). Determine if they are the same.<br /><br />One possible solution is to sort the input and then compute the rolling hash of it. Can you find a linear-time hash? The solution is indeed very simple. Let's take a random integer r from the interval [0, MOD), and compute the hash (r+a_1)(r+a_2)...(r+a_n). This is a polynomial of r of degree n, so again from Schwartz-Zippel lemma, the collision probability is at most n/MOD.<br /><br /><b>Problem 4</b>. You are given two rooted trees with n vertices. Determine if they are isomorphic.<br /><br />Like previous examples, we assign a polynomial for a rooted tree, and evaluate the polynomial for random variables. However, unlike previous examples, this time we use multi-variable polynomials. We define the polynomial P(G) for a rooted tree G as follows. If G is a single leaf, P(G) = 1. Otherwise, let d be the depth of the tree, k be the number of children of the root of G, and G_1, G_2, ..., G_d be the subtrees corresponding to those children. Define P(G) = (x_d + P(G_1))(x_d + P(G_2))...(x_d + P(G_k)). See the picture for an example. (x_1, x_2, x_3 are replaced with x, y, z).<br /><br /><a href="http://1.bp.blogspot.com/-ympA_ncj9h0/WJRgC61jnFI/AAAAAAAAAKE/HQy2eiffxr4M-7ds68h_SkcmSIPmGgLJACK4B/s1600/tree_hash.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" src="https://1.bp.blogspot.com/-ympA_ncj9h0/WJRgC61jnFI/AAAAAAAAAKE/HQy2eiffxr4M-7ds68h_SkcmSIPmGgLJACK4B/s320/tree_hash.png" width="248" /></a>This way we get a multi-variable polynomial over d variables of degree l, where l is the number of leaves in the tree. We can prove that two polynomials that correspond to non-isomorphic trees are different (by using the uniqueness of factorization of the ring of polynomials). Thus, if we evaluate this polynomial for random variables, we get a good hash with collision probability at most l/MOD!<br /><br /><br /></div>Makoto Soejimahttps://plus.google.com/103129477659853988041noreply@blogger.com6tag:blogger.com,1999:blog-3837719125298824799.post-31314826824834426512017-02-01T04:12:00.001+09:002017-02-01T04:12:16.854+09:00January Contests<div dir="ltr" style="text-align: left;" trbidi="on"><a href="http://2.bp.blogspot.com/-9AuHo7lQM_k/VmFjQUHgvWI/AAAAAAAA1Ss/f4pkGtm9X2Y/s1600/book_character_smile.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img alt="ピカピカの本のキャラクター" border="0" height="243" src="https://2.bp.blogspot.com/-9AuHo7lQM_k/VmFjQUHgvWI/AAAAAAAA1Ss/f4pkGtm9X2Y/s320/book_character_smile.png" width="320" /></a>No, I didn't forget this blog. I was just too lazy to write new posts. Anyway, after the last post I was not very active - I participated only in one SRM and Facebook Hacker Cup's rounds (and SnarkNews Series, though I gave up after having trouble with understanding the Russian statements).<br /><br />In SRM 706, the first two tasks were relatively easy. My idea for the Hard was as follows. In this task you want to find a family of subsets of {1, ..., N}, where no subset contains two consecutive numbers and for each non-consecutive pair (p, q), at least one subset contains both. First add a subset that contains all even numbers, and add another subset that contains all odd numbers. Now we only need to care about pairs with different parities. In my construction, for example when N = 15, add three sets: {2, 4, 6, 9, 11, 13, 15}, {1, 3, 5, 7, 10, 12, 14}, and {1, 3, 5, 8, 11, 13, 15}. Then do the same thing recursively for {1, ..., 7} and {9, ..., 15} (like divide-and-conquer). This way we only need about 3 * lg N sets but it turned out that the numbers can be as large as 10^25. Maybe we can do it with 2 * lg N sets? I still don't know the answer, let's add it to "Upsolving" section.<br /><br />My rating slightly increased after the contest and it became 3615. It seems this is the 4th in TopCoder's history. However it's far away from the three legends - Petr (3924), ACRush (3902), and tourist (3836).<br /><br />Do you understand what the picture above means? Facebook. There were four elimination rounds in this year's FHC. The first three rounds were relatively easy. The second task in Round 1 was notable - at first it looked like a very tedious task where you have to find all subsets of points that can be enclosed by a circle, but there's a significantly simpler solution: you only need to consider the union of two squares.<br /><br />Last Sunday I participated in Round 3, the last elimination round. I knew problem A so I solved it quickly. After that I read everything and decided to solve B first - it's tricky and requires careful coding, but I'm good at counting tasks and it looked like the easiest one. After that the choice was between C and E. D looked overwhelming at that time. I chose C mainly because it looked very easy to do stress-testing and believed that correct A+B+C can qualify (which turned out to be wrong and I was quite close to the cutoff due to too big penalty). Maybe careful A+B+E was a better strategy. (However, I have to admit that I made a small mistake and my E failed during upsolving).<br /><br />My solution for C was as follows. First for each non-center vertex i, compute two values x[i] and y[i]. x[i] is the position along the cycle and y[i] is the shortest path from the center. Now the problem asks to compute the sum of min{x[j] - x[i], X - (x[j] - x[i]), y[i] + y[j]}, where X is the total cycle length. In my solution I assumed that the arrays x and y are arbitrary. This leads to a solution with BIT. But x and y are not arbitrary arrays and these satisfy triangle inequalities of a certain graph. If we use this fact, we can prove that for a fixed i we can split the cycle into three consecutive parts and in each part we can determine which of x[j] - x[i], X - (x[j] - x[i]), y[i] + y[j] is the smallest. This leads to a simpler linear solution.<br /><br />During the contest, my first idea for D was as follows. Consider two numbers x and y, and two masks (the set of non-broken positions) mask1 and mask2. We can't distinguish two states (x, mask1) and (y, mask2) after t seconds if ((x + t) & mask1) and ((y + t) & mask2) are the same. For each bit there are several cases: it is broken in both states, broken in one state and off in another, ... and so on. And looked completely unapproachable. Indeed I missed a very simple thing: when the position i is 0 in the input, we can always assume that the display is broken at this position. This observation greatly simplifies the problem and now we know mask1 and mask2 - now we can concentrate on x and y. After that it's still tricky, but quite solvable task.<br /><br />How should we solve it during the contest? One possible approach is to consider various cases, but I don't prefer it with such weak examples. Digit DP is also possible but it's fairly complicated. Here's a very simple solution: we can prove that in the optimal solution x and y differ in only two bits. Fix two positions p and q - at position p, x is 0 and y is 1, and at position q, vice versa. And in other positions x and y are the same. In this case, we can prove that there's some r such that the bits at positions [r, N) doesn't matter at all, and we want to minimize the numbers in the least-significant r bits. If we fix these three numbers (p, q, r), we can solve the task without considering various cases. This will lead to a very simple O(N^4) solution. <a href="http://ideone.com/Rrjggy">Source code in ideone.</a><br /><br />Anyway, I managed to qualify to the Finals. If you also qualified, see you in Seattle!<br /><br /><br /><br /></div>Makoto Soejimahttps://plus.google.com/103129477659853988041noreply@blogger.com0tag:blogger.com,1999:blog-3837719125298824799.post-2252552302180255632017-01-01T19:56:00.000+09:002017-01-01T19:56:08.462+09:00SnarkNews New Year Blitz Contest<div dir="ltr" style="text-align: left;" trbidi="on">My first contest in this year was SnarkNews New Year Blitz Contest (<a href="https://contest.yandex.com/newyear2017/contest/3642/problems/">problems</a>, <a href="http://snarknews.info/mlr2017.html">results</a>). <br /><br />Due to the difference of time zones, it was somewhat a strange contest for me. I successfully celebrated 6:00 AM of New Year's Day.<br /><br />Problem G was easy but nice. You are given a decimal number up to 250 digits. Consider all integers that can be obtained by permutating the digits, and compute the gcd of those integers.<br /><br /></div>Makoto Soejimahttps://plus.google.com/103129477659853988041noreply@blogger.com0tag:blogger.com,1999:blog-3837719125298824799.post-26196711436990285162017-01-01T05:00:00.003+09:002017-01-01T21:21:19.558+09:00Welcome to My Blog!<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;">Happy New Year!</span></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;">New Year's Day is a good day to start something. This year I decided to start writing a blog.</span></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;">The main purpose of this blog is to keep track of my activities related to competitive programming (mainly for personal training). However, after contests, I may share my impressions or some problems, and those posts may be interesting to read.</span></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span><span style="font-family: "arial" , "helvetica" , sans-serif;">There are <span style="font-family: "arial" , "helvetica" , sans-serif;">three</span> <span style="font-family: "arial" , "helvetica" , sans-serif;">subpage</span>s in this blog:</span></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span><a href="http://rng-58.blogspot.jp/p/list-of-contests.html"><span style="font-family: "arial" , "helvetica" , sans-serif;">List of Contests</span></a></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Here I will list all contests I've participated. I don't think I can recollect everything, but I will do my best.</span></span></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br /></span></span><span style="font-family: "arial" , "helvetica" , sans-serif;"><a href="http://rng-58.blogspot.jp/p/my-problems.html">My Problems</a><br /><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Here I will list all problems I authored.</span></span></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><a href="http://rng-58.blogspot.jp/p/upsolving.html"><span style="font-family: "arial" , "helvetica" , sans-serif;">Upsolving</span></a></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Here I put everything I'm planning to upsolve in the near future.</span></span><br /><br /><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">You can <span style="font-family: "arial" , "helvetica" , sans-serif;">also go to these pages from links at the top-right corner.</span> </span></span></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><span style="background-color: white; color: #222222; display: inline; float: none; font-size: 13.2px; font-style: normal; font-weight: normal; letter-spacing: normal; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><span style="font-family: "arial" , "helvetica" , sans-serif;">Also, at the top-right corner, you can find the superset of contests</span> I'm planning to participate (I may skip some of them). All times are in JST (GMT+9). You can also use it as a reminder :) </span> </span></span></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span><span style="font-family: "arial" , "helvetica" , sans-serif;">Occasionally, I may post something unrelated to competitive programming, but I'm not sure now.</span></div><div style="text-align: left;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div></div>Makoto Soejimahttps://plus.google.com/103129477659853988041noreply@blogger.com0