Hi, all!

This year GP of China is scheduled on Sunday, March 10, 2019 at 11:00 (UTC+3). Writers have spent several days and nights creating and developing these problems. Hope you enjoy the contest!

**UPD:** Thanks to zimpha, Claris, quailty and me, here is editorial.

Links: OpenCup Main Page, GP of China (Div. 1), GP of China (Div. 2), and New Team Registration?

"The virtual contest is in progress. You are not allowed to participate" again...

It seems Div.1 is postponed for 2 minutes, and Div.2 will be postponed for 30 minutes.

UPD:Said on the main page, Div.2 will be postponed for 1 hour.You shouldn't replace problems during the contest.

I finished coding some problem, tested samples, found that samples don't look correct, then reloaded the problem and found that the problem was changed to a completely different problem...

Maybe the old problem statement was misplaced by admin? I'm not sure about that. I have no access to the backend so far. Anyway, sorry for the inconvenience.

OK, sorry for complaining if it's not your mistake. I guess it's just an unfortunate error.

I'm just a contestant, can I register a sector?

I'm afraid I can't answer the question. If you really want to register, you can send messages to

snarknewsfor more details.What is test 44 in B?

k=1,2

Try this test:

1

6 6 1

2 4

1 6

6 4

2 5

3 6

5 4

Answer: Yes

Contrary to its outlook, J was actually very cool. Thanks!

C is pretty easy if we are given some oracle which draws a line slightly right to some two points, but it seems that's what actual problem is. Is there any easy way for that?

And how to solve H?

How to solve C if we can draw such line?

Assume

nis even. Then we dividenpoints into half by sorting in pair (x,y) and draw some line almost parallel tox-axis. Now there isn/ 2 points in each side. You pick convex hull of all points. Then there exists some edge in hull, which one point is in upper side, and other is in lower. Draw a line to eliminate it.Of course this is not verified, so please tell me if this is wrong.

Here's an "oracle" that passes the tests:

It returns up to four segments, some of which pass slightly to the right, and others slightly to the left, so you try them all and take the one that eliminates the two points.

That's very clean, thanks!

How to solve J?

We first see how to enumerate all repeats in a string. Fix the length of repeat 2

k. Divide the string into [0,k- 1], [k, 2k- 1].... We partition suchN/kstring into a maximal interval of string which all strings are same. Thus, we are left with some interval [ik,jk+k- 1]. If we slightly extend it right (by amount ofLCP(ik,jk)) and extend it left (ReverseLCP(ik,jk)), they are the maximal substring which all 2ksubstrings are repeat. With suffix arrays we can enumerate them in time.Thus, we are left with pieces of information indicating that there is an edge connecting

x+i,y+iwith costwconnecting . Of course, we can decompose them in pieces wherex= 2^{k}for somek.We will now process from

k= 19 ->k= 0. Notice, that for eachk, we only needO(n) such information. because anything that is not contained in spanning forest are useless. So, we can compress it by Kruskal's algorithm, andXinformation ink=t+ 1 can be replaced with equivalent 2Xinformation ink=t. If we propagate down untilk= 0, we get exactly what we want. This gives . While writing it down I found my implementation was actually , but it passes in 1.5s so who cares.A piece can just be represented as two pieces where

x= 2^{k}since redundancy doesn't affect the answer.The intended solution is to sort all

kbyw_{k}and merge pieces found by lengthk.We can maintain disjoint union sets for each

k. When we want to mergex+iandy+i(0 ≤i< 2^{k}), first let's check whetherxandyare already connected indsu_{k}, if so then we are done, just break it. Otherwise we need to connectxandyindsu_{k}and then recursively considerx,yandx+ 2^{k - 1},y+ 2^{k - 1}indsu_{k - 1}. Since there are at mostO(n) times of merges in eachdsu, the total complexity is .Yeah, it should be two pieces. I think that was my original thought, and I even preprocessed ⌊

log_{2}(n)⌋ because of it. But somehow I did in the hard way :pIs there a name for your method for enumerating repeats? I have seen it sevaral times, but cannot find any papers or blogs talking about it.

At that time I didn't know it's name. I just thought it was a well-known method. But I think people call it "Run enumeration" and there are more efficient method than what I've described.

Coincidentally, I'm writing a blog post about this, so you can learn about it soon.

A method related to Lyndon words for calculating runs is well- known in China, but I think this method is easier and more practical(in OI competitions we cannot use library). Maybe it should have a name.

I think the method based on Lyndon words are strictly better in terms of implementation and time complexity. The only downside is that it's hard to see the correctness (so people just have to memorize?)

I think I first saw this kind of problem on BOI 2004 Repeats.

Maybe your implementation of this method is too complex. You can see some implementations here. It's so simple after building a suffix array.

Maybe, but the linked problem is a simple one. Anyway, I wrote a blog post about it, though I think it's well known in China.

H:

Use centroid decomposion on

T_{1}. Suppose the centroid iso, and the distance betweenxandoisw_{x}. What we need to calculate is . Herex,yare vertices in a connected componentSofT_{1}according to centroid decomposion.Let's take an edge with length

