First find all connected components of robot graph, and treat all numbers in single component as one node of the graph. (I have used DSU for the same)
Now directed edge is added into the graph, if there is a requirement of B at position A and component of A and B are not same. so directed edge com[A] → com[B] is added.
Now, run the DFS on the generated graph. if reach at some node which is
a. Not Visited - increase the ans
b. Visited But not in current path - increase the ans
c. Visited and in current path - dont increase the ans (this edge represents cycle, so not counting this edge towards final ans)
Also dont increase the ans for starting node of dfs (Basically, there is no edge to be counted in this case)
Sir, in your solution why the get method is returning character values?@arthurfn
Also if anyone else has understood setter’s solution, kindly provide the reason
j=(j-1)&i This line. This implies the set bits in j will be a subset of i, as in j&i==j. There are \binom{k}{i} numbers with i set bits, and j will have 2^i possible values. So we get \sum\binom{k}{i}2^i=3^k
Infact, it will go through all possible values of x such that x&i==x.
Edit : It seems you don’t know the math behind \sum\binom{k}{i}2^i=3^k
I will give a simple combinatoric proof.
Proof
Each bit has 3 choices, Either not in i and j, Only in i but not in j, or in both i and j, therefore there are 3^k possible values of i and j.
#define debug(args…) printf(args)
when they ran it locally they used to log with printf but when they have to submit instead of commenting or deleteing they just remove “printf(args)” so logs don’t get printed
Hi Can you please explain this part of your code?
How can there be a case as you commented
“if node i occurs in the path,
// and if its not detected in the dfs,
// then this is not a simple cycle.”