|
+9
It doesn't work for s = "ab", t = "ba". |
|
+5
Why can I sometimes see a "not solved" sign for a hidden test set in the scoreboard? |
|
+8
Your estimation can be improved. It seems you just count the number of independent sets in a rectangle 3 × n. But we can also use the conditions for truthful processors in the central column of this rectangle. This gives that the number of transitions is O(αn) where α is the largest root of x5 - x4 - 3x3 - 6x2 + 1, α ≈ 2.8. |
|
0
In this case all f(n) are between 0 and k / 2. You need to use fast exponentiation to find |
|
+5
If k = 1 and f(0) = 109, the answer is very large. Do you need to find it modulo a prime? If not, you probably missed some inequality, like 2f(0) ≤ k. |
|
+25
We don't even need pairs because - 1 is a quadratic residue modulo p (if p if NTT-friendly). |
|
+1
If |
|
0
Hmm, my solution with vector get TL, but this happened only because the random number generator is bad. All queries are of the first type. Is it possible to fail solutions with tree in the same way? |
|
+9
For arbitrary matrices it's false. For example, Also you can use Lagrange's theorem for |
|
0
|
|
0
If we want to find the smallest solution modulo prime, in the last line we just choose the smallest element from this vector. In our case we need to do some binary search here. |
|
0
Did you know how to do it if m is prime? In the same manner we can find the smallest x such that |
|
0
Nice example. That's why we should care about small values of x. If x is too small, we can look at x + tφ(m2). In this example φ(m2) = 6, so for t = 1 we find x = 7. |
|
0
m is product of some powers of primes piki. Some of these primes divide a, some don't. m1 is a product of those powers for which pi divide a. m2 is a product of other powers. For example, if a = 60 = 22·3·5, m = 126 = 2·32·7, m1 = 2·32 = 18 (2|a, 3|a), m2 = 7 ( |
|
-8
As far as I understand, you want to solve the equation Let m1 be product of all prime powers pk such that pk|m and p|a. m2 = m / m1. By construction m1 and m2 are coprime. For sufficiently large x (at least If |
|
+6
We need to find max(pi + a, ..., pi + b) for all i where pi are prefix sums. Easy way: use segment tree. Faster way: use queue with a maximum. |
|
+30
Use binary search on answer. We want to know if the answer is more than x. Subtract x from all elements. Now we want to find a segment with length from A to B and a positive sum. Can you do it? |
|
+8
If all steps of your path go down, you don't initialize the variable |
|
0
Yeah, it was a terrible idea to prepare this contest in just two days :( |
|
-8
At first, if the last value is negative, k[n] = 1, not k[n-1]. Of course, some k[i] can change when we add a number to the end. But I don't really understand where is the problem for the linear solution. In this case you can just re-find all k[i]. Blocks are needed to implicitly store k[i]. Navick has already explained about them. |
|
+10
If two characteristics are equal for all initial creatures, they are equal for all creatures. So, the characteristic is completely determined by the sequence of k zeroes or ones. |
|
+5
You are completely right. Thanks! |
|
+7
Auto comment: topic has been translated by khadaev (original revision, translated revision, compare) |
|
On
c___a_ →
Great Problem on Dijkstra's Algorithm: Shortest Path Queries between 2 Constant Nodes in the Absence of an Edge, 9 years ago
+1
No, in precomputation we only build two Dijkstra trees, from s and from t. We need them to find sets A and B. For each query we find A and B, iterate through edges, and find the answer. |
|
On
c___a_ →
Great Problem on Dijkstra's Algorithm: Shortest Path Queries between 2 Constant Nodes in the Absence of an Edge, 9 years ago
+1
No, for each edge xy we just use precomputed values of d(s, x) and d(y, t) in the original graph. |
|
On
c___a_ →
Great Problem on Dijkstra's Algorithm: Shortest Path Queries between 2 Constant Nodes in the Absence of an Edge, 9 years ago
+1
The word "cut" does not mean that you need some mincut algorithm. Let's explain it in other words. For a given edge e we can define two sets of edges: It's easy too see that each vertex lies either in A or in B. Consider the earliest moment when we go from the vertex in It works in O(qm) for queries and |
|
0
No, you need FFT for the multipoint evaluation. |
|
On
starkkk →
How to find Kth smallest number in an array which formed by product of each element of 3 array A,B,C? (Time limit :2s), 9 years ago
+10
Think about binary search for the answer. |
|
0
Hint: try to find all pairs (x, y) such that you can find two non-intersecting subsets with sums x and y. It's very similar to all possible sums of subsets. |
|
0
There is some solution that's probably slow in practice, but for small A it's asymptotically faster than trivial: Let's divide our numbers to For each query we can find a pair i such that P·Pi has a nonzero coefficient. Now let's just iterate over all numbers in i-th group and check does it fit. Overall complexity is |
|
+22
If A is an upper bound for elements in array, we can solve the problem in Though, this solution can't find indices of these elements. |
|
+5
Are you sure you understand the statement correctly? "1 1 1" is balanced, but your sets aren't equal. "1 1 2 2 2 2" isn't balanced, but your sets are equal. |
|
+8
Let's fix top-left corner of the square. We need to find the largest square with this corner. We can use binary search and check whether the square is filled, using prefix sums. EDIT: sorry, misread |
|
+23
Another (quite standard) way to solve the original problem: We need a queue with "get a minimum" operation. We can simulate a queue with two stacks. Stack with minima is easy: we just need to store pairs (value, minimum of values below this). Finding best k minima can be solved in the same way. Stack with k minima is not harder: just store k minima of values below this. It obviously work in O(nk). |
|
0
As far as I understand, solution from this forum also use matrix exponentiation, and matrix size is even more than in my solution, so I don't know how it can work fast without this trick. Also you can read my code, it's much shorter than your. https://pastebin.com/8yeXvatX |
|
+8
I will explain only the second technique. The first thing is just some non-asymptotic optimization, probably you don't need it to get AC. You don't really need to find A14, you need to find A14v for some vector v. So you can represent it in the form A8(A4(A2v)). All multiplications are matrix-by-vector, they are O(n2). |
|
0
Denote by cntn, d the number of ways to get the score n if the last digit is d. Let's analyze the small case: d = 3. We have recurrence relations: cntn, 0 = cntn - 1, 1 + cntn - 4, 2 cntn, 1 = cntn - 1, 0 + cntn - 1, 2 cntn, 2 = cntn - 4, 0 + cntn - 1, 1. Do you know how to convert usual recurrence relation (like Fibonacci numbers) to matrix expo? Here we can do almost the same. Let's define a vector vn = (cntn, 0, cntn - 1, 0, cntn - 2, 0, cntn - 3, 0, cntn, 1, cntn, 2, cntn - 1, 2, cntn - 2, 2, cntn - 3, 2). These relations mean that vn + 1 = Avn for some matrix A, so vn = Anv0. |
|
+3
As far as I understand your solution work like this: you have some matrix A and vector v which depend only on the base and you need to calculate An·v for different n. When you use fast exponentiation, you find A2k for all k up to logarithm, multiply some of them and then multiply it by v. We can do the same, but in different order. Let's precalculate all A2k. Now instead of O(n3) matrix multiplication we can use O(n2) matrix by vector multiplication. We need to multiply Btw why 77? I got 100 and i can optimize it to 50. |
|
On
Tribbiani →
An ordinary problem with an extraordinary(maybe) complexity! Help needed!, 9 years ago
+39
If we can solve this problem in O(n), we can process q offline "upper_bound" queries to the sorted array of size n in O(n + q). Just reverse this array and add queries to the end. Answers for your problem are precisely these upper bounds. I can't rigorously prove that it's impossible but i have some ideas. Let's assume that it's possible and construct sorting algorithm that looks to be linear in average. We need to sort an array a1... an. Let's randomly divide it into two subsequences b1... bk and c1... cn - k. Sort the first sequence. Now let's run hypothetical algorithm for upper bounds: bi is an array, ci are queries. How many elements of cj lies between bi and bi + 1? It seems to be small in average, so we can sort them by any usual algorithm. Overall (average) complexity is f(n) = f(n / 2) + O(n), so f(n) = O(n). |
|
0
For example, if we need to process online "push_back" updates. |
|
0
If we can compress values, persistent segment tree is |
|
+5
No, it's not in his parent's subtree. If optimal vertex for v is not in subtree of v, it's also optimal for parent cause path from v to this vertex goes through it's parent. But we need to find not the best city but best city different from the current, yeah? For example, we can find two best cities. |
|
+23
Q1: We can delete cats/dogs instead of moving them: just move them next to one of the remaining cats/dogs. The only tricky case is when we delete all cats or dogs, we will process it separately. Let's calculate dp[processed prefix][cat or dog][balance] = number of moves. Balance is the difference between number of "free" lions and number of cat-dog borders. Lion is free if it doesn't stay between a cat and a dog. All transitions are straightforward, answer is minimal value of dp for nonnegative balance. But i don't know any easy way to work with cases when we delete all cats or dogs, my implementation is really ugly. Q3: subtree dp. For any vertex v we can find vertex x in the subtree of v with maximal ax - d(x, v). Now let's run dfs from the root and find the vertex with maximal ax - d(x, v) among all vertices. If it's in subtree, we know it. Otherwise we can use answer for the parent. For any vertex we know next vertex on our path. If k is small. we can just simulate it. But the path will quickly become a periodic! So let's find a cycle and get the answer. |
|
+3
Your coefficients looks correct. But in my first sentence i meant that there is an easier way: let's consider a function g(n) = f(n) + n. 1, 3, 8, 21, 55. Something familiar, isn't it? I don't know how I came up with this, though. Generating functions are a rather universal way of solving such problems. I'm pretty sure that in this problem you can apply them, although I haven't tried. |
|
0
Let's also add a decomposition of n to the one term, the answer will be easier. Can you write any slow solution (DP or even bruteforce) to calculate it? It's really easy to guess the formula by values for small n. |
|
+6
What about "change an element" queries? If we solve the problem offline, they can be reduced to two "toggle" queries. But is it possible to solve it online? For example, by using order statistic tree instead of BIT? Also can someone explain why this structure is called a "wavelet tree"? |