leninT_{2}. The contribution to answer is . It can be easily calculated using tree DP inO(n).O(n) is too slow, but we can mark those vertices inSas important vertices. Let's compress those unimportant vertices whose degree is no more than 2, we will get a tree withO(|S|) vertices. Then run a linear tree DP on it is OK.The total complexity is . The first

`log`

is for centroid decomposion, and the second`log`

is for`std::sort`

to build the compressed tree.The solution also exists, we leave it for readers.

That's awesome... Thanks!

What is test 19 on B about?

k=3

We were failing test 19 because of this case:

1

4 4 1

1 2

2 3

3 1

1 4

Answer: No

I'm not really sure if this is test 19 or not (I can't pass it myself) but my solution fails miserably on a test like this and don't know now how to fix it yet)

picinputAnswer: No

How to solve D in

O(n^{2}) ? We managed to pass sample inO(n^{2}*log(n)), but unfortunately our solution is too slow.My solution calculates

expo(a,b) only whena,b≤n. Hence, powers can be precomputed.Of course, we did it, but we had other difficulties. I will share our

O(N^{2}*log(N)) approach. Let's calculate number of ways to make connected component withivertices having cycle withjvertices on it. Let's call such valueg_{i, j}. Let's calculatef_{i}andf2_{i}usingg_{i, j}, total number of edges in all valid connected components with sizeiand total number of ways to build a valid component withivertices, respectively. We calculated it inO(N^{2}). Now let'sdp_{i, j}be the total number of edges in graph havingivertices and not having components with size greater thanj. It's obvious that answer forNisdp_{N, N}. And we don't know how to calculatedpwith time faster thanO(N^{2}*log(N)). The main idea we used to calculatedpis that we add components in non-descending order of their size. But since we can add more than one component of same size we should divide answer bycnt! and we can't just independently add two components of the same size. I mean we can't just calculatedp_{i, j}as linear combinationdp_{i, j - 1}anddp_{i - j, j}. We should try every value ofkand get the value ofdp_{i, j}as linear combination ofdp_{i - k * j, j - 1}.How about only consider

dp_{i},dp'_{i}as the total number of edges in graphs havingivertices and the total number of graphs havingivertices? A typical approach is to enumerate the sizekof the component which contains vertice 1 and regard the rest part asdp'_{i - k}.1) Precalculate

x^{y}for allx,y≤n. Also precalculate and .2) Calculate number of connected graphs with

ivertices and a loop with lengthj— it's for 3 ≤j≤i≤nandi^{i - 2}forj= 0 (with no loops).3) The resting part is quite straightforward. Calculate count of connected graphs with

ivertices and no more than one loop, sum of edges in all loops in such graphs using values calculated in previous step. Let's say we calculatedC_{i}andS_{i}. Then we can calculate such values for all graphs (not only connected): ,It was not hard for me to find a formula in part 2 because I've already seen similar formula. It was in TopCoder, SRM 700 Div1 Medium.

Thanks! from the third part is exactly what we needed.

Great! The formula in part 2 is an application of generalized Cayley's formula, right? And seems like it appears in some Codeforces round.

Sadly I forgot to use that. Instead, I used the derivative of generating functions to get the conclusion. What a funny stupid :P

Yes, as TimonKnigge mentioned in a comment below it's just a generalization of Cayley's formula. By the way, I completely forgot about it too — I just remembered task with similar formula on TopCoder :)

Here is our solution, in quadratic time. We had to precompute exponentials to remove the logarithmic factor. I'm sure there's many ways to compute the answer though.

Let

C_{n}be the number of partitions of {1, ...,n} into cycles. TakeC_{0}= 1, then . Now note every partitioning will havenedges in a cycle.Let

