 What's up guys, my name is Michael and welcome to my YouTube channel today We are going to go over last digit, which is a basically a problem on spodge, which is Calculating the last digit Basically, you're given two numbers a and b and you want to find the last digit of a to the b power Okay So a could the largest possible value for a is 20 and the largest possible value b is about 2 million almost 3 million So and we're given t test cases and the largest possible value of t is 30 So our job is basically to Find the last digit of a to the b power. Okay, and they're given t test cases Okay, so this problem is a little bit difficult, but it's actually not that if you think about it It's actually not that hard So I'm actually going to explain to you guys how to do it Using something called modular Exponentiation and then you will be able to solve this problem very easily, right? So the first thing we have to know is about the properties of modulus So modulus has a unique property when you have multiplication So let's say I have two numbers a and b and I multiply them and I take these numbers and I just mod by n Right so when I take these numbers are mod by n what I'm doing is I'm just getting the remainder Getting the remainder when I divide by n and that's remainder the thing is you actually can distribute this mod By n into each of these numbers. So this is actually equivalent to a mod by n multiplied by b mod by n and Then mod the whole thing by n Right the reason why is because in modulus you find the remainder you you just it's you just have to distribute this It allows you to distribute these into both sides and think about it kind of like multiplication and distribution So yeah, you could do this in modulus when you do modular arithmetic while doing this problem So now let's actually explain how this problem works Using this property. All right. All right guys. So now imagine I want to do a to the b mod by p, right? So I want to calculate this right so I could actually split this a to the b power into this actually Right because I could just I could have a times a to the b minus one mod by p right because of this this This part this part a to the whatever power is actually just gonna be one a to the first power, right? And if you multiply them together, you're adding one plus b minus one, which that's just gonna get you to be Right, so you could split a to the b to this become this right? so Now now when you do it this way, let's try just We could actually take this power This modulus by p and distribute it on both sides So then we have this a mod p like this and then we multiply by a to the b minus one Mod by p Okay, and then to make sure we don't get like completely off track we mod by p again Okay, so now we have this a to the mod by p multiply a to the b minus one mod by p Okay, now we actually can go even further. All right, the reason why we could go further is because Once we calculate a mod by p we just could you just use this and not actually have to recalculate this over and over again so then if you know at this you could Break it off again and multiply by again. So let's actually do that. So let's have a Mod by p multiply by a to the b minus one mod by p This whole thing mod by p right now. Let's break this off. So this is going to be equivalent to a mod by p Let's break this this a minus a to the b minus one mod by p. So this is going to be a to the b Minus two multiply by a to the one mod by p Mod by p so then now let's distribute this mod by p in both sides. So then we have a mod by p right multiplied by a To the b minus two mod by p multiplied by a mod by p and Then mod by p again Okay, and you can multiply mod by p again in the outside, but yeah, I think you guys get the gist of it So because we know about this we could easily break this down again and continue going over and over again and If you think about it, this this is the same thing as Taking this and moving it out a mod by p because you're multiplying by a mod by p again So technically we don't every time we do this exponent B minus two not be a to the b power, right? We actually could divide by two every single time Right because we could split every single every every power of two Right into a mod by p multiply a mod by p Right, and if we could split them in twice every single time it prevents recalculation over and over again, right? So, yeah, that's basically using this idea We could now use this idea into modular Exponentiation and try to do this ourselves. Okay, so going back to this problem here if we multiply the a To the b power mod by p, right? The only thing that matters is when this exponent What we could do is we could keep dividing this x want to be by two every single time, right? Because in every say every two power we could split into a mod by p multiplied by a mod by p Right and then mod by p, right? We could every every single second power. We could do that and That would that's just recalculating this again and then multiplying it to your final answer By this right you're gonna multiply your answer of a to b minus two mod by p Right by this right because if you're splitting every single Second power split this exponent B into two parts You could split into this a mod by p multiply a mod by p mod by p and then you calculate the rest of this and If you realize that the only thing that matters is that when B becomes an odd number When B becomes an odd number, that's the only time it would affect your actual result Right because that then you couldn't multiply your whatever result you got from modding by p of this That mean number of times over and over again and multiplying it that result by your initial result and mod by p Okay, because the odd number is like the last left over that you split it part over and over again So if you split input to like groups of two A mod by p over again This odd value is when it's odd That's the left over and that was the one that would affect your actual answer So let's actually just code it up right now and then I'll I'll explain it along the way All right our guys So here I have a function of power and this power is good just going to represent a This is going to represent a to the power of B mod by p. Okay And This is the function that we're going to call and we're going to pass our p as 10 because if you mod by 10 that gets you your last digit, right? We're going to pass that in into this function Okay, so how do I write this modulo? Exponentiation function. Okay. First of all If our initial answer was divisible by p, right? So if First let's create a result Answer in that let's just initialize it as one Okay, because this is going to be the result that we're going to return in the end Now, let's check. Let's first check if if it's divisible by p, right? Because if this value is divisible by p Then the last digit is going to be zero you're going to return zero and the reason why is because if it's if it is Divisible by p you don't have to calculate the modulus over and over again, right? Like if five if I'm doing five to the second power and five to the second power I'm modulating by finding the remainder when I divide it by five if I know fives divisible by five It's just going to be zero right no matter how much power you give it's going to be zero. So that doesn't matter Okay, now here's this part where it's going to get a little tricky What am I going to do is I'm going to set while my exponent is greater than zero While my exponent is greater than zero I'm going to keep I'm going to keep dividing my exponent by two okay, and Every time I divide my exponent by two. I am actually going to multiply My a result My results and not I'm not my results my a and I'm going to multiply by itself and mod by p right so if For here every time I do this. I'm going to actually be is going to be divided by two Okay, because we're divide every time we divide by two we split the the exponent into two groups of a mod by p multiplied by a mod by P right so then during that when this happens. I'm going to multiply a times a and I'm going to mod by p Okay, so that's going to solve this initial problem of splitting by two every time now the Issue happens is when if it's an if the exponent becomes odd So when the exponent becomes odd what you're going to do is we're going to check if b mod by two is Not equal to zero. So when this exponent is odd, right? This is the time when we modify our result. So when we modify our results We are going to set results is going to equal to a result multiplied by a and we're going to Yeah, yeah result multiply by a and then we're going to mod by p because The only time when if it's odd that means it's that's the leftover value and that leftover value is going to affect the actual Modulus of the answer, right? If it doesn't affect it if it's even it's not really going to affect it that much So that's why we do this part. We just calculate it multiplying it by itself and mod by p But this one is when really when it affects it, okay? after this is done I'm going to return result and That's going to basically be the answer of this problem and then I'm going to call power So I'm going to read in the test cases t while t minus minus I read an a and b and I'm going to call power a b and I'm going to pass in 10 and the reason why I pass in 10 is because 10 if you mod by 10 Any number mod by 10 is going to give you your last digit, right? And we want to find the a to the b Mod by 10 to get the last digit of a to the power of b. Okay, so I didn't actually test this yet. So I'm going to actually I'm going to actually submit the solution and see if it gets Accepted I'm not super sure about it yet Ideally, this is how it works though And it got accepted. Yeah So yeah, that's basically how you do this problem now we could actually optimize this even further So what I mean by that is that if you want to check if something is odd or even you don't have to actually do mod by 2 and Check if it's not equal to 0 you could just and this by 1 and That's going to tell you if it's odd or even. Okay, and here when we say b divided by 2 You actually can shift the bit right by 2 and that's actually going to Divide the number by 2 so yeah And that's I think that's the best you can Yeah, I think that's actually the best you can go do at this point. Let's just submit it to see if Submit it again to see if this works, but I think that's actually the highest Highest optimization you could do okay. I got wrong answer now. That's weird. Um, I Think it's B shift right wait B is equal to B shift, right? Shift right by 2. I think I think that's how it works. I don't know. There's a way to divide by 2 really fast I think it should work now Okay, I'm still getting wrong. I'll hold up. Let's see how to shift right How to divide by 2 fast and C++ there's a way to do it just by shifting right by 2 I think Shift right by 1 Maybe that maybe that's the thing shift right by 1 Let's see. Let's see if this works now and then after that. I'm just going to After that, I'm I'm just going to see if that works Should work now. Yeah, now I got accepted. Okay. Yeah So if you want to make it even faster if you want to divide by 2 easiest ways to just shift the number right by 1 And that's gonna help you if you want to check the remainder if you want to see if something's odd Or even easiest for to check if it's odd you just and by 1. That's the easiest way to check if a number is odd But yeah, that's how you do this problem. It's using Modular exponentiation raycom subscribe. I hope you guys understood this video. I'll check you guys later. Peace