Comments

Great Effort. :)

0

Glad you liked it. Updated the wording in P2.

0

Auto comment: topic has been updated by dominique38 (previous revision, new revision, compare).

0

Glad you liked it

I nominate this

0

Auto comment: topic has been updated by dominique38 (previous revision, new revision, compare).

It's a good solution indeed. Hopefully not just for myself, the crucial part feels a little vague.

we can detach some nodes from $$$1$$$ and connect them to a greater node to increase the tree's value to $$$x^2$$$, we can instead from a sum $$$(x+1)^2$$$.

My attempt to explain it more elaborately.

  1. Claim 1: $$$x$$$ being the smallest integer such that $$$x^2 \geq S$$$ . Then for all $$$n \geq 5 $$$ , we have $$$n \gt x$$$ and $$$ \lceil (n+1)/ \sqrt 2 \rceil \geq x $$$ .
Proof

Corollary: Taking $$$d = x^2 - S$$$, then $$$2 \sqrt S \gt d$$$ must be true.

Proof

Now, our initial tree is rooted at $$$1$$$, and all other nodes are directly connected to $$$1$$$ as leaves.  Now I want to rearrange the leaves such that my sum is increased by $$$d(=x^2-S)$$$. 

Let's say we pluck the leaf $$$v$$$ and attach it to $$$u$$$. Then our new sum, $$$S_{\text{new}}$$$, is calculated as:

$$$S_{\text{new}} = S - v + uv = S + v(u-1)$$$

If the required increment satisfies $d \leq n$, we can choose the parent $$$u=2$$$ and the leaf $$$v=d$$$. Our job will be done in a single step. If $$$d \gt n$$$, we need a multi-step approach. We claim that we can always find $$$v_1$$$ and $$$v_2$$$ such that $$$n \geq v_1, v_2 \gt 2$$$ and their sum is $$$d$$$.

One possible method for partitioning $$$d$$$ into two parts (given $$$d \gt n$$$):

  1. If $$$d$$$ is odd: Choose $$$v_1 = \lfloor d/2 \rfloor$$$ and $$$v_2 = \lfloor d/2 \rfloor + 1$$$.

  2. If $$$d$$$ is even: Choose $$$v_1 = d/2 - 1$$$ and $$$v_2 = d/2 + 1$$$.

For $$$n \geq 5$$$, these vertices $$$v_1$$$ and $$$v_2$$$ will be $$$\geq 3$$$ in both cases.

But if $$$d=1$$$ or $$$d=2$$$, we need to be a little careful.

Case 1: If $$$d=2$$$, then $$$2 = x^2 - S$$$. We can rewrite this as $$$2 + 2x + 1 = (x+1)^2 - S $$$. Let's break the LHS into 2 parts, $$$3 + 2x$$$. We need to achieve a $$$2x$$$ and a $$$3$$$ increment. We can get that easily in 2 steps: plucking $$$x$$$ and attaching it to $$$3$$$ (which will give a $$$2x$$$ increment), and then joining $$$3$$$ to $$$2$$$ (which will give a $$$3$$$ increment).

Case 2: If $$$d=1$$$, then $$$1 + 2x + 1 = (x+1)^2 - S$$$. Let's make the LHS into $$$2(x+1)$$$. Then we can always pluck $$$x+1$$$ and attach it to $$$3$$$, giving us a $$$2x+2$$$ increment. This is a legal move since $$$x+1 \leq n$$$ . 

Submission link 344535551

Edit1:

Mistake: In calculating $$$x^2$$$ and in using the Ceiling property $$$\lceil x_1 \cdot x_2 \rceil \leq \lceil x_1 \rceil \cdot \lceil x_2 \rceil $$$, I mistakenly took the lower bound of this product when determining the upper bound. That was a stupid mistake.

The following edits are made:

  1. In the Proof section, it is acceptable to take the lower bound when proving that $$$x$$$ indeed follows this inequality: $$$ \lceil (n+1)/ \sqrt 2 \rceil \geq x $$$.

  2. In the Corollary Proof, a much easier and tighter bound to prove is $$$2 \sqrt S \gt d$$$.

  3. Handling the case when $$$d \gt 2$$$: Since $$$d$$$ is the required increment, we can always find $$$v_1$$$ and $$$v_2$$$ such that $$$n \geq v_1,v_2 \gt 2$$$ and their sum is $$$d$$$.

