This video teaches a dynamic programming approach to count pairs (X, Y) where LCM(X, Y) = L. The key insight is that while all divisor pairs of L have LCM dividing L, not all achieve LCM exactly L. The solution involves: (1) computing D² (total divisor pairs) as an initial overestimate, (2) subtracting DP values of all proper divisors to remove 'imposter' pairs whose LCM is a proper divisor of L. Two implementations are presented: O(N√N) using divisor enumeration and O(N log N) using a sieve-like push DP that processes multiples instead of divisors, leveraging the harmonic series for efficiency.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
I stared at DP and LCM. It stared back.Added:
In this video, we will talk about dynamic programming on LCM.
This is a very common and standard technique in the world of competitive programming, especially on CodeChef and Codeforces.
But I just realized that whatever tricks we call as standard, that might not actually be standard if you are someone who is new to the world of competitive programming.
So, every standard technique, even something as small as prefix sum, must have been something that is completely unknown to you.
So, that is why I'm creating this video so that you you get awareness about such standard tricks out there. Now, play pay close attention to what I uh talk about in this video, because some of these facts that I state, that might seem very obvious to some of you and absolutely baffling to others.
So, it all depends upon how well you understand your dynamic programming and number theory.
So, the problem that we will discuss is this simple problem that for each L from 1 up to 10 to the power 6, find out how many pairs exist such that the LCM of these two numbers is equal to L.
Now, notice that in this first iteration, they have not specified the constraints on X and Y, but this is the first standard observation that you can make, that they don't actually need to specify the constraints of X and Y, because one thing is for sure that LCM of X and Y is greater than X and also greater than or equal to Y.
This means that if they have just specified L, it is apparent that X and Y, both the elements of the pair should be less than or equal to L.
But as a corollary, you can also solve the second problem that you see on the screen, which says that how many pairs have LCM greater than L.
And this is the problem that appeared in a Codeforces in a CodeChef round recently.
And now you see that this time they do have to specify the constraints on the uh integers X and Y. So, the constraints were specified as X should be less than equal to L and Y should be less than equal to L. But you can already see that if you are able to solve this problem, this version, the first version, then the second version is very trivial. The second version is simply L squared minus summation of the first L prefix value.
So, if if your LCM is greater than L, it means that the bad pairs are basically the ones where LCM is less than or equal to L. It means the LCM is equal to 1 2 3 up to L.
So, with this algorithm, you can compute all of these values. You can sum it up, and then you can subtract it from L squared, and you would get your answer.
So, that is why in this video, we will not for focus too much on the second problem. Instead, our goal would be on the simplified problem which says given an LCM, how many pairs achieve this?
Now, how do you solve this problem?
There are actually multiple ways to solve this problem. In fact, there is a way that doesn't even involve dynamic programming.
But in this video, I specifically want to talk about the dynamic programming approach because that is something new that you can actually utilize in other problems.
And that is the goal for this video.
Now, let's see how well do you know your LCMs. So, if I ask you, what is the LCM of two and three?
It's six.
What is the LCM of six and five?
You will say it is 30.
What is the LCM of seven and three?
It is 21.
Uh let's make it harder. What is the LCM of nine and five?
It is actually 45.
What is the LCM of six and four?
It should be 24.
But now, let's pause here for a bit. Is the LCM of six and four actually 24?
Because when you go in this pattern, it seems that LCM most of the times is usually just the product of these numbers. So, that's why for some of you, your brain might instinctively respond and say that the LCM of six and four is equal to 24.
But you know that it's not true. If you do the actual computation, the LCM of six and four is equal to 12. It is not equal to 24.
But how wrong are we if we do actually say that LCM of 6 and 4 is equal to 24?
Are we completely wrong or maybe a little bit wrong? Because that would dictate how you develop the algorithm not just for this problem that deals with dynamic programming on the LCM.
But this product intuition that you have is actually almost correct. So you just think in your mind that the LCM of two numbers is equal to x into y.
It is almost correct. There is just one small caveat and that small caveat is what is used in the harder version of this problem where the constraints are up to 10 to the power 9.
And in this hard version we use this same intuitive thing that our brain just did that the LCM of two number is equal to product of them. But this only holds in some special scenarios. What are the special scenarios that I will not cover in this video? And how to actually solve that 10 to the power 9 version that I also I would not cover in this video.
But in this video let us try to focus on this fact that when we say that the LCM of 6 and 4 is equal to 24, how wrong are we? Are we completely wrong or are we a little bit wrong? And what can we actually derive and what can we actually conclude from this intuition that LCM of 4 and 6 is equal to 24?
So you can see that why did 24 sound intuitive? Because you just tried to multiply 4 and 6.
So you can see that 4 and 6 if you just do the multiplication, then you can see that four and six multiplied together, they produce 24.
So, 24 is indeed a common multiple. It is not just the lowest common multiple because the definition of LCM is lowest common multiple.
So, you can see that 24 is indeed a common multiple. So, if your brain thought that LCM of four and six is 24, it is not entirely wrong.
But then, what is the connection between 24 versus what the actual LCM is?
So, the actual LCM is 12.
And your brain just produced 24.
So, can you see what is the connection between them?
You can immediately notice that 12 is equal to uh 24 is equal to 12 into two.
This means that 12 was the lowest uh common multiple and 24 was also a multiple.
So, one obvious thing that you can conclude from this is this fact that all the common multiples are actually divisible by the lowest common multiple.
Now, we are entering the territory where I'll state a couple of facts. And as I already told you before that it might be trivial to some and baffling to others. So, the statement that you see on the screen, it's actually not very obvious unless you think deeply about it. So, we are saying that all the common multiples, they are actually divisible by the lowest common multiple.
This means that if your brain thought that the LCM of 4 and 6 is 24, it is not wrong, not that much wrong. In fact, even if you end up thinking that LCM of 4 and 6 is equal to 48, even then you are also not wrong. But, if you think it is 40, then you are completely wrong.
So, you can generalize it and you can say that if you start thinking of of LCM as this part, uh let's see. If you So, if your brain thinks that your LCM of 4 and 6 is equal to 12, of course, it is correct. But, if it thinks it is 24, then also no big deal as such. It can also think it is 36 and 48 and 60. So, you can see that all the common multiples of 4 and 6 are exactly this 12, 24, 36, 48, and 60. And now you can see the proof of this fact in action. So, this how did we arrive at 12, 24, 36, 48, and 60? Because you have this lowest common multiple and you multiply it by two, that is how you get the second lowest common multiple. And then you multiply it by three, that is how you get by third lowest multiple and so on. So, in fact, this statement is true that all the common multiples, they are indeed divisible by the lowest common multiple.
So, till now we have seen that saying that LCM of 4 and 6 is equal to any of these numbers is not entirely wrong because it already hides some information. What is the information that it hides? because if you claim that I am claiming that my answer is almost correct and my claim is that LCM is 48.
So, I can conclude something concrete from this.
What can I conclude? I can conclude that the actual answer is a divisor of this.
Why? Because this is a multiple of this.
So, keep this fact in mind. We will come back to it later.
Uh what is this fact? That this 4 and 6 it has it actually has a connection not just to 12, but all the multiples of 12. And all the multiples of 12 are exactly the lowest common multiple.
So, all the common multiples of 4 and 6 are the multiples of the lowest common multiples. So, you see it's a it's going in a circle when I say that all the multiples all the common multiples of 4 4 and 6, they are actually the multiples of the lowest common multiples.
So, these are some of the facts that you have to be very familiar with if you want to solve problems related to LCM and GCD.
So, now let's move on to the next fact.
Now, if I say that LCM is a multiple of X, if you compute this LCM of X and Y.
So, I claim that if LCM of X and Y is equal to L, then I my claim is that L is actually a multiple of X. So, L would be equal to either X or 2X or 3X and so on.
Now, take a minute to pause this video and think about how you can prove this.
Did you get the proof?
So, in fact some of you might not be able to get this proof, but actually it is right there in the definition. What does the definition says? Definition literally says it is lowest common multiple.
So, common multiple literally says that the multiple of X, which is X, 2X, 3X, 4X, and so on. And LCM of Y, which is Y, 2Y, 3Y, 4Y, etc. And then you find out what is the thing that is common and what is the first thing that is common, and that you call it as LCM. So, by definition LCM of X and Y is equal is a multiple of X and also a multiple of Y.
So, it is Now, it is obvious if you think about it. Okay? Now, let's go on to the second fact. So, the second fact that let's consider any number >> [clears throat] >> X and Y and suppose that their LCM of X and Y is equal to L.
Now, I'm claiming that X is a divisor of L and Y is also a divisor of L.
Now, this is not clear from the definition, of course, right?
And uh how do you prove this?
Now, now if you look at this fact one So, fact one might be immediately clear to some of you, but this might not be obvious, like why is this fact true?
That why is X a divisor is guaranteed to be a divisor of LCM.
So, this is true because if you really understand your multiples and divisors, you will know that multiples and divisors, they are siblings.
That is if X is a multiple of Y, then Y naturally is a divisor of X. So, you can see that fact two at first glance does not look obvious, but it is actually a corollary of fact one. Because in fact one we said that LCM is a multiple of X. This means X is already a divisor of multiple and Y is already a divisor of the multiple.
So, using this reasoning, you can say that X and Y they are indeed divisors of L.
So, this is of course a necessary condition.
Now Now you know that Now, if I give you any random number 23 or like let's say 286 and I tell you, "Okay, can you tell me about numbers that have their LCM equal to 286?"
Now, you can see that you can claim with kind of confidence that, "Okay, these numbers have to be divisors of 286. That is for sure. They have to be divisor pair. Like X has to be a divisor of 286, Y has to be a divisor of 286. X and Y could be equal, but both of them has to be a divisor.
But now, just like what we did earlier, like we claimed something that was not obviously correct, but almost. Like we claimed that LCM of 4 and 6 is equal to 24.
What happens if I claim that all the divisor pair will have LCM equal to 286?
Is that correct?
So, this time I want to ask the question, like how wrong is it to say that if I have a number L, I know for sure that LCM I know for sure that X and Y, they are divisors of this LCM. If they want to achieve this LCM, then they have to be the divisor. But can I also say it the other way around? That is, if I pick any two random divisor and if I pair them up, will I get LCM equal to L?
How correct is it? Or how wrong is it?
So, you can see that we are claiming it both ways, right? We are saying that first we have to prove that if X and Y are divisors, if X and Y have LCM equal to L, then they indeed have to be divisors, which we have already done from this fact, right? That X and X and Y, they are indeed the divisors for LCM. So, that's why in one direction we have already proved that if their LCM is equal to L, then of course they have to appear as a divisor pair.
Because if they are not appearing as a divisor, if X is not appearing as a divisor in L, then X their LCM could never be equal to L.
But now you will notice that not all pairs that appears at as a divisor, so so suppose it has total number of divisors as D. There are D divisors in total.
So, how many pairs can you create? That is equal to D into D.
Because you can use that same divisor twice.
And this is the fancy thing about LCM. Because if you think about four and six you in your mind you created a 24 which is equal to 4 into 6. So you you were trying to make this divisor and this divisor separate and then you were trying to create your LCM. But now see what happens when the actual LCM is equal to 12.
So if the actual LCM is equal to 12, you will notice that 12 cannot be written as 4 into 6 at the same time.
So that is the beauty of LCM that you have to do it one at a time. That is you write it as 4 into 3, but 4 appeared.
That's good. And then you write it as 6 into 2. And now you can notice that 6 appeared which is good.
So that is why there is overlap between them.
So it is perfectly fine for you to reuse the divisors. So you can see that we reused this two, right? And then we created this three. We borrowed a two from here and then we created six over here.
So that's why if this L has D divisors, then you can freely place all the D entries in the first place for X and you can also freely place all the D entries for Y. So there are D squared possibilities for the possible pairs of divisors.
Now we know for sure that some of the pairs whose LCM is guaranteed to be equal to X whose LCM is guaranteed to be equal to L, they will indeed appear in in this divisor pair. That we know for sure. But you will now notice that there are also some imposters lying inside of it.
Because not all divisor pairs will achieve the LCM equal to X. And I will show you an example why that is true.
We'll consider the same example, 24.
Let's consider the same example, 24. And you know that 4 is a divisor of 24, but 6 is also a divisor of 24.
Right? Both of them are are a divisor of 24, but are But do we have LCM of 4 and 6 is equal to 24? No.
Right? So, this 4,6 pair is a imposter that is lying inside that D cross D uh selection that you have just made.
But when we say that these imposters there are some imposters lying, and when we talk about this this fact that how wrong are we, it turns out that we are actually not that wrong in claiming that all the divisor pairs uh that all the divisor pair will have LCM L. We are not that wrong in claiming this. So, now next we will explore what do the actual LCM of these imposters look like.
Now, from your intuition, what do you think the actual LCM of these imposters look like?
So, with this example that I've just given you, and with the fact that I shared with you earlier, that all the common multiples are actually multiples of the lowest common multiples.
This means that your gut feeling might say that there if you have any pair whose LCM is not equal to this L, then their LCM has to be equal to a divisor of this L.
Right? Because we said that all common multiples are actually divisible by LCM.
Right? So, this number that you are just dealing with, this number that you are just dealing with.
Yeah, this number that you are just dealing with, this must also have been the L It must have been something like this.
Uh you must have computed the LCM of X and Y.
And you multiplied it by two or multiplied it by three or multiplied it by four and that is how uh we got this reasoning, right? That even though LCM of four and six is not equal to 24, but this could have been equal to 12, 24, 36 because all of these, they are actually the multiples of the LCM. So, they are the multiples of the LCM. So, if they are the multiples of their LCM, then their actual LCM must be hiding as a divisor inside this L.
So, this is what our gut feeling says, that if X and Y are divisors, then either their LCM has to be equal to L.
Which we have already proved.
Or if the Or if their LCM is not equal to L, then their LCM has to be equal to a divisor of this L.
Now, this is what our gut feeling says, but we actually have to prove this fact and this fact is not at all obvious. In fact, I would say that this is the most [snorts] difficult fact to prove, but it kind of looks very intuitive.
So, this time we want to find out how wrong is it to say that all the divisors pair will actually create an LCM L.
So, this time we make a very bold claim that if you have a divisor pair and their LCM is not equal to L, then their LCM has to be a divisor of L.
And to actually do a formal proof of this, you would need a different visualization of LCM. And for this I I have already created another video on my YouTube channel that talks about this different visualization of this LCM. And some of you might be aware of it.
Some of you might be aware of it it and then some of you might not be aware of it, but this is also a very common and standard technique in competitive programming. So, one definition is that LCM is the lowest common multiple.
Right? You write down all the multiples and then you find out the common ones and then you pick the lowest. But there is another visualization of LCM that I don't think a lot of people know that if you want to compute the LCM of X and Y, you compute it like so. You write down the prime factorization of X, suppose it is equal to P1 to the power X1 and uh P2 to the power X2 P3 to the power X3 and P4 to the power X4.
And you write down the prime factorization for Y.
Suppose it is equal to P5 to the power X5 and P2 to the power Or let me uh write it like so.
Y is equal to P5 to the power X5 and P2 to the power Let me use Y2 over here.
and P3 to the power Y3 over here.
and P6 to the power Y6 and in fact also here also let me use Y5.
Y5 So, you can see that X and Y have P2 as a common prime factor, P3 as a common prime factor, and P1 is unique, P4 is unique, P5 is unique, and P6 is unique. So, their LCM LCM of X and Y is actually equal to for each prime factor for each PI you compute maximum of XI and YI.
So, since P1 is not present over here, so you retain this full P1. So, P1 to the power X1. And since P2 is common, so you retain P2 to the power max of X2 and Y2.
Same for P3. So, you retain P3 to the power max of X3 and Y3.
and you retain P4 as it is, you retain P5 as it is, you retain P6 as it is.
Why?
Because you still apply that max formula, but the exponent of P6 over here is zero.
So, this is the alternate definition of LCM. In fact, GCD also has this alternate definition where where you use not max but min.
Now, using this alternate definition of LCM, can you prove this fact that all the imposter pairs they will have if they do not have the LCM equal to L, they will actually have the LCM equal to one of the divisors of L.
Now, you can see that it's very easy to prove this. Why? Because consider the prime factorization of L. It will be equal to P1 to the power X1, P2 to the power X2, P3 to the power X3 and P4 to the power X4.
Then, consider X. X can only X cannot introduce new prime factors, so it can only take from P1, P2, P3, and P4. So, suppose X is equal to P1 to the power Y1 and P3 to the power Y3.
And Y is equal to, let's say, P2 to the power uh Z2 and P1 to the power Z1, let's say.
Then, LCM of X and Y, now you can notice that it would be P1 to the power max of Y1 {comma} Z1 and P2 to the power max of Z2 {comma} 0 and P3 to the power max of Y3 {comma} 0.
But, now notice that max of Y1 {comma} Z1, but since Y1 since X is the divisor, Y1 has to be less than X1 and Z1 also has to be less less than uh Z1 also has to be less than X1. This means that whatever prime factor that you end up accumulating in the LCM, they will always look like P1 to the power M1 where M1 is less than equal to X1 and P2 to the power M2 where M2 is less than or equal to X2 and this number this number you can obviously see that this is this number is also a divisor of L. Why? Because any divisor can be constructed by having X1 plus one choices for the first exponent X2 plus one choices for the second exponent and so on.
So now we have proven that even though this fact looked very obvious, we even our gut feeling told us us so that all the imposter pairs of the divisors if they if their LCM is not equal to L, then it has to be equal to a divisor of L.
Now at some Now Now one thing you can notice that this actually just proves one thing that if you have a divisor pair X {comma} Y, then if their LCM is not L, then it has to be a divisor of L.
But it doesn't actually say that all the divisors all the pairs XY So suppose Suppose L has a divisor equal to eight.
Then this fact that we have just proven this this doesn't state that all the elements whose LCM is all the pairs whose LCM is equal to eight, which is this divisor, they are hiding inside this imposter pair list. This doesn't prove it. So the next fact, which is also not obvious, the next fact claims that all the imposter pairs they are actually exhaustive. And by exhaustive I mean that if you have your LCM is equal to L then you find out all the divisor pairs X Y and you also find out all the divisors. So suppose one divisor is two, second divisor second divisor is three.
Then you have four as a divisor, then you have a 12 as a divisor, six as a divisor and so on. So we are claiming that all the pairs that achieve LCM six, they will indeed appear as an impostor pair over here.
Previously we talked the other way around. We we said that if if the pair is an impostor then it has to be its its LCM has to be in this list. But but this time we are saying that every time an LCM is in this list, they also have to be in the impostor pair. And this is also actually not very obvious.
But if you know this fact that divisors of a divisor are also a divisor of the original number. In fact, we already used this fact uh uh kind of in that previous prime exponent thing. But let's talk about it again. Like divisors of a divisor are also a divisor of the original number.
So what this means that consider any number 48, right? So you see that 40 12 is a divisor of 48.
Right?
This means that anything that divides 12 should also divide 48. Hence the claim that divisors of a divisor are also a divisor of the original number. So any divisor at this level, they are also a divisor of this level.
Now using this fact, you can now show that it is indeed exhaustive. Why is it exhaustive? Because the first fact claimed that if their LCM is indeed equal to L, then they have to necessarily appear as a divisor pair.
This means that any number whose LCM is equal to 12, they have to appear as a divisor pair inside 12. But, if they are appearing as a divisor pair inside 12, then they also have to appear as a divisor pair inside 48. Why? Because the list of divisors of 12 is a subset of the list of divisors of 48.
And that's why now you can see that this incorrect claim that we had earlier, that our brain thought that the LCM of 4 and 6 was equal to 24, it wasn't that wrong in the sense that yes, it is 24, but if you want to find out how many pairs, how many pairs achieve LCM is equal to 24, you will know that it hides a lot of information inside it.
It hides the information how many pairs achieve the LCM 24, but it also hides the information how many pairs achieve the LCM 12. It also hides how many pair achieve the uh LCM eight.
And four. And two.
And one. And six. And so on.
So, now you can see that you can now apply a dynamic programming kind of approach because this time, so in typical DP problems, you provide contribution to some other values.
But now, this time your job is to deconstruct something. You have to reverse engineer something because this this DP value of L, suppose you define DP value of L is equal to D into D. Basically, all the divisor pair. Then, this time you have already computed this DP value, but this time this DP value is right now it is incorrect.
So, then you have to uh remove all the imposter pairs. How can you do that? By subtracting the DP values of this, this, this, and this, and so on. So, let's move on to formalizing this dynamic programming.
So, we define DP of L as the number of pairs that achieve LCM exactly equal to L. Notice the word exactly. This time we are not saying that LCM should be a divisor of L and whatnot. We just want to have a very simple DP definition which says that DP of L is this.
But how do you compute the DP transitions?
So, first, you can say that let's write a wrong DP and add corrections to it.
So, you say that you compute the number of divisors. If this number is X, you compute the number of divisors, suppose you get D divisors, then you know that you can create D into D pairs, and they will achieve LCM equal to X, but we said that it is wrong, but not very wrong, right?
So, first you start with that wrong DP, and you populate all of these values.
But now, notice that if this is L and you have to correct the DP value of L then this time you have to subtract the DP value of let's consider this simple scenario 12.
And what are the factors of 12? It is 1 2 3 4 6. Okay, these are the proper divisors of 12.
So you have to [snorts] notice that all the pairs whose LCM is equal to one, they are hiding inside this.
All the divisor pairs. All the divisor pairs whose LCM is equal to two, they are hiding inside this. All the pairs whose LCM is equal to three, they are hiding here. All the pairs whose LCM is equal to four, they are hiding here. All the pairs whose LCM is equal to six, they are also hiding here. In fact, all the pairs whose LCM is equal to two, they are 12, they are hiding over here.
This means that if you can remove all the pairs that LCM is equal to a proper divisor, then what remains at the end will be equal to the number of pairs whose LCM is exactly equal to 12, but there is a caveat. The caveat is that you cannot subtract the existing DP values because the existing DP values are marked wrongly for one and two as well, right? For everything it is marked as D squared, D squared, D squared, D squared, and so on. So you have to subtract the correct DP value for all of these children.
This means that if you can somehow find out the DP value of these, correct DP value of these, then you can subtract these correct DP values from this incorrect DP value of L and you would get your answer. Then you can already see why dynamic programming is being applicable over here because to find out the answer for L, you have to first find out the correct answer for all the numbers that are smaller than L.
And if you are able to correct their DP values, then you can go over all of those DP values and then you can subtract the actual contribution that is hiding over here. And what will remain at the end is the actual pairs whose LCM is indeed equal to X.
This means that if you can do the DP processing from left to right, then by the time you have arrived at L, you have already corrected the DP value of all of the smaller nodes. So, then it is actually allowed to just do DP of L minus equal to DP of imposter where imposter is a divisor of L.
Why is this not wrong? Because you started with D square for every imposter, but then since you are processing everything from left to right, you are correcting all the DP values from left to right.
And why will this be correct? Because of this fact that all the divisors of any number, they are strictly less than or equal to L. So, that's why you will never subtract a bad contribution. Right? Whatever contribution that you subtract, that will actually be equal to the exact number of pairs whose LCM is equal to one, whose LCM is equal to two, whose LCM is equal to three.
So, all we have to do is just simulate this process and you can see that we only need the divisor list to do this DP operation.
So, let us look at the code for this.
So, first we write a simple utility function that computes the divisor of any number. and this is also a standard technique in competitive programming, but again, I believe not everyone is aware. And this is standard trick says that every divisor appear in conjugate. So, if you have a divisor X, then N by X is an integer, but not just an integer, it is also another divisor. This means that if you look at this expression X less than N by X, then you can see that X square is less than equal to N. It means that X less than equal to root N.
This means that for every conjugate pair, what the smaller element of the conjugate pair is on the left hand side of root N and the larger element is on the right hand side of root N. So, you only have to iterate over root N and add the conjugate pair manually. And this time, you have your O of root N algorithm to compute the list of all the divisors. And once you have this formula, you start doing your DP. So, first of all, you write the wrong DP for everything. And in the wrong DP, you add everything including your imposters. So, the your your imposters would be you compute the number of divisors and D into D is the number of pairs that has LCM equal to L, but there are imposters and the imposters are the divisors.
So, then you try to fix the DP value and you try you can fix the DP value by subtracting the actual DP contribution from left to right. So, you go from left to right and for each number, you first find out all of its divisors. And by now, you can assume that all this DP of divisor, this will be the corrected value because divisor is always less than the number. So, you subtract the correct contribution of the divisor and what would remain after this loop is the correct value of dp of i. And by definition, dp of i meant the number of pairs with LCM equal to l. So, this time complexity is O of root n, and this time complexity is just n root n.
And this is also n root n. So, you see that that complicated problem we were able to just solve in O of n root n.
Now, we know one algorithm that can solve how many pairs achieve LCM is equal to l. Now, if you have watched the video so far, that's already good enough. But in the remaining part of this video, I will show you another trick that is also a very standard technique in dynamic in competitive programming that can achieve the same thing in n log n, and it won't require O of n root n.
So, now this might be a bit interesting to some of you but how can you actually subtract the contribution of all the divisors without actually iterating over them, and how can you actually get rid of this root n factor and replace it with this log n? So, this is also something that is not obvious. So, let's talk about it.
So, how do we uh improve the time complexity?
So, all of this So, again, our idea is same. We want to start with the incorrect dp, and the incorrect dp says that claim that all the divisor pairs are LCM equal to x.
So, claim that all the divisor pairs x and y, which will be d squared divisor pair, uh they have LCM equal to l, but realize later that some imposters are there, and the imposters are one are the ones whose uh imposters are the one that are divisors of this l.
So you have to uh subtract their contribution so that core idea is the same but we want to do it in a smart way.
So So this time what we'll do is we focus on this aspect. Remember that earlier we talked about this fact that divisors and multiples are siblings, right? This fact. This fact will come in very handy now.
How are the siblings? Because we said that if X is a multiple of Y then you can claim that Y If X is a multiple of Y then Y divides X. So that's why they always occur in pairs. So whatever you can do with divisors you can also deal also do with multiples. And remember this fact, this is actually more powerful than you think. That whatever you can do with the divisors you can do the same thing with multiples.
And here we were doing some shenanigans with the divisors. So what if we flip the perspective? So in fact this technique is by itself very useful in in competitive programming that you have to flip the perspective. Think not from the perspective of divisors but think from the perspective of multiples.
So in fact this technique you might you must have seen in a lot of contribution technique problems as well.
And what we will do is instead of trying to fix an L and then trying to remove all the imposters, we we will pick one imposter and we'll see in how many L it could be be So let's suppose this number is two.
So, if I give you the correct value of DP of two, because of course you need the correct value to remove the contribution, then can you figure out in which L is two hiding as an impostor? It will be all those L where two appears as a divisor.
But, you can flip this narrative and you can say that these L's are nothing but the multiples of two, which is nothing but this element, this element, this element, this element, this element, and so on.
So, if you already have the If you already have the contribution If you already have the correct value of DP of two, why not just go ahead and we know that this right now this value is wrong, because it is set to D squared, but why not just go to this value and subtract DP2 eagerly? So, this eagerly subtraction that we are doing, this will not make this value correct immediately, because there could be other impostors hiding over here.
But, you do do the subtraction for all of these multiples, and then let's say you reach three, and let's say somehow I give you the correct DP value of three. Then, you can apply the same trick for three. So, for three, you find all its multiples, so this would be six, and then you go to nine, and then you go to 12, and so on, and you subtract DP of three from all of them.
So, we are not claiming that DP of six will be corrected so far.
It won't be.
But, at least you have you are not If you are three, then you are not an impostor over here.
Right? Any Any divisor pair that achieves LCM three, they are not an impostor over here.
Now notice that all of this hinges on the fact that you know the correct value of DP two and you know the correct value of DP three when you do this when you subtract their contribution.
But how can how are you sure that you know the correct value of DP of two?
Here you can use induction and you can see that if you are processing from left to right and for each impostor X that you see first, so if you remove if you consider the first element and if you go ahead and remove the contribution of one from every multiple of one, then one will not act as impostor in any of them.
Now what will happen when you encounter two?
This time you can actually claim that DP of two will already be corrected already points to the correct value by the time you reach here. Why? Because the only multiples of two would have been on the left-hand side and you have already processed on the left-hand side with the correct value of one which we know.
So DP of two is correct. It means that you now subtract the contribution of DP of two from here, here, here, here and so on. Now when you reach DP of three, this time DP of three also will be correct. Why? Because you all the multiples of three are to the left-hand side and you already know the correct value of left-hand side and you have subtracted their contribution. There So those impostors are gone. Then when you reach DP of four, all the DP value of four will be correct. So in fact this idea is the one that empowers that sieve algorithm.
Now now you might wonder that okay, we can do this complicated thingy where we don't deal with divisors, we deal with multiples, but then how does it actually help us? How does it improve the time complexity? Because if divisors and and multiples are indeed siblings, then doing going from X to Y versus going from Y to X should be the same thing.
But surprisingly, this is also another well-known technique in competitive programming that this doing doing the multiple route doing by the multiple way is actually faster and the time complexity is N log N. Why is it N log N because you'll process the first element one and for one there will be N elements that you will have to go through.
But for two, when you are processing two, you will only process N by two elements because if you start making a jump of two, after N by two elements, you will exceed N.
And when you are processing three, you will only touch N by three element because if you start making bigger jumps, then in just N by three steps, you will exceed N and you don't want to go beyond that. So, this will continue like so plus N by four plus N by five plus N by six and so on and you will recognize that this is nothing but your this is bounded by the sum of harmonic series which is N log N. And that's why doing in this fancy alternative way brings down the time complexity from N root N to N log N.
Not that it is absolutely required. In fact, I was able to get an AC with both of these approach for this problem. But anyway, this N log N approach is very good to know because this is what will actually be reused in a lot of DP problems that deals with GCD, Mobius function, and LCM, and so on.
So, this time you can see that we are using the DP transition and our trick is same, but this time we are not using a push DP. We are instead doing a pull DP. And what is the difference between them? In push DP, you try to fix an L and you try to find out what should be correct value of DP of L be.
So, in order to correct DP of L, you start with a wrong value D squared and then you subtract all the imposters. So, that is why you are Uh that is why you are pulling from your divisors. You are pulling the contributions from your divisors. And this time you want to do a push DP. And in push DP, you start with a multiple and you start pushing your contribution.
And And in this context, pushing your contribution actually means taking back your contribution from these D squared pairs for each of your multiples. So, finally, let's look at this code. And it is actually counterintuitive that the code for this complicated part is much more simpler and easier to understand than the original N root N complexity code because here you see that you had a utility function and then that does this. But this time, with this push DP trick, it's just this much code and you will see that it is actually very easy to understand what we are doing. So, first, you have to count the number of divisors of each integer. And previously, we counted that in root N by actually computing the divisors. And this time you can already see that trick that within N log N you can actually find out the number of divisors by doing a push DP instead of a pull DP. So, you fix any divisor D and you iterate over all of its multiples and you increment their divisor count. So, this is Again, this is You can also say that this is a contribution technique because you are contributing. So, each divisor is contributing to all of its multiples.
So, in this manner you have your divisor count and if you have your divisor count, you can uh initialize your DP incorrectly with D into D pair.
Right? That is what you needed. And to fix your DP, all you have to do is follow the same trick again of push DP that you iterate through all the divisors.
And then you remove your contribution from your multiple. So, that's why you do 2 into D and you see that this trick that we are using that it starts with D and plus equals to D. This plus equals to D and not plus equal to I is what ensures that the time complexity is n log n because this plus equal to D means that you're doing a hop of D steps every time.
One minor thing that you would notice that in this first version, when we're counting the divisors, we started with D.
But in the second version, when we're uh dropping the contribution, we started with 2 into D. And this is obvious because if you are D, then if you're a number n, then n is also a divisor of n.
Right? And that's why we said that n is also a multiple of n.
So, that's why we started with this. But when you want to pull the contribution, you don't actually want to subtract the DP value of n, right? Because you only want to subtract the proper divisor. And by proper divisor, I mean the divisor which is not exactly equal to n. So, that's that's why we start this loop as 2 as 2 into D, but this loop as D.
And once you are done with this DP transition, then you can notice that the final DP value would actually represent for each DPL, it will actually represent how many pairs achieve LCM L.
And this code that you see on the screen, this is a recurring theme in a lot of number theory problems that deal with GCD, LCM, divisors, and multiples.
So, make sure that you understand this idea thoroughly so that you can actually reuse it. Actually, this n root n thing might not be reusable that much, but it's still that was a good starting point for us to understand what the DP is trying to do, and then we optimized it via this harmonic series trick.
So, then you can see that n log n is actually much easier to code and understand and maintain.
So, which is an interesting idea that the harder the harder version is actually easier to code and understand.
And yeah, that was it about the video.
Do let me know what you Do let me know in the comment section what do you think about the approaches that we have discussed, and if you have any other alternate approach to solve this problem because I know there is an alternate way to solve this problem that doesn't involve DP. I think I also saw that in the official editorial. So, do let me know how did you solve it in the in the official contest. And of course, if you are interested, there is also also a version with 10 to the power 9 where you have to find how many pairs achieve LCM greater than 10 to the power 9, and where there you again have to use your intuition that LCM is mostly x into y except some cases where it's not, and those cases are actually, technically speaking, they are where x It is only possible when x and y are coprime. So, there what you do is you you try to So, if your brain thinks that LCM of x and Y is equal to X into Y, let it think so and you can do that by you can correct your brain by saying that okay, if X and Y if they have the GCD G, then let's write G as G into A and Y as G into B and now your brain can keep on thinking that LCM is equal to A into B and this G is actually a common factor with the caveat that GCD of A comma B is equal to one. So it starts from this fact and yeah, it's a bit complicated so that's why I did not add that in this video, but hope that you at least got to learn something new from this video. That's all for today. Thank you.
Related Videos
Escaping the Fog
LogicLemurGaming
760 views•2026-06-03
Olympiad Mathematics | Indian | Can You Solve This One?
PhilCoolMath
650 views•2026-06-03
A Brutal Radical Expression Made Easy! The Shortcut Changes Everything.
tamoshop
112 views•2026-06-02
V : jee main /advance class 11 mathematics : Binomial Theorem class-1 ( 29 may 2026 )
dcamclassesiitjeemainsadva9953
125 views•2026-05-29
Is This Pentomino Tileable?
3cycle
241 views•2026-05-30
This Sudoku Has Many Lines!!
CrackingTheCryptic
2K views•2026-05-29
Olympiad Mathematics | Indian Can You Solve This One?
PhilCoolMath
268 views•2026-06-02
Olympiad Mathematics | Indian | Can You Solve This?
PhilCoolMath
669 views•2026-06-02











