 guys my name is Michael and welcome to my youtube channel today we're going to go over this ad code beginner contest 171c problem statement uh what is this what is this problem statement oh one quadrillion and one dalmatians huh that's weird okay so uh whatever this this many numbered dogs suddenly appear under the roof of a house all which he wanted to keep dogs are numbered one through this many dogs and he gave them names so 1 to 26 are abcd up to z 27 to 702 is aabac up to z y zz 703 to 18278 is three a's and then it goes to two a's one b two a's one c going up to two z's one w uh one one y and then three z's then we have 18279 goes up to seven five seven four seven five two five four and that's four a's and then three a's one b three a's one c going up to three c's one y four z's and then five a four seven five two five five and four seven five two five six are five a's and then four a's one b and then yada yada so on to sum it up it goes like this okay so this is what it is and basically you want you're given the number n and you want to know what the name of the dog is based on these rules okay so how do you do this well first of all let's find a relation pattern between all these all right guys so based on what the problem statement said i redrew all the corresponding numbers and then the corresponding characters that correspond to the numbers so all these numbers and stuff like that and now i'm going to basically try to to solve this problem you have to think about how the relation between these numbers and the characters that's what you have to think about so first of all we have one to 26 and that's a b up to z and these are single letters right so let's call the length of the the numbers of this to be l so because we only have a length one of one character for one to 26 l is going to equal to one okay now let's look about the at the second one our length is two right a and then zz's goes up to zz which is two so our l is going to be equal to two and let's just name l the three l the four for all these right l equal to five right these are just the lengths of the string so i that's just a numbering that i made up okay so now let's think about the relation between these numbers excuse me so we if we look at this if we look at each every single character here right we realize is that it's actually changing the last character of each number um this should be this should be ab yeah my bad this is ab um so if we realize that we're actually keeping the same letter and then we're changing the last character so a becomes b and we're increasing it by one right and we go on so forth and later on we uh the first character we increase it to one afterwards and it gets as easy and we do the same thing for the rest of these characters a a a becomes um wow that i'm not doing this right a a becomes ab and then it goes on zz um this is actually third one was the same thing it's a a a a the fourth one is a a a and then a a a b and then it goes to zz zz right so we're change only change the last character and then later we change this character and this character and this character until we get up to zz z and here it's the same thing right so now let's try to represent these characters that we are repeating and we're changing in terms of one to twenty six because that's what we represent one to twenty six ads all right so what is twenty seven how do we represent twenty seven as twenty six that's just twenty six plus one okay so the twenty seven is just twenty six plus one and i just wrote that here okay now what's seven oh two well if you try a bunch of calculations seven oh two is actually twenty six squared plus twenty six so if you try a bunch of calculations you can put that in your calculator and try it but yeah that's what it is now what's seven oh three seven oh three is just seven oh two plus one so if we represent seven oh three as seven oh two as a in the values in twenty six that's just going to be one plus twenty six squared one plus twenty six plus twenty six squared yeah how about represented an increasing order right yeah one plus twenty six plus twenty six squared and now let's look at one eight two seven eight what is that that's twenty six to the third plus twenty six squared plus twenty six if i recall correctly one eight two seven eight yeah so it's twenty six to the twenty six plus twenty six squared plus twenty six to the third okay and let's look at this one one this is just one eight two seven nine is just one eight two seven eight plus one so we're gonna do twenty one plus twenty six plus twenty six squared plus twenty six to the third and then we do the same thing here it's just the same Values just another power. So it's 26 plus 26 square plus 26 cube plus 26 to the fourth Right, let's just make sure that let me quickly make sure that's right Yep, that's right. Yeah, and then this one goes on the same fourth So on so forth one plus 26 plus 26 square plus 26 to the third plus 26 to the fourth and Then this one is just the last one. We didn't label it But it's just 26 plus 26 square plus 26 to the third plus 26 to the fourth plus 26 to the fifth Okay, so as we could see here We're just increasing the power each time of 26 for the last one when we're adding it When we're adding it right the first one we're gonna start at one for all these Relations of the pattern we start at one and we add 26 then we add 26 square plus 26 to third So it's just the last number plus one and we do this so on and so forth So now let's try to find a relation comparing to the length of our Letters so here L is equal to let's start at beginning. Okay, so actually not not beginning Let's start at L equal to four so at L equal to four. We realize that The first starting starting number which corresponding to a a a a right That is one plus 26 plus 26 square plus 26 to the third So if L is four, what am I what is my last exponent? My last exponent is L minus one right look at look at my last exponent. It's three Three is L, which is four four minus one three is four minus one. So I know I'm iterating through Increasing this number of exponents up to L minus one, right? That's what this is Well, it turns out turns out that if you were to keep doing this and finding the pattern for all these values you actually have to Get is that the name final name of N is Going to be the tape name among the 26 to the L names of length L right So basically We if you subtract The current So okay basic so basically we are given the number the number n right This is the number that we need to represent this in terms of these letters Right these letters and the number of n it could be like 804 805 806 807 We need to classify this n we need to classify this end in terms of its L's right So if it's if we find the pre-subtract and some number of times the ones that are not What n is right? We will get to the certain corresponding length of L Right, so if n it was like 705 Right, and if we subtract 705 from 703 Right, that'll give us the current current location based on our Our representation of L equal to 3 right if we could if we have subtract our current number from The boundaries of where it's at which is like 703 or whatever to hear if we find them with a certain number boundaries We subtracted by it Pretty much we would be able to find whichever Number it lies between right so if it's 705, let's say the number 705 right if I could find that It's between 703 and 18278. I know that I have to repeat a Length of L of 3 That it's a length of 3 right. It's a length of 3 and Then if I know it's like the 3 then I based on the location of which it is from the beginning I would be able to Repeat a certain number of letters for that number if you guys understand So like I would be able to repeat certain number of letters to be a a and an a b a c Whichever how far it is from my starting number then I could get my get my Actual representation of what n is right whichever number of representation it is so if you see this Basically We have to subtract n from some number from n Okay, and that'll give us the kth name among the 26th to the length L Okay, so how do how do I subtract it? Well, if I look at the last boundaries 26 plus 26 square plus 26 third That's length of L. Okay. That's like them L if I were to subtract n minus This last boundary and get that representation of where it is between The starting and the ending I would be able to get the right location of which I which it is I'm repeating so it turns out we have to subtract n minus 26 plus 26 squared plus yada yada up to L minus one. Okay, and that's based on what we found so far here Right. So like here based on this we see that. So This this last number is going to be L minus one So that's why we have to subtract it based on that, right? It goes up to 26 to L minus one and that'll give us the Kth name among the 26th of the L length L. Okay So now we want to find the kth among the strings of length L so now that we have the Kth among the strings of like L We just have to find the kth name among these numbers of L Okay, so Um To do this We we have to think about it in terms of when We have to let's re-rechange this problem. Okay Make it a simpler problem because now that where I've like found the boundaries of where this L is Lying based on this equation. We need to let's change this problem a bit. Okay, so let's make this problem a little easier So let's this is going to be a simplified version So this is going to be a simplified version and What am I going to simplify it as? Let's consider the situation where there's only 10 Alphabets, so there's only a b C up to J. So there's only 10 alphabets Right. This is only 10 alphabets Under these this assumption the number of length L length 3 is Going to be 10 to the 3rd. So if if my length is 3 of my string It's going to be 10 to the 3rd, right? And that's going to be 1000 and this is going to be the length string a a a a a b going up to a a j Okay, I'm not a a j goes up to JJ J. It's my bad JJ J Okay, so that's what this representation is. Okay, so among them, let's say the 246 Whatever one is BDE in decimal representation. So like the 246th one Let's assume the 246th one is BDE The decimal representation of k minus one the ones place corresponding to the letter and the tens place corresponding the second letter and the Tents place corresponding to the I plus one letter is going to be a comparison of a B up to J corresponding to Zero one two up to nine Right. So like if the each letter of the ones place tens place Hundreds place corresponds to a certain number Representation, okay zero to nine, right? So similarly when there's 26 letters of alphabet used in the notation of 20 base 26 the 26th place corresponding to the I plus I plus one Last is the last letter Right, and you could find each digit by the following procedure. So you could what you could do is you could initialize a variable Basically could initialize a variable with a k minus one repeat it until it becomes zero and You get the remainder of each one by dividing by 26 repeatedly right, so how this would work is that let's say you have a you're gonna have a initial variable x and then you Initialize it with like k minus one k minus one, which is the k minus one represents the Yeah, okay. So k minus one represents. So k is the kth string kth value among the strings of length l Right, you're gonna initialize x with k minus one, which gives you the so Let's say I want a fourth place with length l equal to three right or the 246th place of this of less length is equal to three. Well, I'm gonna initialize this value to have 245 right instead of 246 I'm gonna have 245 because I want the 246 place I'm gonna repeat this until it becomes zero and Then I'm going to be how I get the remainder Quotient and the remainder when I divide it by 26. So if I get 245 divided by 226, this is gonna be like the quotient and Then to remainder is gonna be 245 mod by 26 Repeatedly like do this repeatedly The ith result of R of this computation is the ith last digit of k minus one Right, so the last if I do this 245 times right The ith result of R so like let's say the fifth result of R of 245 mod 26 Is going to be the fifth Last digit of k minus one. Okay, so I know this is like kind of crazy to explain so I'm gonna Show you guys the code of what I mean. Okay, so that's I'm gonna show you got that what I mean by that All right. All right guys, so I'm gonna explain the code while I'm doing the problem itself because I actually haven't done this problem yet Actually, but I did look at the editorial and then I think I understand what's going on. Okay, so first We're gonna initialize our string. It's gonna be empty. All right, and that's the string. We're gonna return Okay, now we're gonna read in the actual number of n Right because we have to represent the number n that we're reading and then we're gonna change that to the actual corresponding Letters, right? That's we have to do Okay, so now remember I told you about the 26 That you have to mod by 26 every time and then we're gonna subtract one every time because of the Place of the I digit I minus one is gonna be the I digit, right? I Representation so what are we gonna do? So every time we're gonna subtract one from n. Okay, and that's going to represent the the That's just representing We were subtract one from n because that's how much the length we're decreasing it by we're decreasing length by one every single time right and We have to represent that That that length be to be that digit that we're changing it into whatever character it is So we subtract n by one every single time in this wild bread So that changes like the first digit and then it's 10th place and then 10th place 100th place thousand place place stuff like that All right, so we subtract one to get the corresponding value for each Single place right for 10th hundred thousand stuff like that. All right, so now what are we gonna do? We have to divide n By 26 Right because we're just we're dividing to n by 26 because that's that's Based on the equation that we represented it's n minus 26 plus whatever to l minus one So we have to divide n by 26 that mean number of times. Okay, because that's how you we're gonna represent Each 26 by that number of letters, right? There's 26 letters ABCD EFC up to Z We're gonna divide it by 26 that mean number of times and then we subtract one from for representing each place, right? 10th place one's place 10th place hundred place stuff like that. Okay? So now we did we got this out of the way. What do we have to do? So now now we have to add our string. Whoops my bad We have to add our string s and We're gonna add it by n mod by 26 So n mod by 26 is the remainder when you mod by 26 So that's the number that we're representing based on the kth place of The beginning of a so then once we have this number. That's how far it is from a Eight of our form to a up to Z, right? We have to add this to a Okay, and a is just the letter of the character a Okay, so that's what we do here. Um, yeah, that's pretty much it That's all I have to do and then now for every single place You add S plus equal to a and then then you go to the 10th place hundreds place stuff like that If you subtract this and then we divide by 26 again yada yada yada, then all we have to do is see out s Okay So now let's just see what this see if this works So 703 should correspond this a a a a so 703 a a a a yep, that's right now. Let's look at another one 705 c a a. Um, is that right? I'm so sure if that's right 705 should be a a a c actually hold up Let's see 704. Maybe I'm something. Yeah, okay, something's not right. Um, oh, okay, we have to do this Because plus equal is weird in C plus plus Why can't I do that? string s plus equal Yeah, compared to character Let's see 704 a b. Yeah. Yeah, so we have to convert this to a character And then we have to add s because plus equals actually adds it to the beginning, right? We have to add it to the end of append it to the end. So yeah, that's we have to do that Now let's just save it and then now submit This is one cortillian one Dominations whatever it is Okay, and it got a seed. So yeah, that's how you do this problem. I hope you guys understand What's going on? Basically, we're just representing every single Place and dividing by 26 and then modding it by 26 to get the remainder of how far it is from the letter a Because a to z and then we just append that to the end of all our characters That's what we're doing. All right. So yeah, Ray copy scribe. I'll check you guys later. Peace