LCMCONST - Editorial

So like you said we have 3 cases
And that leads us to only 2 possibilities everytime

  1. The power of a prime at the node is equal to the base value
  2. It is less than base.

This made for a bitmasking kind of an approach where all the masks could be checked without pruning

Later i just added backtracking with pruning. I initially thought of using the graph but even that wasnt needed.
Check my submission CodeChef: Practical coding for everyone

I just went from node i to node i+1 without caring about the relation between them xD

1 Like

Oh! This is a really nice approach. Whenever we see such a small n, there is almost always a bitmask solution :relieved:.

I visualized the problem as a graph, so I guess my mind stuck to the graph implementation :stuck_out_tongue:

3 Likes

tnx bro for being active every time we reply

1 Like

Ohk thanks…

What was your approach can you explain?. And what does that mean i just went from node i to node i+1 without caring about relation between them ??

Lets see the problem as a graph problem (However just for understanding, no DFS or BFS :slight_smile: )

First of all we will calculate answers for each power base and then would
multiply for answers for all powers, to get the final solution:
Here in my graph , ith index of array represents node ‘i’.

Lets solve the problem for power base p.

Lets say in the queries, A is connected to B and C with lcm’s being,
x and y resp. then max value at node A can be min(x,y) always.
(Lets say x was min, then if we give x+1 as node value, then the lcm
between A and B would exceed the given lcm would be >x, not valid.)

Now if we go through queries,
we will have an array (a) with max values of each node stored.
Let say arr is like = (a1, a2, a3, a4).

If no restrictions were there, then our answer would have been
Total arrays = (1+a1)(1+a2)(1+a3)…
Since allowed values for each is [0,ai]

But now there are restrictions of lcm between 2 nodes.
Now lets say if A and B was connected with lcm between them for
power p be ‘c’. Then if max value of both A and B is less than ‘c’
then we can never achieve that LCM. Thus if it happens in the graph
for any edge, our answer would be 0.

Lets say one of them is >=c (can always be at max c (Think! ;))
Then, we have to give one node a value of c and other node
can take whatsoever is wants , since their lcm would be c.
Here in the product = (1+a1)(1+a2)(1+a3)…
We are actually multiplying each possible value of node with others.
But this is not possible in case when edges is there bw nodes(both being less than c would mean invalid combination.)
, thus we would remove those terms in which any two nodes is
connected by an edge

For example
The graph was 1--------2
With lcm for power p being 3, and a1=3 and a2=3.

The possible values of array are :
0,3 ; 1,3 ; 2,3 ; 3,0 ; 3,1 ; 3,2 ; 3,3 = 1+3+3 = 1+a1+a2
( we deleted the term a1*a2 since there was an edge between 1 and 2).

Thus by following this algorithm, we find answers for each power
and then would multiply each other and get the answer.
(Beware of self edges-> say node a has self edge, then all the terms
with a gets deleted).

Overall Time Complexity → O(n^4)
My solutionhttps://www.codechef.com/viewsolution/35531057

2 Likes

Why accepted solutions are giving WA
accepted-CodeChef: Practical coding for everyone

WA- CodeChef: Practical coding for everyone
both codes are same.
Is there any changes in test case?

Yes…tc were updated.

why were all solutions not rejudged? Why was there no announcement about it?

Tc were updated in practice not in contest. There was thread on discuss by another person regarding it after the contest. You can try to find it.

1 Like

“set the values of other vertices to values in the half-open range [0, v)”
why v is not included in this range?

unable to implement basic back tracking need your help
use any classic example

unable to implement basic back tracking need your help
use any classic example

Take the problem of finding and printing all cycles in a graph. This has a backtracking solution.
This might help.

If that’s hard to follow, here is an easier backtracking problem.

How did you guys think about this problem in terms of graph? Because it is not really evident that we should make a graph and then proceed.
Can someone explain the intution behind this?
@aneee004

With a little practice you’ll be able to easily convert questions to a graph wherever necessary.

The observation here is that, the LCM relations are not independent. Interaction between A1 and A2 should be compared with the interaction between A1 and A3, A1 and A5, and all other existing constrains on A1 together, which pushes you towards using a graph.

Also when I drew the constrains on a piece of paper, I connected two array indices with a line, and kept connecting stuff randomly, which ultimately looked like a graph and it pointed towards dfs.