### pompon's blog

By pompon, 5 years ago, , Additional explanation of the solution, requested by ehsanoo. On the contrary to the tutorial, we will apply the queries online (however result itself will be calculated offline anyway; after you understand this part, you can revert the construction to obtain the solution described in the tutorial). We will keep our string in a compressed form of a DAG. • The DAG on the left represents the string from the first sample test: 123123. Note that the root consists of 6 cells. Each represents a digit of 123123, however their values are not stored there. Each cell points to the leaf corresponding to its content. Note that cells corresponding to the same digit point to the same leaf.
• Now the first query comes: 2->00, so our string should become 10031003. What we do? We grab our leaf '2' and replace it with a new node (with 2 cells pointing to 0). Everything what was pointing to '2' now points to our new node (i.e. we replaced all occurrences of '2').
• Note that we create a new leaf '2' (marked as red). In general case the replacing string can contain the digit which is being replaced and we don't want the node to point to itself (i.e. we are not doing a recursive replacement).

To sum up: every node of the DAG represents some substring of the current string. At any point of processing the queries, we keep track of nodes which correspond to the single digits, so that we can replace them with a string from a query. For the replaced digit we create a new node and from now on, any new occurrences of this digit should point to this new node. At any point we can retrieve the current string by traversing the DAG. After processing all the queries we want to retrieve the result. To do that, we can dynamically calculate 2 values for each node:

• the number it represents mod (109 + 7)
• mod (109 + 7)

It is easy to see how to calculate them for the parent, if we already calculated them for the children. By pompon, 5 years ago, , Formal proof on how many weapon levels can be neglected.

Let us take k = 1 and denote the stopping time of obtaining a weapon of cost i as . Then independent

We can bound from below the geometric distriubtion by uniform one, in a sense that independent where is exactly an event in which we eventually obtain a weapon of level x. Now let us present the probability space of Y1, .., Yx as Ω = [0, 1] × [0, 2] × .. × [0, x] which is a hypercuboid with volume x!, therefore probability measure P has density in lebesgue measure. It is easy to see that is contained in a hypercone of lebesgue volume . By taking into consideration the density g, we get an upper bound Now back to the original task. Let us denote the total profit from selling the weapons as T ≤ n2. We want the following expectation to be small: For n = 105 and ε = 10 - 9, using the formula above we can check that x = 900 is enough.
{"ft_lang_label":"__label__en","ft_lang_prob":0.91697705,"math_prob":0.9731817,"size":1918,"snap":"2019-51-2020-05","text_gpt3_token_len":430,"char_repetition_ratio":0.122257054,"word_repetition_ratio":0.0057142857,"special_character_ratio":0.24139729,"punctuation_ratio":0.10025707,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9918796,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"im_url_duplicate_count":[null,3,null,null,null,2,null,3,null,3,null,3,null,null,null,3,null,3,null,3,null,3,null,3,null,3,null,3,null,3,null,3,null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-24T03:27:01Z\",\"WARC-Record-ID\":\"<urn:uuid:1663d85c-f8d4-4db0-afae-ab0178dca703>\",\"Content-Length\":\"88090\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e18519e-61d5-4f00-9976-df57425d4e3c>\",\"WARC-Concurrent-To\":\"<urn:uuid:adc450dd-ea90-4c9f-94bb-0c36c5585873>\",\"WARC-IP-Address\":\"81.27.240.126\",\"WARC-Target-URI\":\"https://codeforces.com/blog/pompon\",\"WARC-Payload-Digest\":\"sha1:RQISAYMBFKLOYY4GWVBMEW3ZE5OY6UEP\",\"WARC-Block-Digest\":\"sha1:TNRV4ODKCKF6YKNR75BQRTQGV6K4PJ3U\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250614880.58_warc_CC-MAIN-20200124011048-20200124040048-00039.warc.gz\"}"}