Introduction
This blog is inspired by this blog, which is an interesting constructive problem:
- How to construct a DAG with no multiple edges, such that there exists exactly $$$k$$$ distinct paths from vertex $$$1$$$ to $$$n$$$?
Obviously, there is a trivial solution which requires $$$2k$$$ edges and $$$k+2$$$ vertices. Simply connect vertex $$$1$$$ to each vertex numbered from $$$2$$$ to $$$k+1$$$, then connect each of these vertices to vertex $$$k+2$$$. In this blog, we shall discuss how to minimise the number of edges used.
Using no more than $$$4 \log k+1$$$ edges
This is likely to be the first way that many people will approach this problem. Consider the following structure: 
There are exactly $$$2$$$ distinct paths from $$$1$$$ to $$$3$$$. If we chain them together as such: 
There will be $$$2^3=8$$$ distinct paths from $$$1$$$ to $$$7$$$. We can see that it is possible to create $$$2^m$$$ paths using $$$3m$$$ edges.
For simplicity, we will call the chain $$$1 \to 3 \to 5 \to 7 \to \dots$$$ as the backbone, and $$$v_i$$$ to be the $$$i$$$-th vertex from $$$1$$$ on the backbone. For example, $$$v_0=1$$$, $$$v_1=3$$$, $$$v_k=2k+1$$$.
Using the method above, we can only construct a graph when $$$k$$$ is a power of $$$2$$$. Consider the binary representation of $$$k$$$. We first construct the backbone such that the number of paths has the same most significant bit as $$$k$$$. Then, for every non most significant set bit in $$$k$$$, say its the $$$j$$$-th bit, we add an edge from $$$v_j$$$ to the terminal vertex. For example, for $$$k=11$$$, its binary representation is $$$1011$$$, and we can construct the following graph:

Since for each $$$v_i$$$, there are exactly $$$2^i$$$ path ending at that vertex, we simple connect the positions corresponding to the set bits to the terminal vertex. There is a slight detail: we can't connect $$$v_{b-1}$$$ to $$$v_b$$$, as multiple edges are not allowed. In this case, we need to add another vertex and another edge.
In the worst case, where $$$k=2^p-1$$$ for some integer $$$p$$$, we need $$$4p+1$$$ edges: $$$3p$$$ for the backbone, $$$p$$$ for $$$p$$$ set bits, and $$$1$$$ extra for the detail above.
Can we do better?
The answer is Yes. To utilise edges even more, we need to change the structure of the backbone. After some thoughts, I came up with the following structure:

where $$$i$$$ connects to $$$i+1$$$ and $$$i+2$$$ for all $$$i \le n-2$$$, and $$$n-1$$$ connects to $$$n$$$. It is not hard to see that the number of distinct paths ending at vertex $$$i$$$ would be $$$F_i$$$, the $$$i$$$-th fibonacci number. The proof is left as a small exercise (hint: consider dp).
Using this structure, we can create $$$F_m$$$ paths using $$$2m-1$$$ edges.
Surprisingly, we can still use the binary representation idea above. Just like every integer can be represented by the sum of distinct powers of $2$s, every integer can also be represented by the sum of distinct fibonacci numbers. The theorem is called Zeckendorf's Theorem.
What makes this construction better is that: in the binary method, we need to consider the worst case where all bits are set. However, in the fibonacci method, the number of distinct fibonacci numbers each integer is partitioned into is not that large. In fact, I bruteforced all $$$k$$$ no larger than $$$10^7$$$, and I found that the maximum number of edges used is only $$$83$$$; for randomly generated integers in the range $$$[10^{17},10^{18}]$$$, the maximum number of edges used is $$$199$$$, whereas you need around $$$240$$$ for the binary method (worst case).
I don't have proofs of whether this method will be better for very large $$$k$$$s, like up to $$$10^{100}$$$ in the original blog. If you have explored this problem even deeper or is willing to, please share any interesting discoveries.
Anyways, this is my first blog on Codeforces. It is very coincidental, as I have made this problem on polygon for my friends a few months ago and they rated it around 2400. The constraints in that problem was $$$k \le 10^{17}$$$, and maximum $$$200$$$ edges.
Don't really know how to end off, so just hope everyone's rating grows like fibonacci numbers. Good luck!








Auto comment: topic has been updated by __baozii__ (previous revision, new revision, compare).
Auto comment: topic has been updated by __baozii__ (previous revision, new revision, compare).
Auto comment: topic has been updated by __baozii__ (previous revision, new revision, compare).
This task is very similar to WEOI 2024 Maze
Auto comment: topic has been updated by __baozii__ (previous revision, new revision, compare).
Thank you for writing the solution.
Actually it's still possible if $$$k$$$ is up to $$$10^{100}$$$ which fits no more than $$$333$$$ bits, and i think first method is actually enough for the original problem
Based on your formula, we only need around $$$3 * 333 = 999$$$ nodes and $$$4 * 333 = 1332$$$ edges. In the worst-case ($$$k = 2^x-1$$$), we add at most $$$332$$$ extra edges, totaling up to 1664 edges max
The only thing need to worry about is bignum, it's kinda a bit struggling for me to rewrite the whole thing
Very nice construction!
To get a very large number with as little edges as possible, the Fibonacci construction is indeed optimal: Order all nodes by the number of paths to that node. Suppose some node has indegree d > 2, then we can replace those edges by d-2 intermediate nodes with indegree 2. Therefore, each indegree is at most 2. It is clearly optimal to have the edges going into each node to be from the largest nodes before that node. Therefore, we only care about the number of paths to the largest 2 nodes, lets say a < b. We have two options: either the next node has degree 2, in which case it is a+b. Or it has degree 1, in which case it is b. Since having more than 2 nodes of the same size is useless (since all nodes have indegree at most 2), we know that the node after that must be b+b. Hence, if we want to grow as quickly as possible, we have 2 possible transitions:
$$$(a,b) - \gt (b, a+b)$$$ with 2 edges
$$$(a,b) - \gt (b, b) - \gt (b, 2b)$$$ with 3 edges.
Note that after both the second largest element is identical, even though the first uses less edges. Moreover, if $$$a \geq (\sqrt[3]{4}-1)b$$$, then the largest element grows quicker on a log scale as well. Therefore, when $$$a \geq (\sqrt[3]{4}-1)b$$$, the first operation is optimal. Now, note that in the fibonacci sequence, 1, 2, 3, 5, ..., from 2/3 onwards, all ratios are larger than $$$\sqrt[3]{4}-1$$$. This means that after some of the second operations, if we do the first operation a single time, it is optimal to only do the first operation from that point onwards. Therefore, the optimal growth rate is achieved by doing O(1) of the second operations (in fact only 1!), then only do the first operation.
This argument for the (asymtotically) fastest growth is not sufficient for the question, since we need to add extra edges to the last node (as in binary/fibonacci expansion). However, we can easily see that the fibonacci construction is more efficient in this than the binary construction as well. Suppose we used F_i as well as F_{i+1}, then we might as well have used $$$F_{i+2}$$$ instead. Hence we need to use at most $$$\frac{\log_{~1.61}(n)}{2}$$$ extra edges, instead of the $$$\log_2(n)$$$ extra edges in the binary construction.
Awesome!
cool idea
When you shared another day, I learnt that all numbers can be represented as fibonacci numbers plus this cool construction. Goes to show how interesting things are!