This task is a task about euler's function, you only need to know the Euler function to solve it. If you need information about Euler's function go here. Also, don't precalculate the values and store in array , count the answer after you read teh number so that you can avoid TLE.
This is the blog of site xoptutorials.com. All the news will be posted here and on the facebook page of xoptutorials.
Friday, January 31, 2014
Thursday, January 30, 2014
SPOJ 3923. Philosophers Stone
Here is the problem statement .
We will solve this problem with dynamic approach mening that we will get the solution for current cell (X,Y) with the help of (X-1,Y) (X-1,Y-1),(X-1,Y+1). We will call teh given array A. We will also keep another array B where B[i][j] will show that maximum number of stones we can collect . INitially every element of the first row of the array B equals to every element of the first row of array A, the others have a value of 0. Now we need to loop over array B and for every coordinate (X,Y) we need to see if we can update B[X+1][Y] , B[X+1][Y-1] or B[X+1][Y+1]. Then we need to find the maximum of the last row of the array B and print it out.
We will solve this problem with dynamic approach mening that we will get the solution for current cell (X,Y) with the help of (X-1,Y) (X-1,Y-1),(X-1,Y+1). We will call teh given array A. We will also keep another array B where B[i][j] will show that maximum number of stones we can collect . INitially every element of the first row of the array B equals to every element of the first row of array A, the others have a value of 0. Now we need to loop over array B and for every coordinate (X,Y) we need to see if we can update B[X+1][Y] , B[X+1][Y-1] or B[X+1][Y+1]. Then we need to find the maximum of the last row of the array B and print it out.
DOWNLOAD THE FULL SOURCE CODE
Wednesday, January 29, 2014
SPOJ 9948. Will it ever stop
Here is the problem statement.
In this problem we need to notice that it will stop if at some point n will be 2 and with teh first if statement it will turn 1 and the cycle will break. If N has divisors other than the powers of 2 => that the cycle is unbreakable. So we only need to check if the given nurmber is a power of 2 or not.
In this problem we need to notice that it will stop if at some point n will be 2 and with teh first if statement it will turn 1 and the cycle will break. If N has divisors other than the powers of 2 => that the cycle is unbreakable. So we only need to check if the given nurmber is a power of 2 or not.
DOWNLOAD THE FULL SOURCE CODE
SPOJ 10798. Wachovia Bank
Here is the problem statement.
This is another classical dynamic programming problem called Knapsack Problem. If you need information about knapsack problem with 2 parameters go here
This is another classical dynamic programming problem called Knapsack Problem. If you need information about knapsack problem with 2 parameters go here
DOWNLOAD THE FULL SOURCE CODE
SPOJ 1724. Counting Triangles
Here is the problem statement
This problem is one type of a problem which I can't explain. You just need to take pen and paper , draw a few examples and the solution will pump up on your brain. SO If you tried many times and very tired of it I can give you the formula.
Here is the formula
A[N]= ( n*(2*n+1)*(n+2) )/8
This problem is one type of a problem which I can't explain. You just need to take pen and paper , draw a few examples and the solution will pump up on your brain. SO If you tried many times and very tired of it I can give you the formula.
Here is the formula
A[N]= ( n*(2*n+1)*(n+2) )/8
DOWNLOAD THE FULL SOURCE CODE
SPOJ 1. 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.
SPOJ 11573. A Famous ICPC Team
Here is the problem statement.
Just because the heights are the same, this task is a very simple one. Those are out numbers A1,A2,A3,A4. We need to sort them in increasing order, suppose we got B1,B2,B3, B4. Now if we take the needed side of a value B3+B4 we can absolutely be sure that all of them wu\ill be fit without overlapping.
Just because the heights are the same, this task is a very simple one. Those are out numbers A1,A2,A3,A4. We need to sort them in increasing order, suppose we got B1,B2,B3, B4. Now if we take the needed side of a value B3+B4 we can absolutely be sure that all of them wu\ill be fit without overlapping.
DOWNLOAD THE FULL SOURCE CODE
SPOJ 3375. Stamps
Here is the problem statement
Obviously for bothering as less friends as possible we need to start taking from friends which offer the most so that we can reach our required amount as fast as possible.
Obviously for bothering as less friends as possible we need to start taking from friends which offer the most so that we can reach our required amount as fast as possible.
DOWNLOAD THE FULL SOURCE CODE
SPOJ 3410. Feynman
Here is the problem statement
This problem needs noticing. We need to notice the following connection between the I and I+1 the papers.
For N=1 the answer is 1
for N=2 the answer is 5 , the 4 little squares and the big one.
We need to notice that the answer for N=3 is as follows 3*3 (the number of little squares) + A[N-2] (the number of the rest squares which are formed with more than 1 little square equals to the number of totalt squares of the previous answer).
In other words
A[I]=I*I+A[I-1]
This problem needs noticing. We need to notice the following connection between the I and I+1 the papers.
For N=1 the answer is 1
for N=2 the answer is 5 , the 4 little squares and the big one.
We need to notice that the answer for N=3 is as follows 3*3 (the number of little squares) + A[N-2] (the number of the rest squares which are formed with more than 1 little square equals to the number of totalt squares of the previous answer).
In other words
A[I]=I*I+A[I-1]
DOWNLOAD THE FULL SOURCE CODE
SPOJ 39. Piggy-Bank
Here is the problem statement.
This is another classic problem of dynamic programming called Knapsack problem with 2 parameters. Read about knapsack probelm with 2 parameters here.
This is another classic problem of dynamic programming called Knapsack problem with 2 parameters. Read about knapsack probelm with 2 parameters here.
DOWNLOAD THE FULL SOURCE CODE
Subscribe to:
Posts (Atom)