This is the blog of site xoptutorials.com. All the news will be posted here and on the facebook page of xoptutorials.
Thursday, August 28, 2014
COJ 2091. Counting Task
There are many ways you can solve it. You can can take every element get it's code and mark it in a boolean array or you can simply use C++ map to mark the characters directly.
FULL SOURCE CODE
Wednesday, August 27, 2014
COJ 1324. Snow White
There are only 9 number so the easiest approach is taking every single possibility and check. There are different ways but the easiest method is using 7 for loops.
FULL SOURCE CODE
Tuesday, August 26, 2014
COJ 1237. Mean Median Problem
There are only 3 cases possible
1. (A+B+C)/3=A
2. (A+B+C)/3=B
3. (A+B+C)/3=C
from each we can get
1. C=2*A-B
2. C=2*B-A
3. C=(A+B)/2
You can make a function which checks the answer for 3 numbers and run it 3 times on all of the cases above and pick the minimum.
1. (A+B+C)/3=A
2. (A+B+C)/3=B
3. (A+B+C)/3=C
from each we can get
1. C=2*A-B
2. C=2*B-A
3. C=(A+B)/2
You can make a function which checks the answer for 3 numbers and run it 3 times on all of the cases above and pick the minimum.
FULL SOURCE CODE
Monday, August 25, 2014
COJ 1219. Bounty Hunter
This task can be a little confusing because of the second parameter which is the distance from home. You may easily ignore it, there is nothing specified in task which wants us to use it, and more to that it says that who cares if we walk much, the important thing is to get paid as much as possible SO we only need to sort the array and start taking deals from the most expensive until our bullets run out.
FULL SOURCE CODE
COJ 1273. Domino Factory
The most important thing is to correctly calculate the number of domino figures. So how do we do it?
Suppose we have numbers from 0 to N, how many distinct pairs we can make?
0-1,0-2.0-3.......,0-N
which gives N+1 pairs
so now let's consider all the new dominoes with 1
1-1,1-2,1-3,...1-N
total of N pairs
than for 2 it is
2-2,2-3,2-4,....2-N
N-1 pairs
and so on until the last one which is 0 .
So the total number of pairs of number from 0 to N is the sum of numbers from 1 to N+1 which has a good formula ( (N+1)*(N+2) )/2. The answer will be the sum of number from 1 to N+1 multiplied with the 2 sides of domino.
Suppose we have numbers from 0 to N, how many distinct pairs we can make?
0-1,0-2.0-3.......,0-N
which gives N+1 pairs
so now let's consider all the new dominoes with 1
1-1,1-2,1-3,...1-N
total of N pairs
than for 2 it is
2-2,2-3,2-4,....2-N
N-1 pairs
and so on until the last one which is 0 .
So the total number of pairs of number from 0 to N is the sum of numbers from 1 to N+1 which has a good formula ( (N+1)*(N+2) )/2. The answer will be the sum of number from 1 to N+1 multiplied with the 2 sides of domino.
FULL SOURCE CODE
COJ 1101. Binaries Palindromes
The constraints are a little high. It would be better if the answers have been calculated before reading data. Check whether the number from 1 to 200000 are binary palindromes and keep the answers/ And then input data and output answer.
FULL SOURCE CODE
COJ 2205. Counting Ones
The constraints are very low, up to 1000 so we can do it with simple implementation, take every number and divide it to 2 and every time add the answer and the output the final result.
FULL SOURCE CODE
Sunday, August 24, 2014
COJ 1573. Just Another Easy Problem
The task consists of 2 parts, conversion and check. First of all conversion. If we have a hex number and want to convert it to decimal we have to do the following. We need to multiply every digit with the power of it's index base 16 counting from the right to left. For example we have a number ABCD , after converting to decimal it will be 13*(16^0) + 12* (16^1) + 11*(16^2) + 10* (16^3). Now let's check for the divisibility. The sum of numbers form 1 to N is N*(N+1) /2 and it is easy to notice that when N is odd then the result is divisible otherwise not.
FULL SOURCE CODE
COJ 1839. A Funny Task
This task may sound a little weird, because even if we have even number of oranges, after passing the first guard we will be down to odd number and we might have a little confusion when giving the half and 3 more, but you just skip this and write the answer as it should be , (((N+3)*2+3)*2+3)*2 .
FULL SOURCE CODE
COJ 1842. Distance of Manhattan
The statement of the problem already gives everything, you only need to input 4 numbers and output the sum of absolute differences of the first third and second forth numbers.
FULL SOURCE CODE
COJ 1118. The Drunk Jailer
Due to it's small constraints we can easily implement this algorithm. N is up to 100 which makes it very easy to solve using brute force.
FULL SOURCE CODE
COJ 1445. What's Next?
The task itself is a very easy task. A very simple implementation just check if the difference between the first two equals to the difference between the second two elements. There is a little part to take care of. when one element equals to 0 we might end up dividing on 0 when we are checking if the progression is geometrical. Use this pseudocode
if(a2-a1==a3-a2)
print "arithemitc"
else
print "gemoetric"
Don't check for geometric progression
FULL SOURCE CODE
COJ 1388. Last Digit of A ^ B
We can only have a look at the last digit of each base by ignoring the rest of the number. If we have a closer look at the last digits of powers from bases 0 to 9 we will notice a good pattern. So below is the list of last digits of powers of numbers from 1 to 9.
1- 1 1 1 1 1
2- 2 4 8 6 2
3- 3 9 7 1 3
4 -4 6 4 6 4
5- 5 5 5 5 5
6- 6 6 6 6 6
7- 7 9 3 1 7
8- 8 4 2 6 8
9- 9 1 9 1 9
One thing we can notice which is true for all numbers is that after every 4th power the last digits are repeating themselves . This means that the answer for the power of 3 and the power of and the power of 11 are the same. So we only need to consider the powers from 1 to 4 and then when outputing the answer we need to only take the remainder of the given power and check for the according last digit. Also don't forget the power=0 case.
1- 1 1 1 1 1
2- 2 4 8 6 2
3- 3 9 7 1 3
4 -4 6 4 6 4
5- 5 5 5 5 5
6- 6 6 6 6 6
7- 7 9 3 1 7
8- 8 4 2 6 8
9- 9 1 9 1 9
One thing we can notice which is true for all numbers is that after every 4th power the last digits are repeating themselves . This means that the answer for the power of 3 and the power of and the power of 11 are the same. So we only need to consider the powers from 1 to 4 and then when outputing the answer we need to only take the remainder of the given power and check for the according last digit. Also don't forget the power=0 case.
FULL SOURCE CODE
Friday, August 22, 2014
COJ 1025. Democracy in Danger
It is easy to understand that we need to choose the groups which have the smallest amount of people so that the half of it would be small and the total number of voters would be as small as possible. So we need to sort our given data and choose the first N/2+1 groups.
FULL SOURCE CODE
COJ 1079. Sums in a Triangle I
There is not much to say about this problem. The idea is simple , we just keep an array of answers , the we loop over it and update the current element with the best answer ( it would be max( a[i-1][j-1]+a[i][j],a[i-1][j]+a[i][j]) ) The drill ios the source limit. You need to preserver as much as possible . Avoid using many enters, spaces. Don't keep variables which you are not going to use. Make sure to check my source code. Its only 2 lines :D The first is #include <iostream> and the second on is full of operations. Its a long line :)
FULL SOURCE CODE
COJ 1506. Exam Grader
Another implementation. Just loop over the correct string and every other string and calculate the answer accordingly.
FULL SOURCE CODE
COJ 2196 - Even? Odd?
If the last digit of a number is divisible by 2 it means that the entire number is also divisible by 2. The rest is easy, input it as a string, take the last char and check.
FULL SOURCE CODE
COJ 1212. Jingle Composing
The task is easy , it needs a good implementation. For making things a little less complicated I recommend writing a separate function which gets the value of a char.
FULL SOURCE CODE
Thursday, August 21, 2014
COJ 1132. Divisor Summation
The number of test cases i really large so we can't process every test individually , it will just take a lot of time. So, instead we can precalculate all the answers from 1 to 500000. here is how we should do it, it will look like a sieve of Eratostenes, we will start looping from 2 to 500000 and every time. Let us have an array A where A[I] shows the answer for I. On every step of our loop we take the current index and add it to every member of A which is divisible to I. Such as, for 2 we take 2 and add 2 to numbers , 4,6,8,10,....500000. Then we do the same for 3,4,...500000. This will not take too much time and will give us all 500000 answers we seek. Then we just need to input the queries and output the answer directly.
FULL SOURCE CODE
SPOJ 74. Divisor Summation
The number of test cases i really large so we can't process every test individually , it will just take a lot of time. So, instead we can precalculate all the answers from 1 to 500000. here is how we should do it, it will look like a sieve of Eratostenes, we will start looping from 2 to 500000 and every time. Let us have an array A where A[I] shows the answer for I. On every step of our loop we take the current index and add it to every member of A which is divisible to I. Such as, for 2 we take 2 and add 2 to numbers , 4,6,8,10,....500000. Then we do the same for 3,4,...500000. This will not take too much time and will give us all 500000 answers we seek. Then we just need to input the queries and output the answer directly.
FULL SOURCE CODE
COJ 1022. Train Swapping
If we look deeper we will understand that no matter how we do the swaps the answer is not going to be different ( of course if we do swaps only when one is bigger than the one standing next to him). The rest is easy, we need to loop over and swap if there is a need until there will be no more swappings left.
FULL SOURCE CODE
COJ 1683. DPA
The constraints are very low (N<=500) which means that the simplest brute force solution will pass 100%. Just loop over all the numbers uptp N-1 and check for divisibility and add to sum, then compare the sum with N and output the answer accordingly.
FULL SOURCE CODE
COJ 1326. Account Balance
Again an easy implementation, just read the numbers and subtract or add according to the character given with the number. The output the answer.
FULL SOURCE CODE
COJ 1252. The Seven Percent Solution
Another implementation task. I recommend avoid using insertions or other complicated stuff, just loop over the string and see if the current char is from a given list or no. If it is than print the corresponding code otherwise output itself.
FULL SOURCE CODE
COJ 1077. The 3n + 1 Problem
First of all, there is no specific pattern for numbers so we need to calculate all of them. First of all we better calculate all the numbers and save them in an array. We need to do it extremely efficiently because 10^6 is large and we might easily end up getting TLE. So first of all the A[I] shows the answer for the number I. A[1] is 1 , then we need to continue, looping from 2 to 10^6 we need to calculate answer for every single answer, but we can be a little clever. Here is how, suppose we need to calculate for a specific number K. now we calculate all the numbers on the way, suppose we got Kn,Kn-1,.....1 so there are n numbers, So the answer for K is n. We calculated it successfully but there were numbers which we just threw away, the ones we got after K before reaching 1, isn't it true that the number after K has an answer of n-1 and the number after him has the answer of n-2.... So instead we can keep all the numbers we seen so far, and then when we reached 1 we can loop back and give each number on the way it's appropriate value which will save us a lot of time. Then the rest is just inputing and outputing the answer. NOTE on big note which caused me a lot of WA, it is not specified that in the input i is smaller or equal to j, this is important thing to consider. Also check out the source code for a better understanding,
FULL SOURCE CODE
Wednesday, August 20, 2014
COJ 1157. u Calculate e
This task is easy by itself, it is just a simple calculation but as you can see it has a low accepted percent which depends on some reasons. I got wrong answer many times only because of the output format.First of all you it is a little confusing but you really need to output all the garbage before actually outputing the numbers. The first two lines, which are "n e" and "- -----------". Those two need to be printed, then you need to keep the format. The first tree numbers you better output individually then you need to keep the precision , exactly 9 digits after the dot., nor more and not less.
FULL SOURCE CODE
COJ 1024. Hangover
Here is the problem statement
http://www.spoj.com/problems/HANGOVER/
Just because the constraints are 5.2>N>0.02 this task can be solved with a simple implementation. Just loop from 2 and add to our current legth the 1/i (i is our current loop value)/ If we reached the value print the index and exit.
http://www.spoj.com/problems/HANGOVER/
Just because the constraints are 5.2>N>0.02 this task can be solved with a simple implementation. Just loop from 2 and add to our current legth the 1/i (i is our current loop value)/ If we reached the value print the index and exit.
DOWNLOAD THE FULL SOURCE CODE
COJ 1069. Soda Surpler
Constraints are small so we can do it with the most obvious way. First of all, add the first two numbers, now we need to check how many sodas we can buy, and after that, when we buy them how many empty total bottles we are having left after buying and drinking the current sodas. Do this until we are unable to buy any more. Also check the case of infinite , there are 2 cases, either soda costs 0, or soda costs 1 but we have at least 1 bottle.
FULL SOURCE CODE
COJ 1493. Geometrical Task II
Here are formulas for the necessary areas
Triangle (a*h)/2 a is the side and h is the height taken to that side
Rhombus (d1*d2)/2 d1 and d2 are diagonals of rhombus
circle pi*r*r where pi is 3.14 and r is the radius.
If you are having trouble outputing with correct precision have a look at this.
Triangle (a*h)/2 a is the side and h is the height taken to that side
Rhombus (d1*d2)/2 d1 and d2 are diagonals of rhombus
circle pi*r*r where pi is 3.14 and r is the radius.
If you are having trouble outputing with correct precision have a look at this.
FULL SOURCE CODE
Output double/float numbers with precision
Sometimes we require to output numbers rounded to some exact precision. There are a few ways of doing them. If you are using scanf/printf you can do it in this way. Suppose we need to output to 7th precision,
double a;
printf("%.7f",a);
This means output a rounded to 7th precision.
If you need to use cin and cout here is how you should do it. Include iomanip ( #include <iomanip> )
then before couting write this
cout<< fixed << setprecision (7);
cout<< a<< endl;
double a;
printf("%.7f",a);
This means output a rounded to 7th precision.
If you need to use cin and cout here is how you should do it. Include iomanip ( #include <iomanip> )
then before couting write this
cout<< fixed << setprecision (7);
cout<< a<< endl;
COJ 1043. Simple Dishes
Here is a very simple but good approach, we loop from the right to left over our dishes and whenever we see one we can take we sure take it and decrease the mass bye the dish we just took which will eventually work out the answer itself.
FULL SOURCE CODE
COJ 1177. Group Reverse
The task is a simple implementation task. You can input and output directly with carefully using loops or you cna modify the string and then output it. Have a look at source code.
FULL SOURCE CODE
COJ 1176. Ternary
We need to keep a sequence and every time add the element N%3 to that sequence and divide N to 3 until it becomes 0. And then output the reversed sequence.
FULL SOURCE CODE
COJ 1070. A Simple Calculation
For explaining this task we need to have a look at an example.
On the picture above you can see the example of N=3. So, as you can see I gave coordinates to every single edge. Now, we will solve 2 different tasks, the number of squares and the number of rectangles. First of all let's try to calculate the number of rectangles. We will form rectangles with 2 coordinates which we will choose, one of them will be the upper left point and the second one the lower right point. So the it's easy to understand that the first coordinate (upper left coordinate) can be only in rancge ( [1,3] ,[1,3] ), so at first let's consider the first coordinate as point (1,1) how many rectangle we can form with the upper left coordinate (1,1) the lower right coordinate can be (2,2) (2,3) (2,4) (3,2) (3,3) (3,4) (4,2) (4,3) (4,4) in other words we can take the coordinate (2,2) and we can see how long can we expand it both vertically and horizontally which leaves 3x3 toatal of 9 possibilities where the upper left coordinate is (1,1). Now let's check for (1,2) the same way, we can take coordinate (2,3) and see how much we can expand it, we can expand 3 vertically and 2 horizontally which gets us total of 2x3 6 possiblities. I think we can understand now that we will have this result, 3x3,3x2,3x1, 2x3, 2x2, 2x1, 1x3,1x2,1x1 those are results where the upper left coordinate is respectively on the first second and the third line. SO, the final answer is all the multiplication pairs of numbers from 1 to N. .
Now about the squares, we can figure out the number of squares with the same way. If we consider the upper left coordinate as (1,1) then how many squares we can form? 3 totally Then if we consider all the acceptable coordinates of the first line as the upper left coordinate we will get the following result , 3,2,1.
Then on the second line it's 2,2,1. Then it's 1,1,1. As you can see the second line's answer is smaller than the previous one by 1. And the third is smaller than the second by 2.
On the picture above you can see the example of N=3. So, as you can see I gave coordinates to every single edge. Now, we will solve 2 different tasks, the number of squares and the number of rectangles. First of all let's try to calculate the number of rectangles. We will form rectangles with 2 coordinates which we will choose, one of them will be the upper left point and the second one the lower right point. So the it's easy to understand that the first coordinate (upper left coordinate) can be only in rancge ( [1,3] ,[1,3] ), so at first let's consider the first coordinate as point (1,1) how many rectangle we can form with the upper left coordinate (1,1) the lower right coordinate can be (2,2) (2,3) (2,4) (3,2) (3,3) (3,4) (4,2) (4,3) (4,4) in other words we can take the coordinate (2,2) and we can see how long can we expand it both vertically and horizontally which leaves 3x3 toatal of 9 possibilities where the upper left coordinate is (1,1). Now let's check for (1,2) the same way, we can take coordinate (2,3) and see how much we can expand it, we can expand 3 vertically and 2 horizontally which gets us total of 2x3 6 possiblities. I think we can understand now that we will have this result, 3x3,3x2,3x1, 2x3, 2x2, 2x1, 1x3,1x2,1x1 those are results where the upper left coordinate is respectively on the first second and the third line. SO, the final answer is all the multiplication pairs of numbers from 1 to N. .
Now about the squares, we can figure out the number of squares with the same way. If we consider the upper left coordinate as (1,1) then how many squares we can form? 3 totally Then if we consider all the acceptable coordinates of the first line as the upper left coordinate we will get the following result , 3,2,1.
Then on the second line it's 2,2,1. Then it's 1,1,1. As you can see the second line's answer is smaller than the previous one by 1. And the third is smaller than the second by 2.
FULL SOURCE CODE
Tuesday, August 19, 2014
COJ 1441. Egypt
we only need to sort them properly and check 2 smallest with the biggest one and output the answer.
FULL SOURCE CODE
COJ 1244. Flowers Flourish from France
The task itself is not hard, but we need to carefully handle the implementation. Here is hat I suggest, take the first letter and keep it in a variable. Let's call it K. K is the symbol which we need to check every time. and also turn it into upper case, if it is already uppercase it will not change, we do it so that we can avoid the problem of letters being the same but the cases being different. Then loop over the given string , whenever we encountered a space that means that the next letter is a first letter of a word, we take that letter, turn it into uppercase and check with variable K. If they are the same we keep going, but now the value of K becomes the newly found letter uppercased. If at some point a letter which was after ' ' (the forst letter of a certain word) is not equal to current K than we abort and output 'N'.
FULL SOURCE CODE
COJ 1180 - Bijele
Just keep array of the correct set which is (1,1,2,2,2,8) and then subtract input from every element on this array accordingly and output the answer.
FULL SOURCE CODE
COJ 1046. Product Subsequence
You only need to count I*(I+1)*(I+2) and then count the sum of these in the specific range but be careful when handling modulo 100.
FULL SOURCE CODE
COJ 1297. Divisibility by 495
495=5*9*11 which means that if the number is divisible by 5 and 9 and 11 at the same time then the number is divisible on 495. The constraints are big which means we need to input the number as a string. So the divisibility rules
5: if the number's last digit is either 5 or 0.
9: If the number's sum of digits is divisible on 9.
11. If the difference of sums on the even and odd indexed digits is divisible on 11.
5: if the number's last digit is either 5 or 0.
9: If the number's sum of digits is divisible on 9.
11. If the difference of sums on the even and odd indexed digits is divisible on 11.
FULL SOURCE CODE
COJ 1300. Modulo
We can keep a boolean array A where A[I] shows whether we already saw the number I or not. then we can just input and every time check whether the current number has been seen or not.
FULL SOURCE CODE
COJ 1293. Powers of Two
It is easy to understand that the answer will not fit into long long integer. So we need to deal with them considering them digit by digit -> we need to keep an array where every element will be only 1 digit of the entire number. Initially it will represent 2^0 which is 1. Then we need to loop over it N times and do the multiply by 2 part carefully Check the source code for a good understanding
FULL SOURCE CODE
COJ 1566. Cannon Balls
After having a look at a few layers you will figure out how the pyramid is built.
The first layer is only one ball. The second layer needs to have 4 balls so that the one can be on the second layer's top. The red dots show the spots were the previous layer is laid. As you can see every layer needs to be a square, and the side is the index of it's own layer.
FULL SOURCE CODE
COJ 1064. Alarm Clock
You can just subtract 2 times in a clever way but I suggest a easier approach. We can take the first time and keep adding 1 minute to the first time's minute and count how many we added before we reach the second time. Also we need to keep in mind that when minute reaches 60 it becomes 0 and hour increases by 1 and if hour reaches 24 then hour turn into 0.
FULL SOURCE CODE
COJ 1484. Hotest Mountain
This is a simple problem of calculating maximum. This is how you do it, you take a variable max and give it a value of the first element assuming that it is the maximum. Then you loop over array and check if there is one grater than the current maximum, if there is , then you update the value of MAX and also keep track of the position.
FULL SOURCE CODE
COJ 1485. Increasing Order Word
This is again a little sorting problem. You can go with a little longer code and implement bubble sort or you can use C++ sort but be careful when doing it because you can't use sort with strings, you can use with char* only.
FULL SOURCE CODE
COJ 1050. Coprimes
There are different ways of doing it. Here is my suggestion. We loop over all the numbers from 1 to N and check if their GCD (greatest common divisor) is 1 or not. For making it on the time limits, we need to write the greatest common divisor function efficiently. The Euclid's algorithm suggest a way for finding GCD in logN time. For more info about GCD go here.
FULL SOURCE CODE
COJ 1318. Abc
Just sort the 3 numbers and they will be in order A,B,C, then output the answer accordingly.
FULL SOURCE CODE
COJ 1166. Speed
This task is a little bit unique, it gives us only input and output and we need to figure out the format, and the statement of the task. By carefully observing input we can understand that the first number is the number of test cases. Then there are test cases , the first number of each test case is the number of elements and then there are elements themselves. And we should find the number of even and odd numbers in that sequence and print how shown in the output.
FULL SOURCE CODE
COJ 1188. Cow Multiplication
Nothing special in algorithming, you only need to get a number and separate the digits for multiplying them, it can be done easier if you input both numbers as strings which will help easier to loop over it's digits.
FULL SOURCE CODE
COJ 1288. Div 6
If the number is divisible both on 3 and on 2 than the number is divisible on 6. The rule of divisibility to 3 is: if the sum of digits of the number is divisible on 3 then the number itself is divisible on 3. The rule of divisibility of 2 is the following: if the last digit of the number is divisible on 2 than the number is divisible by 2. The input is a large number so we need to input it as a string and then check the sum of it's digits and the last digit.
FULL SOURCE CODE
COJ 1483. Geometrical Task I
Simple, just input the first word as a string and check, if it says "square" then the answer is A*A otherwise the answer is A*B.
FULL SOURCE CODE
COJ 1306. Div 4
The rule of divisibility to 4 is the following , if the number which is formed by the 2 last digits of the given number is divisible to 4 then the number itself is divisible to 4. The input is huge so we need to input it as a string, take the last 2 digit and check for divisibility of that 2 digit number, if it is divisible then the given number is divisible as well.
FULL SOURCE CODE
COJ 1495. Increasing Order List
The simple bubble sort will cover it because of the low constraints but if you are using C++ you can use the function sort in the library 'algorithm'.
FULL SOURCE CODE
Monday, August 18, 2014
COJ 1102. You Can Say 11
For solving this we need to know only the rule of divisibility by 11. The rule is, sum up the digits in the odd places and digits in the even places and then subtract smaller form bigger, and if the result is divisible on 11 then the number itself is divisible on 11.
FULL SOURCE CODE
COJ 1099. Pythagorean Numbers
we only need to sort them properly and check 2 smallest with the biggest one and output the answer.
FULL SOURCE CODE
COJ 1159. A Very Easy Problem!
Here is the problem statement
http://www.spoj.com/problems/EASYPROB/
I am posting tutorials about spoj tasks which will give hints and help the reader to figure out himself vut on this task I can't give anything but the correct answer, If you already triedmany times and you want to know the answer then keep on reading.
The 2(...) means the power of 2. This is all you need to know. The rest is up to you
http://www.spoj.com/problems/EASYPROB/
I am posting tutorials about spoj tasks which will give hints and help the reader to figure out himself vut on this task I can't give anything but the correct answer, If you already triedmany times and you want to know the answer then keep on reading.
The 2(...) means the power of 2. This is all you need to know. The rest is up to you
There is no source code this time :)
COJ 1160. Number Steps
Here is the problem statement
http://www.spoj.com/problems/NSTEPS/
This task is full of stuff that need to be noticed. Suppose the give coordinates are X amd Y. First of all notice that the the number exists only when x==y or x==y+2. Then you need to check for one more thing. Notice that if x is an even number the answer is x+y else its x+y-1
http://www.spoj.com/problems/NSTEPS/
This task is full of stuff that need to be noticed. Suppose the give coordinates are X amd Y. First of all notice that the the number exists only when x==y or x==y+2. Then you need to check for one more thing. Notice that if x is an even number the answer is x+y else its x+y-1
DOWNLOAD THE FULL SOURCE CODE
COJ 1156. Life, the Universe, and Everything
Here is the problem statement
There is nothing to say about this problem, you only need to now the basics of your favourite programming language to solve it. Here is my solution written in Pascal.
There is nothing to say about this problem, you only need to now the basics of your favourite programming language to solve it. Here is my solution written in Pascal.
COJ 1051. Div 3
let's have a closer look at our sequence,
1,12,123,1234,12345,123456
So, every time the previous number is taken and the current index is being attached to the end of that number. The 3 divisibility rule is the following : if the sum of digits of a number is divisible by 3 then the number itself is divisible by 3. So, let's notice something. The first number is not divisible by 3 , and if divided by 3 it gives 1 remainder. Then the second index (which is number 2) which is going to be attached to the previous number is not divisible by 3 either but it gives remainder 2 when divided. So when this new index is attached to the previous number we get a number divisible by 3. And the third index is divisible to 3 so the third number will be divisible as well. So, when it comes to 4th, it again gives remainder 1 when divided on 3 so when we attach this to the previous number which was already divisible by 3 we will get a number which gives 1 remainder. Then again, in the same way the 5th (which has 2 remainder when divided on 3) is being added the number is again divisible. Now if we numerate this sequence, only the all the numbers of 1+3*K form are not divisible on 3. The basic formula for this would be ANS=N- round_up(N/3) .
1,12,123,1234,12345,123456
So, every time the previous number is taken and the current index is being attached to the end of that number. The 3 divisibility rule is the following : if the sum of digits of a number is divisible by 3 then the number itself is divisible by 3. So, let's notice something. The first number is not divisible by 3 , and if divided by 3 it gives 1 remainder. Then the second index (which is number 2) which is going to be attached to the previous number is not divisible by 3 either but it gives remainder 2 when divided. So when this new index is attached to the previous number we get a number divisible by 3. And the third index is divisible to 3 so the third number will be divisible as well. So, when it comes to 4th, it again gives remainder 1 when divided on 3 so when we attach this to the previous number which was already divisible by 3 we will get a number which gives 1 remainder. Then again, in the same way the 5th (which has 2 remainder when divided on 3) is being added the number is again divisible. Now if we numerate this sequence, only the all the numbers of 1+3*K form are not divisible on 3. The basic formula for this would be ANS=N- round_up(N/3) .
FULL SOURCE CODE
COJ 1059. Numeric Parity
The task only requires us to change a decimal number into binary, count the number of 1s and output the result in a way. So, what do we need to do for converting decimal to binary? We need to keep a sequence, and every time add an element n%2 and divide n to 2 until n reaches 0. This can be done easily with vectors,, just push_back n%2 every time and divide n/2. The rest is easy. Also check out the source code.
FULL SOURCE CODE
COJ 1049. Sum
Just a simple sum problem with little constraints, but be careful when dealing with the negative numbers because the input can consist of negative number as well.
FULL SOURCE CODE
COJ 1028. All in All
At first , this task smells like dynamic programming but it is much easier than that. The thing is only checking if the first string can be obtained by removing some elements form the second one. If it is possible, then all the letters of the first string must exist on the second string on the same order as the exist on the first string. The only thing we need to do is too loop once over the second string and by starting from the first letter of the first string check and find every element in the exact order. If we reach the end of the loop but our checker didn't reach the final letter of the first string it means that the answer is "No" , otherwise it is "Yes". Check the source code for a better understanding
FULL SOURCE CODE
COJ 1023. Financial Management
Nothing special about this task, just sum up the 12 numbers and divide into 12 to get the arithmetic mean.
FULL SOURCE CODE
Saturday, August 16, 2014
COJ 1011. Army Strength
This task is very simple. We need to understand one simple fact. No matter how many monsters there are on the field at each teams the winner team will be the team which has the strongest monster because even if the strongest monster is left alone, every step he will kill the opposing monsters until the battle is over. (If you need information about finding maximum/minimum in an array, go here.
FULL SOURCE CODE
COJ 1004. Traversing Grid
The constraints of this task give us a hint that there is no complicated algorithm which must be hardly implemented. After doing a few tests with pen on paper you might figure out a few things. First of all suppose we have NxM grid, so after doing one full cycle (on cycle means that we went to the end of the first row, then turned down, went to the bottom of the grid, then turned left went to the end and then turned upwards and went until we reached the coordinate (2,1) which means that we are again facing right which means that we have go all over again but on the grid of size (N-2,M-2). For a better understanding have a look at the picture below.
The red line represents the first cycle, after it we are again facing right and it is the same as passing 1x2 grid. So, the first thing is decreasing the grid as much as we can by cutting 2s out of it. How do we cut 2s? We take the minimum of 2 sides minus one, and if that number is even then we subtract it from both sides or we decrease it with 1 and then subtract it. After the cutting process the smaller side of grid will be left either 1 or 2. Now it is time only for a few conditions. In case of (1,1) the answer is R, in case of (1,2) it is R, in case of (2,1) it is D , in case of (2,2) it is 'U' , in case of (1,other) , it is R and in case of , (other ,1) is D and for (2,other) is 'L' and (other, 2) is 'U'. Also check out the source code .
The red line represents the first cycle, after it we are again facing right and it is the same as passing 1x2 grid. So, the first thing is decreasing the grid as much as we can by cutting 2s out of it. How do we cut 2s? We take the minimum of 2 sides minus one, and if that number is even then we subtract it from both sides or we decrease it with 1 and then subtract it. After the cutting process the smaller side of grid will be left either 1 or 2. Now it is time only for a few conditions. In case of (1,1) the answer is R, in case of (1,2) it is R, in case of (2,1) it is D , in case of (2,2) it is 'U' , in case of (1,other) , it is R and in case of , (other ,1) is D and for (2,other) is 'L' and (other, 2) is 'U'. Also check out the source code .
FULL SOURCE CODE
Friday, August 15, 2014
COJ 1003. General Election
This task is not hard, we only need to sum up the numbers correctly => sum up all the columns and find the maximum among the results.
DOWNLOAD THE FULL SOURCE CODE
COJ 1005. Rent your Airplane and make Money
2 thigs we will use in this task 1. Dynamic Programming 2. Binary search
First of all we sort our array by the start time.
Suppose this is our array
s1 e1 c1 (start, end, cost)
s2 e2 c2
...
...
sn en cn
Then we will keep our array for dynamic programming approach, we call it dp. As we all know, for dynamic programming we need to build our current solution based on the ones we got from our previous operations. We need to loop from back of the sorted array. When we are on I th element. We need to find the leftmost element which is on the right side of I ( index is grater than I) and the start time of that element is more than the end time of our current element( AKA we can take the Ith order and the order which we found with this step). That will be element with the index J. So we are sure that the orders from J-1 to I+1 can't be taken If we take teh order I. So does it worth it? That makes our dp[I]= max (dp[I-1], dp[J]+cost[I]) (AKA we either reject this order and take the profit we already collected ,or we reject all the orders from I+1 to J-1 to take the order I). Make sure to check the source code for a better understanding ,
DOWNLOAD THE FULL SOURCE CODE
COJ 1238. Factorial Again!
This task doesn't require any algorithmic planning , just a good implementation is enough. You can start looping from right to left, meanwhile calculating the factorial for the current position and taking the last digit of the number multiply and add the answer, and simply removing the last digit by dividing the number to 10. Check the source code for a better understanding
DOWNLOAD THE FULL SOURCE CODE
COJ 1002. New House
The brute force solution passes easily if implemented correctly. So, what do we need to do. First we loop over our array, if there is a '.' then we stop there and start the procedure. We need to form a square as big as possible. When we found a square (on the first step is only 1 '.') then we should try expanding the size by 1 and checking again whether the new square is full of only '.'s or not. For making our solution a little faster we can only check the new row and the column which we added to form a square of one size larger than the previous. For being more clear have a look at the pictures below.
The black cyrcles represent the are which we already found as a square and the red ones are the places where we need to check to see if we can expand our house by 1. As you can see there is only 1 row and 1 column to check, this makes the solution faster and it is important because in the task the maximum value of N is not stated. Check the source code for implementation.
DOWNLOAD THE FULL SOURCE CODE
COJ 1000. A+B Problem
This was the first problem of the most programmers, you know , the famous A+B Problem.
DOWNLOAD THE FULL SOURCE CODE
Monday, August 11, 2014
SPOJ 16033. Tip Top Game (TIPTOP)
http://www.spoj.com/problems/TIPTOP/
(A little misunderstanding regarding the task, the thing which we need to check is whether the number has even number of divisors or no).
Judging from the input (10^5 test cases and each texst casu up to 10^18) we can assume that this task is a type where we need to input the number and ouput the answer without much operations. For solving tasks like this I recommend writing a brute force code, output the answer for some number and then spot the pattern easily. You can download the output for the first 1000 numbers below and you might figure out the pattern.
(A little misunderstanding regarding the task, the thing which we need to check is whether the number has even number of divisors or no).
Judging from the input (10^5 test cases and each texst casu up to 10^18) we can assume that this task is a type where we need to input the number and ouput the answer without much operations. For solving tasks like this I recommend writing a brute force code, output the answer for some number and then spot the pattern easily. You can download the output for the first 1000 numbers below and you might figure out the pattern.
Download output for first 1000 numbers
Basically, the answer is yes when the number of divisors is odd. So when the number of divisors of a number is odd? By observing the output you will figure out that it is true when the number is a perfect square. So, let's discuss why is it true. In case of all prime numbers which can't be perfect squares , the number of divisors is even (only 2 divisors).
Let us have a list of out divisors of a certain prime numner P.
1 and P
So, when we multiply this number with any other number not equal to himself, we will get a new list of numbers. Supose we multiply with. K
we get, 1, K, P, P*k which is again even. It will turn odd only if there is a number in that list which we wrote 2 times which is K and P so we must multiply a prime number with itself to get a number with odd number of divisors =>only perfect squares have that feature.
DOWNLOAD THE FULL SOURCE CODE
Subscribe to:
Posts (Atom)