Pages

Monday, 19 January 2015

SPOJ PROBLEM 2:PRIME GENERATOR

Problem Link: http://www.spoj.com/problems/PRIME1/

SUMMARY

Given an interval [m,n] where 1\leq m\leq n\leq 10^{9} and n-m\leq 100000, find all prime numbers in that interval.

Logic:
No composite number less than or equal to n will have a factor greater than \lfloor {\sqrt  {n}}\rfloor so we only need to know all primes up to this limit, which is no greater than 31622 (square root of 109).

To view my solution:

https://github.com/shivam04/spoj/blob/master/PRIME1.cpp

Sunday, 18 January 2015

SPOJ PROBLEM 2727:ARMY STRENGTH

Problem Link: http://www.spoj.com/problems/ARMY/

The next MechaGodzilla invasion is on its way to Earth. And once again, Earth will be the battleground for an epic war.
MechaGodzilla's army consists of many nasty alien monsters, such as Space Godzilla, King Gidorah, and MechaGodzilla herself.
To stop them and defend Earth, Godzilla and her friends are preparing for the battle.


Each army consists of many different monsters. Each monster has a strength that can be described by a positive integer. (The larger the value, the stronger the monster.)
The war will consist of a series of battles. In each battle, the weakest of all the monsters that are still alive is killed.
If there are several weakest monsters, but all of them in the same army, one of them is killed at random. If both armies have at least one of the weakest monsters, a random weakest monster of MechaGodzilla's army is killed.
The war is over if in one of the armies all monsters are dead. The dead army lost, the other one won.
You are given the strengths of all the monsters. Find out who wins the war.


Logic:-
What I did is I took input of strengths in an array of required size and then found out the greatest integer in both the array and then compared it according the above criteria to score an AC



To view my solution:-