In the forum we are given a prime $$$MOD (= 998244353)$$$ and the solution in the forum is using that fact too, but how do we find summation for arbitrary $$$MOD$$$ in subquadratic time, where we are not guaranteed modulo inverse exists?

I noticed a minor issue(maybe typo) in the transition of the DP state in P4 : Checking the existence of the Hamiltonian walk in $$$O(2^{n} \cdot n)$$$, since I'm writing a comment anyway let me explain with a little more detail which smol idea help us optimize it from $$$O(2^{n} \cdot n^2)$$$ to $$$O(2^{n} \cdot n)$$$ .

Idea — Since we only need a binary (yes/no) answer for each vertex $$$j$$$ in $$$mask$$$ to see whether it can reach $$$i$$$, we can use mask $$$X$$$ to store all that information instead of using $$$j$$$ (which is iterating over all $$$n$$$), this saves us extra $$$O(n)$$$.

Def. — $$$dp'[mask]~=~X$$$, $$$X$$$ is a subset of the $$$mask$$$, where the set-bits in $$$X$$$ indicate the vertices that allow for a successful Hamiltonian Walk across $$$mask$$$ which ends at the set-bit vertex.

Transition — For $$$i$$$-th bit that is set in $$$mask$$$ , we need to know whether $$$dp'[mask \oplus 2^{i}]$$$'s successful Walks can reach $$$i$$$-th vertex, for that we need to precalculate $$$M_{i}$$$, $$$M_{i} = $$$ mask made of vertices that are incident to $$$i$$$. If $$$M_{i}$$$ and $$$dp[mask \oplus i]$$$ share vertices we can extend our successful Walk towards $$$i$$$ and set $$$dp'[mask]$$$'s $$$i$$$-th bit as $$$1$$$.

$$$ dp'[mask] = \displaystyle \sum_{i=0}^{n-1} 2^{i} ~ \cdot ~ (( ~ dp'[mask \oplus 2^{i}] \& M_{i} ~) ~~ \& \& ~~ (mask \& 2^{i}) ~ ? ~ 1 : 0)$$$

Base — $$$dp'[2^{i}] = 2^{i}$$$ for every $$$i \le n$$$.

This relation is pretty interesting :)

Important GCD properties we will use are

  1. $$$gcd(a,b) = gcd(a \pm kb,b) = gcd(b, a \% b)$$$, such that $$$a \pm kb \geq 0$$$.
  2. $$$gcd(a,0) = |a|$$$ for $$$a \neq 0 $$$ , since any number is a divisor of 0, and the greatest divisor of $$$a$$$ is $$$|a|$$$ .
  3. $$$gcd(a_1 \cdot a_2,b) = gcd(a_1,b) \cdot gcd(a_2,b)$$$, if $$$a_1$$$ and $$$a_2$$$ are relatively prime.

Let assume $$$a \gt b$$$, the base cases are $$$F_{0}=0$$$, $$$F_{1}=1$$$ and $$$F_{2}=1$$$ .

Claim $$$I$$$ : Every 2 consecutive Fibonacci numbers will always be co-prime $$$gcd(F_{a+1},F_{a})=1$$$.

Proof I

Claim $$$II$$$ : $$$gcd(F_{a},F_{b}) = gcd(F_{a-b},F_{b})$$$

Proof II

Corollary from Claim $$$II$$$ : $$$gcd(F_{a},F_{b}) = gcd(F_{b},F_{a \% b})$$$ , from GCD property $$$1$$$ we can keep on subtracting $$$b$$$ till $$$a-kb \geq 0$$$ which gives us $$$a \% b$$$ at the end.

Final Steps :

  1. Now we are seeing that subscript variables are following same pattern for finding $$$gcd(a,b) = gcd(b,a \% b)$$$ recursively and indeed we do follow the same steps because of our proven Claims are allowing us do so.
  2. Just like $$$gcd(a,b)$$$ hit base case $$$gcd(x,0)$$$ and we claim $$$x$$$ is our Greatest Common Divisor, we will hit same base case for $$$F_{b},F_{a \% b}$$$ which will give, where $$$x = gcd(a,b)$$$
$$$ gcd(F_{a},F_{b}) = gcd(F_{x},F_{0}) = F_{x} = F_{gcd(a,b)}$$$
$$$ F_{gcd(a,b)} = gcd(F_{a},F_{b}) $$$

which proves the interesting relation between $$$gcd$$$ and Fibonacci.

very well explained and pretty well animated! Thats a very good start! :)