H_{n}count the number of partitions of {1, ...,n} into cycles with trees attached (so each component will have at least one cycle), weighted by the number of edges in cycles (per the problem statement. ThenHere we use a generalization of Cayley's formula. So $k$ is the number of vertices (and hence edges) in a cycle, and the remaining

n-kvertices have to be attached as subtrees.Finally, we may have some components that do not contain cycles. Let

W_{n}count the number of ways to build such a forest withnvertices. ThenW_{0}= 1 and .Finally, to compute the answer

A_{n}we then have to partition thenvertices into components with and without cycles, so .What's the easy way to code B?

To give up.

(Seriously, fuck that task.)

I don't think there's an easy solution, but here's what I did:

m<n- 1, just read 2mintegers and print`No`

.`1`

component of size 3, 4, ...,k+ 2. Those components should be cycles (number of internal edges = number of vertices). The other components should have size 1.Cthe number of components. Compress components (we get a tree) and mark vertices that correspond to the cycles. Note that those vertices are now leaves by the previous condition.k≤ 2 andC≥k+ 2.`Yes`

.Coding wasn't to bad, but figuring out all the cases was.

How to solve I?

We need to count number of equivalence classes of arrays of positive integers of length

nwith summand then subtract bad arrays that have some element at least . Let's deal with bad arrays later. To count number of equivalence classes we use Burnside's lemma: it's equal to , whereI(π) is the number of arrays that don't change after applying π andGis a group of permutations generated by a cyclic shift by onesand reversalr. Each permutation inGis eithers^{k}, i.e. a cyclic shift bykors^{k}r, i.e. reversal followed by a cyclic shift byk, so |G| = 2n.For

s^{k}, number of cycles is equal togcd(k,n) and each cycle has length . On each cycle we need to have equal elements. So if elements of thei-th cycle are equal toa_{i}, we have . Ifddoesn't dividemit's impossible, otherwise we need to count the number of sequencesa_{i}with sum . It's a standard binomial coefficient problem. So far we learned how to do this part inO(n). But everything depends ongcd(k,n) and the number ofk≤nsuch thatgcd(k,n) =gequals to . So we can try allgdividingnand for eachgmultiply the answer by . We can precalculate φ(x) for allx≤ 10^{7}and after that this part of the solution will work in .For

s^{k}r, we notice that we have 0, 1 or 2 cycles of length 1, depending on and and all other cycles have length 2. If all cycles had length 2, we could again use binomial coefficients and everything would be easy. Notice that if we knew that some cycle of length 1 has even value, we could split it into 2 halves and treat it like a cycle of length 2. If it has odd value, we could subtract 1 and also split it into 2 cycles. So for each cycle of length 1 we try both cases. Here we need to be careful and remember which cycles can have zero values and which cycles cannot. This part works inO(1).Now we need to subtract bad arrays. Notice that there can be only one edge with length at least , so we cannot shift anything. But we still can reverse the array. So now we have |

G| = 2 and we need to count the number of fixed points foridandr.idis obvious andris very similar to the previous case: we have 1 or 2 cycles of length 1 and all other cycles have length 2. This is solved in the same way as previous case, except we subtract from one edge. Here's the codeWhat's the point of constraints on modulo in D? Why 1 ≤

p?Probably author wanted to increase amount of pain in this world

probably for the same reason we can have 2e5 tests with n=2e5, m=1 in B

The modulo had nothing to do with primes but it was somehow written as

pHow to solve I?

How to solve F?

We tried some greedy after fixing permutation to kill monsters. After we kill first monster using 1...

i, if we give it more damage than its health, we take some of to give to other 2 players. These cannot be more than 102 each, so we brute over them. Also, these 2 damages cannot be same if they are ≤ 2. Then try to kill second monster usingi+ 1...jand possibly take some extra damage dealt during an intermediate attack to third monster similar to 1st case, but here all of this is transferred to third monster. Then we kill third as fast as possible. Can someone point out a flaw in this argument?We may kill two little monsters first. In this case, the damages given to the boss monster may exceed 102, right?

In fact, we know in this case the first two monsters should be killed in no more than 102 seconds, since their health points don't exceed 100 and we only need to waste at most one second for the last monster.

P.S. Editorial is coming soon :)

Consider the example.

HPs: 1, 1, 1+2+...+1000

Attacks: 1, 1, 1e9

In this case it makes sense to kill the third monster in the first 1000 moves. If we delay his death and kill small monsters earlier, we get more damage from the large one (and fewer from the smaller ones, but that's minute).

So, in this example it is not true that small monsters die early. Am I wrong somewhere?

I would like to discuss the health points of the third monster first, and if

HP_{C}> 5050, I would discuss the order of their death. A mass of discussion, huh?Probably I don't get your point. In the version of the analysis I have (shipped to the Moscow Workshop) there was a claim that the first two monsters always die in first 100 seconds. If I understand it wrong and some weaker statement is indeed claimed, I am happy to wait for the analysis with a proof.

It seems that the analysis was fixed last afternoon, so maybe what you've seen is an old version (and wrong). Please check out the final version that I posted on this blog.

The damage I am talking about( ≤ 102) is given to other monsters before the first monster dies completely. This will definitely be less that 102 if the monster 1 has low health. If monster 1 has high health, then it does not make sense to give more than 102 damage to other monsters while the high health monster dies, because they can die in weaker attacks.

This failed for test 3. Will it be possible to share the test cases?

If the high-health monster has health 1 + 2 + 3 + 4 + ... + 102 and does high damage too (say, 10

^{9}), whereas the other two monsters have low damage (say, 1), then you should still attack the high-health monster first.From round 100 (or rather,

max{H_{a},H_{b}}) you can speed this process up though by binary searching for when monstercis dead, after which you can one-hit the other one or two monsters that remain.Try this one:

1

6 3 6 5 3 5

Answer: 54

F:

DP[attacksdone][hp_{a}][hp_{b}] = minimum amount of damage we'll need to take to finish from that state. Note that we can compute the remaining health of the boss from these three parameters. To transition, try hitting every non-dead monster. The base case ofDP[100] is computed by the first paragraph, another base case is all monsters being dead. Note thathp_{a},hp_{b}might get negative, but never smaller than - 100.Auto comment: topic has been updated by skywalkert (previous revision, new revision, compare).