 what's up guys my name is michael and welcome to my youtube channel today we are going to go over another problem called same ro8f payment okay so basically the gist of the problem is just that you're given a value n and you need to figure out how many different squares are in the n by n squares okay how many different squares are there in a grid of n by n squares so in this case we have n is equal to two and then the answer is five and the reason why it's five is because five is the number of different squares in the grid two by two right n by n two by two so if we were to create a two by two grid like this and then count how many different squares there are we get an answer of five and the reason why it's five is because we have one square here on the left the second square on on the top right a third square on the bottom left fourth square on the bottom right and then the in total the total square the whole square is five right that's uh the total square so then that's why the whole answer of the number of different squares in n by n squares is five because now we have one two three four five five different squares in n by n is equal to five so now now we have to check uh if n is like three four five six seven eight nine ten whatever value it is we need to find the number of different squares there are for an n by n grid okay so how do you do this problem um easiest way is actually just to draw it out so for n is equal to two we have this left side and right side and then bottom left and then bottom right and then the total right and that's equal to five answer is five now let's check if n is equal to three so if n is equal to three we have three by three grid so it's like this let's see how many we have so we have one here so that's one we have another one here so that's two we have three and then four five i shouldn't write it like this okay so we have five here six seven eight nine so first we have nine squares write each separate of different separate squares but now we have to count the number of different ones so if i were to count this square let's let me change it to a different color let's see if i could change it to a different color yeah if i would count this square this individual square right here this total square so every four we see that it's a different square so this would be another square so this is equal to another one so this is equal to ten and then we have this one let's give another color this one this blue one is another square right every four is another square so this is another one so there's a b there's 11 then we have another square which this square is bottom left there we go color so then that's this one is 12 that means there's another one plus one is another 12 and then we have a final one uh bottom right let's make a green which is this this square that's the final different square okay so this is 13 my bad plus one is 13 right and then we have the total square which i'm going to actually label it gray here's the original square this total square everything this whole original square like the one the three by the whole three by three square that's another different square so we add another one and that's give us 14 okay so now if we were to redo this we would have we have if n is equal to two we have uh answers would be five right so now if n is equal to three the answer is 14 right from what we wrote so now let's look at what happens when n is equal to four okay um so if we were to draw a four by four matrix uh yeah right here okay so if we draw four by four um so for originally we would have 16 because there's 16 different individual squares in here right so this is four four by four um now let's actually count each individual ones like each every four four ones so we have this one so that'll be 17 um we have another this one this is going to be 18 this one it's going to be 19 this one it's going to be 20 so this is actually now 20 right um so yeah that was that but now let's count the other ones so there's other other ones we could do so we could have let's see we could have this one um this one we could have this one okay so we could have this square there's another square so that's 21 um we could have this one another square uh let's actually label this blue so this one there's another square so that's 22 and then we could have this one and this one is uh not that bad this one this square that's 23 and yeah let's I think that's all oh no we have one more on the bottom of this one 23 so this is 23 um and then we could have like the final one I think I missed one did I miss one no I have one here right here this one this one this square in the middle right and that's 24 now we have the whole final square let's label this green so we have this total square right here the gigantic one the original square so that's that's just that's this whole giant one right here and that's 25 so yeah we would have 25 different squares so if you were to actually read read look at the different test cases we have n is equal to 2 because it gives us 5 and equals to 3 gives us 14 and n equals 4 gives us 25 I think yeah so now let's try to figure out a pattern of how this happened um so if you were to actually try to mathematically do it continually you realize that this is actually just the sum of every single square from 1 to n so 5 is equal to 1 square plus 2 square right which gives us 5 3 14 is actually equal to 1 square plus 2 square plus 3 square right this would give us 1 plus 4 plus 9 which is give us 1 plus 13 which gives us 14 right so that's what this gives us and 25 is equal to 1 square plus 2 square plus 3 square plus 4 square and this is equal to actually we could just take the previous our value that we recalculated which is 14 plus 16 which gives us actually 30 okay so I actually miscounted yeah there's there's four more originally but yeah that's actually the the answer to this problem okay um the answer to this problem is actually yeah I miscounted damn but yeah this should be 30 uh there were more there were more squares there I didn't know notice okay um oh there's the the top left and then the bottom right and the other two ah dang I didn't count all them but yeah the answer is actually the summation of the squares of each value from 1 to n that's basically the solution so the answer is given n it will just be the summation of uh i equals to 1 to n and that's just i square right the summation of i one i square to 1 n and uh if you were to do mathematically summations is actually a formula this one n times n plus 1 times 2n plus 1 over 6 so yeah you could actually use this is actually going to equal to a formula for this so n times n plus 1 times 2n plus 6 over 2n plus 1 over 6 okay uh you probably could prove by induction if you really want to understand the computer science of this right you could do approved by induction but yeah that's basically and the answer the answer is just n times n plus 1 times 2n plus 1 over 6 so yeah let's just type up the code and then submit it so the code is basically read in the number of test cases t uh while t minus minus you're going to read every single test case n and then just print out n times n plus 1 times 2n plus 6 over 2 and now let's just submit this because i don't know if this actually does work hope it does see what this does i got a wrong answer what was the problem oh i typed it wrong 2n plus 1 over 6 oh wow this is a different this is okay yeah this is a weird input um they want us to keep inputting it until the last line is equal to zero so if if if okay okay this is very strange okay um so while t is not equal to zero this is super duper weird um so they want us to read to keep doing this until the line is not equal to zero so we're gonna have to do it this way okay so we read an n and then while if while n is not equal to zero then we print out the the value then we read an n again okay because they want us to continue until the the last line is not equal to zero right because if it's equal to zero then it's done for so yeah uh why is this not responding now what is going on oh okay okay yeah so i mean it didn't do anything but yeah okay let's try this now that's a very strange yeah now i got accepted okay yeah that was a very weird input statement um yeah i didn't expect that but yeah that's that's basically how you do this problem rate com subscribe i'll check you guys later peace