 What's up guys, my name is Michael and welcome to my YouTube channel today We are going to go over planning trips, which is with the second problem of the top coder contest of SRM 799 okay, so Basically the problem is asking that This guy is going to travel the end locations This girl this girl is gonna travel the end locations. Okay. She's playing everything and she knows the travel costs the So the weird thing for travel costs is that the travel costs is It's the cost for a travel cost is basically summing a to the Cost of each ticket so the travel cost for the ticket trip turned out to be special It's the cost of all tickets powers for the same a so more precisely the travel costs of the location I cost a to the num I tickets units of money, okay, so She wants to pay for all n tickets at the same time and her payment must be a power of 8 Okay, so to avoid paying for more than necessary She wants to find the smallest non-negative integer case such that the payment of a to the k is enough to pay for all the tickets okay, so Here I'll explain what that means. Okay, so in the first example, we have 10 a is equal to 10 so let's actually Open this up So in the first example, we have a is equal to 10 and then The array is for each for the values for each tickets five six and three. Okay, so it's five six and three Okay, so these are the values for each ticket and the sum of the whole trip is going to be a to the the power of each Ticket right num of I Right, so it's a summation of a to the num of I okay, that's what it is so in this example what this is doing is that Hold up, let me just move this up real quick Okay, so in this example Basically, if you in order the total cost of the trip is going to be 10 Because it's a right to the fifth plus ten to the six plus ten to the third. Okay, and Basically after you add up all these numbers right You get a certain number right, which is going to be I think they even gave it to us Even gave it to us. Hold up Yeah, it's 111 1 1 0 1 0 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 0 0. Yeah, which is this number, right? Okay, and the what she wants is she wants to find The smallest k such that it's greater than this Okay, smallest a to the k. So here This value right we need to find 10 to the k has to be greater than or equal to this number Which is 1 1 0 1 0 0 0. Okay, that's what we need to find Is able to play for it. So in this case 10 to the 7th is greater than or equal to so in this case 10 to the 7th K is it would equal to 7 because 10 to the 7th would be greater than or equal to this Okay So how do you do this problem? Well before we Even get started doing the problem Let's think about powers Understand about powers. Okay, so let's say I had like a is equal to 4 or something right so let's say I'd like 4 to the 3rd plus 4 to the I have no idea Yeah, 4 to the 3rd plus 4 to the 3rd plus 4 to the 3rd plus 4 to the 3rd right so in order to understand how to do this problem Think about this. How would you simplify this? Right this expression? Well If you had this expression right you can't just add the exponents because In order to add the exponents That that only works from laws of exponents only works for multiplying them together So you cannot add the exponents what you can do is that because it's 4 to 3rd plus 4 to 3rd plus 4 to 3rd plus 4 to 3rd What you do you realize that? This is actually just 4 to 3rd is getting added To itself four times so if you want to simplify this expression You would be 4 to the 3rd times 4 right and because anything to the first power is 1 so this would be 4 to the 4th Right, so this is just like a basic example how you would simplify summations of exponents, right so Basically based on this conclusion as long as if I add a certain power Like this this power added a certain number of times and it's actually equal to a If it's if the if I add the certain number of times it's actually equal to a then then Then we could just simplify it down and remove the number of times this showed up And then this would be 4 to the 4th right so if you had a like if you give in like a giant expression So let's show another example Let's say this is this example 2 to 2 13 13 right so 2 a is equal to 2 and we have the arrays 13 and 13 right It's in this example The summation is going to be 2 to the 13 plus 2 to 13 right and in this case This is going to equal to 2 to the 13 times 2 whoops Right if you simplify this down, it's going to equal to 13 times 2 so then because these are Now multiplied and this is a 1 this is going to go 2 to 14 So the answer returns the n is going to equal to 14 right k would equal to 14 right because this is equal to 2 to the k Right, so the smallest k that is just larger than or equal to this value is going to be 14 So the idea for this problem is basically to simplify the exponents every single time Is basically to go through the array Every single time you add a certain number of occurrences of the exponent Right if it reaches a So in this case a is 2 right so if I added added this These values a certain number of times and it reaches a in this case is 2 right so 2 to 13 plus 2 to 13 I did that twice right what I would do is I would remove 13 from my number of occurrences and then I would add 14 right because I would that's the next largest exponent Right, that's just simplifying the next largest exponent That would add the next largest exponent right so if I keep doing this while I'm looping through my array of exponents In the end I'm going to get the largest exponent that I could use in order to do it right Um There is a caveat so let's say I couldn't simplify anything down So let's say I had like I don't know let's say I had 14 1312 or something right so in this case And a is equal to 2 right this case 2 to the 14 plus 2 to the 13 plus 2 to the 12 right In this case, I cannot simplify this down anymore Ideally I can't I don't think so. Yeah, you can't Yeah, because you're not adding to the 14 this down. So in this case It can't just be the largest exponent that you kept track of it. It's not it can't just be 14 So what you have to do is in this scenario you actually have to add one to the largest exponent So 14 plus 1 is 15 Right because if you sum up if you add these together the maximum exponent 2 to the K they're just larger than this Has to be The next exponent of your largest exponent, right? K would have to be the next exponent because 2 to the 14th is Not greater than it is smaller than the sum of 2 to the 14 plus 2 to 13 plus 2 to the 12, right? So like if I sum these up The largest exponent 2 to the K, right? It's can't be 14 Right because that's smaller than the rest So you have to take the largest exponent here 14 and add one by one. So in this case K would equal to 15 Okay, so yeah, that's basically how you do this problem. So you would loop through the loop through your array Keep track the number of times a number of occurrences You're doing a keep track the number of occurrences that you're adding up and then when once you reach The number of occurrences that you're adding up for each exponent reaches a you remove the previous You remove the previous exponent from your array and you add you add one to the next occurrence, right? So if it was 14 14 if this was To the 14 got added with to the 14 again, and it became to the 15 I would remove 14 from my array and I would add 15 to my array in the end Whatever value is just going to be your maximum exponent if Your maximum exponent if you have more than one if you if you have more than one size in your number of exponents like in this case 14 13 12 You have to add one to your final exponent, right? So if you had more if the size of your exponents in the end is greater than one Then it's just you have to add one to your final exponent. Okay? So I hope I hope I explained that correctly. So basically keep loop through your array Keep track of how many exponents you added While simplifying it, right? If you added a certain exponent The number of occurrences of exponent is equal to a right Remove that exponent add the next exponent. So add one 15 continue doing that until you're done with your array in The end if your array has more than one value, right? So in this case 14 13 12 if your array has more than one value and you can't simplify it anymore It's just going to be 14 plus one It's just going to be your largest value of your exponent plus one or so 15 if it's not Like if if if all your values are exponents actually simplified down to one exponent like two to the 14 Then it's just two to the 14. Okay, so yeah That's how you do it. I'm gonna code it up with you guys because I didn't actually submit this yet But yeah, so this solution is actually brought to you by this guy who actually coded this up He hacked me during the contest. So yeah but His solution makes sense. So I'll actually just code this up with you guys. Okay, so I'm in the practice room And um Yeah, so here we have find we have there We have find a vector int num. So this is like the The class and the method you have to submit So I'm going to include Map because I need to use a map to keep track the occurrences of each exponent So num is the number is the array of your exponents. So what I'm going to do is I'm going to loop through my Every single exponent, right? Now I'm going to keep track in any map. So let's call this map Uh, let's do long long int. Okay This is occurrence. So here I'm going to do occur at num at i plus plus Okay, so while I'm going through this if um Okay, so now I'm going to get the um, so what what he did is he got the While this is equal to a right then you have to erase it from your your your your value and then you add up Add the next value. Okay, that's what they mean so so in this case While my occurrence at num at i Is equal to a Right. So this is simplifying down the exponent While num at occurrences. So this is like Wouldn't I add it up a certain number of times? Right. I got the same a value, right? Um, what I'm going to do is I'm going to I'm going to what I'm going to do is I'm going to increase the count I have to erase the value. So first let's actually get our current Current value is actually occurrence at num at i I think so. Yeah Uh Oh, no, no, no, it's num at i what what's the one I'm doing Num at i so that's the current exponent. We're on right current current exponent. Let's call a current exponent Okay, um, what we're going to do is we're going to erase the current exponent from our Map, so we're going to erase this. So once we we erase this exponent and then once we have that Once we erase our exponent, what we're going to do is we are going to, um Increase the count, right? So the current exponent is now going to Increase right because we're going to when you add to the 14 plus to the 14, right and is equal to Uh two right you did that twice. You're going to reduce the exponents Down so this what this does is it adds one to the exponent, right? So once you finish adding one to the exponent, that's what happens Um, and then oh, yeah, you also have to increase the count Of the exponent so occur at current exponent Here plus plus, okay All right at the end of this what you want to do is you need to check if the size of current is equal to one Because after you're done I'm actually going to make this larger. Yeah, so it's easier to read Okay, so at the end of this Okay, so at the end of this, um What you want to do is you want to check if the occurrence The size of the occurrence is uh equal to one. So yeah here equal to the one Or um And one is yeah. Yeah, okay. So yeah, so you have to check if the size of the occurrence And um the number of occurrences at I think it's back back. Oh begin. Whoops Uh at the beginning Dot second is that what he did Yeah, okay beginning second So if this is the case then we return Um occurrence at beginning First Right Uh, yeah, I think I think it's first. Yeah. Okay. So we return for occurrence at beginning first And um, why do we do this? So remember in our scenario when we reduce the exponents down to one exponent So let's go back to this Is two we have 13 13 So two to 13 plus two to 13 Can be two to 13 times two it's reduced two to the 14, right? So at this point, um, what the code would do is remove both the 13s and uh at 14 So if our end of their array Is the size is one And the number of occurrences is one that means you can't reduce it anymore So this is just the right value the answer would just be two to the uh just be 14 Okay So that's the reason why um If it was like if there's more values then you can't you you have to add one So that's what the guy did on the next one So over here Yeah, it would be you have to add one to the last value. So So otherwise you have to do Arbygan Okay, so the reason why he does arbygan Um occur dot arbygan is the last value, which is the largest Value in your map the largest exponent in your map. So what he's doing is that he gets the He gets the largest exponent of his map and then he adds one right and the reason why arbygan Arbygan is the last value of your map of occur and occur what occur is is that it's uh, it's actually sorted from From lowest to highest right because it's it's a map. It's sorted So if you get the last value, that's just going to be the largest exponent That's you stored and you add one Okay, so ideally this would work. Um, let's compile Do not compile. What was the problem? Oh a long line was not declared in the scope So let's actually define long long uh long long as Oh define l allows long long Okay, um And now we just go to the test panel check all run all tests To we got wrong return 15. Did I not remove it pretty sure I did hold up and Num at n count dot erase Oh, I think I don't think okay if you're gonna loop from I to zero to the size I don't think the map Erases a current number If I recall Let's see map Yeah, it erases the wait. It also erases the key wise I don't know don't know why this is not current erase Race and and plus plus current and I Hmm, I don't know. I mean we couldn't make it auto auto at and I just don't know why it's not racing Auto current exponent Maybe it's because it has to be in order. I was not declared in scope. Oh current exponent It has to be current exponent also Mm-hmm Something's not right current exponent count auto n and num Oh, whoops num not occur. Yep. Let's test panel Now it works. Okay, so that's really weird how it didn't erase it. Maybe it's because like It sounds super weird weird. I didn't erase it Maybe you had to like erase at a position I don't know because when you use auto it actually gets the position of num num So maybe that's why but I don't know. I don't know why it didn't oh Wait Oh Okay, whoops. Okay. Yeah, I think I got a got a reason why I didn't Well, whatever. I think I understand. Okay. Um, submit it here. Uh, let's run all system tests And the solution pass system test So yeah, that's how you do this problem I hope you guys understand the solution of planning trips raycom subscribe. I'll check you guys later. Peace