Guest User

SRM 763 author notes

a guest
Jul 17th, 2019
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1.Loopsie Doopsie
  2. The only digits that have a loop in them are (0,6,4,9,8).
  3. We should not use any digit that does not contain any loop since if we included such a digit, we can always remove it to get a smaller number. Only 8 has 2 loops others have a single loop. To find the smallest number we should prioritize reducing the number of digits. So we use floor(K/2) times 8. If K is odd we need to use an additional digit. using 4 at the start will give us the smallest result. (If we include 0 instead, we will have to place it at the end which will result in a larger number).
  5. 2.Weird Triangle
  6. We know that non-degenerate triangle inequality says:-
  7. a+b>c
  8. a+c>b
  9. a+b>c
  10. So,
  11. If I choose vertices u,v and w.
  12. And uv= A[u]+A[v], vw= A[v]+A[w], uw= A[u]+A[w]
  13. So applying above,
  14. uv + vw > uw
  15. (A[u]+A[v])+(A[v]+A[w])>A[u]+A[w]
  16. So A[v]>0
  17. Similarly A[w]>0, A[u]>0
  18. ans is (numberOfPositiveElements)C3
  20. 3.ET Sums
  21. The process described finding the value of each node is nothing but performing the Euler tour on it. As a result, the values in the subtree of every node will form a contiguous range. Let mx(i) denote the maximum value in the subtree of node i which can be calculated using a simple dfs. In the node i, every value between i and mx(i) would be present. Now let us calculate the contribution of each node in the final sum. Node i will lie in the path from root to node of every node in it's subtree and it will be the depth(i)th node in each of those paths. Therefore, it's contribution will be:
  22. cost[i]*(Sum{j=i to mx(i)} depth(i)^j), which forms a gp and can be calculated quickly using sum of gp formula.
  23. The summation across all nodes will give us the required answer.
  24. Time Complexity: O(NlogN)
  26. 4.Cut, cut, cut
  27. We can deduce the following.
  28. Let's say our line is L, and let's say it intersects with k previous lines.
  29. We can say that the number of pieces gets incremented by k+1.
  31. 5.Root it right
  32. Root the tree at 1. Find the following for each node using dfs:
  33. cost[i] = cost of node i as described in the problem statement.
  34. subcost[i] = sum of cost[j] across every node j which lies in the subtree of i.
  35. sm[i] = sum of values nodes on the path from 1 to node i.
  36. subsum[i]= sum of sm[j] across every node j which lies in the subtree of i.
  37. ct[i]= number of nodes in the subtree of i.
  38. Now if we root the tree at a child X of 1 instead, the difference in the sum of costs will be as follows:
  39. A. For every node j in the subtree of X, node 1 will no longer lie on the paths and the coefficient of terms of cost will decrease by 1, effectively the new cost will be:
  40. cost[j]=cost[j]-sm[j]-val[1]
  41. The effective change in subcost will be:
  42. subcost[i]=subcost[i]-subsum[i]-ct[i]*val[1]
  43. B. For every node j outside the sub tree of X, node X will also lie on the paths and the coefficient of terms of cost will increase by 1, effectively the new cost will be:
  44. cost[j]=cost[j]+sm[j]+val[X]
  45. We can calculate the change similar to the previous step.
  46. After calculation for total cost rooting the node in every possible way, we choose the minimum.
  47. Time Complexity: O(N)
  50. 6.Product and product
  51. We can find :
  52. sum{for all possible combination(x1,x2..xn)}(product{i=1 to n}(A[i]+xi)) ---------(1)
  53. and then divide it by (M+N-1)C(N-1).
  55. Lets consider a polynomial,
  56. P(x,i) = sum{i=0 to inf}((A[i]+i)*x^i)
  58. We can see that (1) is a coefficient of x^m in product{i=1 to n}P(x,i)
  60. If we try to compute this directly using ntt, the time complexity would by NMlogM
  62. Lets see P(x,i):
  63. P(x,i) = sum{i=0 to inf}(A[i]*x^i + i*x^i)
  64. P(x,i) = (A[i]*sum{i=0 to inf}x^i) + (sum{i=0 to inf}i*x^i) ----------------(2)
  65. Now sum{i=0 to inf}x^i is expansion of 1/(1-x)
  67. And sum{i=0 to inf}i*x^i,
  68. we know 1/(1-x) = 1+x+x^2...
  69. Differentiating and multiplying by x,we get
  70. x/(1-x)^2 = x+2x+3x^2...=sum{i=0 to inf}i*x^i
  72. So resubstituting these in (2), we get
  73. P(x,i) = (A[i]/(1-x))+(x/(1-x)^2)
  74. P(x,i)=(x*(1-A[i])+A[i])/(1-x)^2
  76. So now since the degree of the numerator is 1, we can multiply using divide and conquer and ntt in O(MlogN*logM)
  78. Now we have something like this:
  80. (1) = (polynomial of degree at most m)*(1-x)^(-2n)
  82. We can expand (1-x)^-2n and then iterating the power of x in (1-x)^-2n we can calculate the coefficient of x^m.
  84. Time Complexity: O(MlogNlogM)
RAW Paste Data