Problem 1:
You are given a weighted directed graph with N nodes and M edges. Determine whether there exists a path from node 1 to node N such that the sum of the weights on that path is a prime number. If such a path exists, print the minimum achievable prime sum.
Constraints:
N <= 2 * 10^5 M <= 2 * 10^5 Maximum edge weight <= 2 * 10^7
Problem 2:
You are given an array a of N integers and an integer T. You need to choose a subset of numbers from a such that the LCM of the chosen numbers is less than or equal to T.
Print the maximum possible size of such a subset.
Constraints:
N <= 2 * 10^5 T <= 10^9 a[i] <= 60








Nope, they are not easy.
First: looks NP-hard
Second: Observe that there are only 2 141 181 possible LCM values using numbers ≤ 60. We can precompute all such LCMs that are ≤ T.
Let dp[x] = maximum size of a subset whose LCM equals x. Initialize dp[1] = cnt[1], others = 0
Process the LCM values in increasing order. For each current LCM = cur and for each number v from 1 to 60 that appears in the array, compute nxt = lcm(cur, v). If nxt ≠ cur && nxt ≤ T, then: dp[nxt] = max(dp[nxt], dp[cur] + cnt[v]).
Finally, the answer is the maximum dp[x] over all valid x ≤ T.
O(LCM_count * max(a[i]))
Trying to make an actual proof that the first one is hard:
Since you said "path", and not "walk" that makes things easier for me :)
Basically we'll make a gadget to try to reduce to longest path / Hamiltonian path. We'll make all weights 1, and basically reduce "is the longest path from 1 to N exactly N-1 edges long" to this problem.
We need to find a prime gap at least $$$N$$$ in size: we want $$$X$$$ to be a prime, but no numbers in range $$$X...X-N+1$$$ to be prime, because then we can start from vertex "0", make an edge of length $$$X-N+1$$$ to vertex 1, and then the only way to get a prime path is a path $$$N-1$$$ edges long.
To prove that this prime gap exists, consider the sequence:
$$$N!+2, N!+3, ..., N!+N$$$
You can see that all $$$N-1$$$ numbers in this sequence are definitely composite, since $$$N!+x$$$ will be divisible by $$$x$$$ since $$$N!$$$ is divisible by $$$x$$$. So the next prime number after $$$N!+N$$$ can be what we target.
Asymptotically, $$$N!$$$ has roughly $$$N \log N$$$ digits, so this doesn't break polynomial time. Going to the next prime doesn't break the promise either since we know the density of prime numbers scales proportional to 1/$$$digits$$$.
I will slightly rephrase your proof, because the idea is very good but it took me some minutes to understand it.
Imagine that you have a graph $$$G$$$ that you want to search Hamiltonian path in. Assume that we can actually solve your
PrimePathproblem in polynomial time. We will show that we then will be able to solveHamiltionianPathin polynomial time, which will show thatPrimePathis NP hard.First, notice that we can loop over all vertices pairs and ask whether those are the endpoints to your Hamiltonian path. If for any of them are, we will have found the path and the graph is Hamiltonian. So now fix two vertices $$$a$$$ and $$$b$$$ as the potential path endpoints.
To check Hamiltonian path from $$$a$$$ to $$$b$$$ with
PrimesPath, add a new vertex1connected to $$$a$$$, and mark $$$b$$$ asN. All the edges in the original graph should have weights 1, and the new edge should have weightprimeAtTheGap'sEnd - N + 1. This way, only the Hamiltonian path will result in any path through the graph with prime total (the total will beprimeAtTheGap'sEnd).Btw, this also solved the "Walk" version, because for walks instead of "No solution" we will get some prime larger than
primeATGE.Edit: you can even check all potential endpoint pairs with one call to
PrimesPathif you make1andNconnected to all of the original vertices, and make only edges connected to1with this special weight (all other are 1).I don’t think walk guarantees Hamiltonian-ness. If we start at 94 (so the next prime is 97), we could also get a walk that goes to N in (any higher prime number).
If there’s an odd cycle then anything is possible, if only even cycles you can still cheese it half the time.
you are right ;)
To me both of them look NP-Hard with these contraints but if you decrease them to like 20 or something they will be a lot easier and like spammable...
Have you noticed GomerDoGo's solution or that $$$a[i] \leq 60$$$? As for a "nicer" complexity bound, $$$\mathcal{O}(n+2^{\pi(a)+\mathcal{O}(\sqrt{a})})$$$ looks decent.
nope bro I havenot checked other's solutions and trying to solve these problems, i already solved these for n being about 20 or that but not for these constraints yet...
it is not possible to solve the first one with these contraints even using Super Computer 💀💀💀
For second problem,
(I assume $$$1 \le a[i] \le 60$$$)
The prime numbers between 1 and 60 are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
let x be an integer in the inclusive range of 1 and 60.
The prime factorization of x can contain $$$2^5$$$ (when x is 32) but it can never contain $$$2^6$$$ (for that $$$x \ge 64$$$, which is not allowed)
The prime factorization of x can contain $$$3^3$$$ (when x is 27 or 54) but it can never contain $$$3^4$$$ (for that $$$x \ge 81$$$, which is not allowed)
Let P(m,n): The prime factorization of x can contain $$$m^n$$$, $$$m^{n-1}$$$, ..., $$$m^{0}$$$ but it can never contain $$$m^{n+1}$$$
It can be shown that the following are true: P(2,5) P(3,3) P(5,2) P(7,2) P(13,1) P(17,1) P(19,1) P(23,1) P(29,1) P(31,1) P(37,1) P(41,1) P(43,1) P(47,1) P(53,1) P(59,1)
So, total number of possible LCMs= 6*4*3*3*2*2*2*2*2*2*2*2*2*2*2*2*2 = 1769472
However, we can verify programmatically that the number of LCMs less than or equal $$$10^9$$$ is 206181 using the code below
We can count the frequency of each element in a. This frequency array is guaranteed to have atmost 60 elements. Let this frequency array be b
Now, we iterate over every allowed LCM value and calculate the number of permitted elements from array a that can be choosen in the subset. Let's call this computed value as m. The max value of m over all iterations will be the answer
no. of operations = 60*206181 which is approx. in the order of $$$10^7$$$
Code:
EDIT: fixed code taking too much horizontal space
EDIT: fixed a mistake (I used variable x to describe 2 different things but now i replaced one of them with m)
There is a proof for Poblem 1's hardness in both versions. It only proves Weak NP-completeness under a conjecture that maximal prime gap in arithmetic progression realtive to the common difference of this arithmetic progression is polylogarithmic. This proof is based on a Hamiltonian path problem's NP-completeness.
Let $$$n$$$ be the number of vertices in our graph $$$G$$$, $$$E$$$ the set of edges of $$$G$$$, $$$p_i$$$ be the $$$\text{i'th}$$$ prime, and $$$P := \prod\limits_{i=1}^np_i$$$. We also define $$$i$$$ being achievable by a DAG with given source and sink as an existance of a source to sink path with a sum of weights equal to $$$i$$$.
Gadget 1
We want a DAG s.t. all integers $$$i\in[0;2^k)$$$ are achievable:
It's easy to see that this gadget does what we want. We can multiply all the weights by $$$P$$$, so that we only could achieve multiples of $$$P$$$.
Gadget 2
This gadget has $$$n$$$ inputs $$$i_1,i_2 \ \dots \ i_n$$$ and $$$n$$$ outputs $$$o_1,o_2 \ \dots \ o_n$$$. There are only edges connecting $$$i_k$$$ to $$$u_k$$$ with weight $$$\frac{P}{p_k}$$$.
Gadget 3
This gadget also has $$$n$$$ inputs $$$i_1,i_2 \ \dots \ i_n$$$ and $$$n$$$ outputs $$$o_1,o_2 \ \dots \ o_n$$$. For each edge $$$(a,b)\in E$$$ this gadget has an edge connecting $$$i_a$$$ to $$$o_b$$$ with weight $$$0$$$.
Gadget 4
This gadget also has $$$n$$$ inputs $$$i_1,i_2 \ \dots \ i_n$$$ and $$$n$$$ outputs $$$o_1,o_2 \ \dots \ o_n$$$. It has $$$n$$$ gadgets 2 and $$$n-1$$$ gadgets 3 "chained" together. By this I mean that $$$\text{i'th}$$$ output of the first gadget 2 is connected to the $$$\text{i'th}$$$ input of the first gadget 3 by an edge of weight $$$0$$$. They are just interleaved this way.
Final graph
Now we need a long enough gadget 1(according to the conjecture polynomial in $$$n$$$ suffices). We connect the sink of this gadget to all inputs of gadget 4 with edges of weight $$$0$$$. All the outputs of this gadget we connect to a singular sink vertex with edges of weight $$$0$$$. If we don't allow zero weights just add $$$P$$$ to all the edge weights. We run our alleged algorithm solving Problem 1 on this graph where the source is vertex $$$1$$$ and the sink is vertex $$$N$$$. The result is the same as asking about existance of a hamiltonian path in graph $$$G$$$.
Explanation
Since it's a DAG, a walk is the same as a path. Notice that each path has to go through exactly $$$n$$$ gadgets 2. Each of them is a type of a "gate". A working path has to go through each input index of a gadget 2 exactly once. If not, we have an unvisited index $$$t$$$. Notice that all the weights except that for index $$$t$$$ in gadget 2 are divisible by $$$p_t$$$, so the sum on such a path can't be prime. Next we are interested in coprimality of the sum on a working path with $$$P$$$. Notice that for each $$$i$$$, $$$p_i$$$ doesn't divide this sum because all the weights except exacly one are divisible by $$$p_i$$$ and this one is not. Since the sum is coprime with all the prime factors of $$$P$$$, it's coprime with $$$P$$$. Thanks to coprimality and our conjecture, there exist a path inside the gadget 1 s.t. the whole sum is prime. Finally we just showed how to solve the Hamiltonian path problem using a solution to Problem 1. We have used weights of magnitude $$$\text{exp}(\text{poly}(n))$$$, and thus proven Weak NP-completeness of both versions of Problem 1.