You are on page 1of 23

SPOJ Problem Set (tutorial)

450. Enormous Input Test


Problem code: INTEST

The purpose of this problem is to verify


whether the method you are using to read input data is sufficiently fast to handle problems branded
with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of
input data per second at runtime.

Input
The input begins with two positive integers n k (n, k<=10 7). The next n lines of input contain one
positive integer ti, not greater than 109, each.

Output
Write a single integer to output, denoting how many integers ti are divisible by k.

Example
Input:
7 3
1
51
966369
7
9
999996
11
Output:
4
Added by:
Date:
Time limit:
Source limit:
Memory limit:
Cluster:
Languages:
Resource:

Adrian Kosowski
2004-11-09
8s
50000B
256MB
Pyramid (Intel Pentium III 733 MHz)
All except: NODEJS PERL 6
Idea put forward by Michael Mendelsohn

SPOJ Problem Set (tutorial)

1118. Snowman
Problem code: SNOWMAN

Finally the time of the year has come where children can build snowmans. The children have collected
some snow and ask you how big the snowman is going to be.
Assume that the snowman will consist of three spheres stacked on top of each other. The lower two
spheres are of equal size, the smaller sphere (used for the head) will have a radius of 25 percent of the
radius of the larger spheres.

Input
The first line of the input contains a number t 100, which indicates the number of test cases to
follow. Each test case consists of a line with one integer a 500000, the amount of snow in cm3.

Output
For each test case, print a line with the height of the snowman in cm. Round this number down to the
nextsmaller integer.

Example
Input:
2
100
500000
Output:
10
175
Added by:
Date:
Time limit:
Source limit:
Memory limit:
Cluster:

Languages:

Adrian Kuegel
2006-11-28
5s
5000B
256MB
Pyramid (Intel Pentium III 733 MHz)

All except: ERL JS NODEJS PERL 6

SPOJ Problem Set (tutorial)

1392. Sum of Factors


Problem code: CZ_PROB2

Find the sum of the factors of a number including 1 and the given number.

Input
Number of test cases T followed by T lines of number n.
0 < T <= 5000
0 < n <= 999999999
Note: The number will not have a very large prime factor.

Output
The sum of the factors for each test case.

Example
Input:
2
6
5
Output:
12
6
Added by:
Date:
Time limit:
Source limit:
Memory limit:
Cluster:
Languages:
Resource:

Rahul
2007-03-10
1s
4000B
256MB
Pyramid (Intel Pentium III 733 MHz)
All except: ERL JS NODEJS PERL 6
Siddharth Agarwal

SPOJ Problem Set (tutorial)

2524. Conversions
Problem code: GNY07B

Conversion between the metric and English measurement systems is relatively simple. Often, it
involves either multiplying or dividing by a constant. You must write a program that converts between
the following units:
Type
Weight
Volume

Metric

English equivalent

1.000 kilograms
0.4536 kilograms
1.0000 liter
3.7854 liters

2.2046 pounds
1.0000 pound
0.2642 gallons
1.0000 gallon

Input
The first line of input contains a single integer N, (1 N 1000) which is the number of datasets that
follow.
Each dataset consists of a single line of input containing a floating point (double precision) number, a
space and the unit specification for the measurement to be converted. The unit specification is one of
kg, lb, l, or g referring to kilograms, pounds, liters and gallons respectively.

Output
For each dataset, you should generate one line of output with the following values: The dataset
number as a decimal integer (start counting at one), a space, and the appropriately converted value
rounded to 4decimal places, a space and the unit specification for the converted value.

Example
Input:
5
1 kg
2 l
7 lb
3.5 g
0 l
Output:
1 2.2046 lb
2 0.5284 g
3 3.1752 kg
4 13.2489 l
5 0.0000 g
Added by:
Date:
Time limit:
Source limit:
Memory limit:
Cluster:
Languages:

Marco Gallotta
2008-03-11
60s
50000B
256MB
Pyramid (Intel Pentium III 733 MHz)
All except: ERL JS NODEJS PERL 6

Resource:

ACM Greater New York Regionals 2007

SPOJ Problem Set (tutorial)

2912. Super Primes


Problem code: SPRIME

In mathematics, a prime number (or a prime) is a natural number which has exactly two distinct
natural number divisors: 1 and itself.
Super-prime numbers are the elements of the subsequence of prime-numbers that occupy primenumbered positions within the sequence of all prime numbers. That is, if p(i) denotes the ith prime
number, the numbers in this sequence are those of the form p(p(i)) or Primes with a prime index in the
sequence of prime numbers (the 2nd, 3rd, 5th, ... prime).
Your task is to generate all super primes &le 10^7.

Input:
There is NO input for this problem.

Output:
Print all super-primes &le 10^7 in ascending order,one per line.

First few lines of Output


3
5
11
17
31
41
59
67
83
109
...
Added by:
Swarnaprakash
Date: 2008-08-05
Time limit:
8s
Source limit: 10000B
Memory limit:256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages:
Resource:

All except: ERL JS NODEJS PERL 6


Myself

SPOJ Problem Set (tutorial)

3032. Adding two numbers


Problem code: ADUN

Your task is to read two numbers a and b (0 < a, b <2100000000) and to output their sum.

Input
Input contains two lines, on the first line the number a and on the second line the number b.

Output
Output the sum of the two numbers.

Example
Input:
20
30
Output:
50

Added by: Pripoae Toni


Date: 2008-09-14
Time limit: 1s
Source limit: 1024B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: ERL JS
Resource:
Original

SPOJ Problem Set (tutorial)

3378. Mirrored Pairs


Problem code: MIRRORED

The letters b and d are mirror images of each other, as are p and q. No other pairs of letters are
mirrors, except for letters like H that are mirrors of themselves, and whats the interest in a pair thats
just two of the same letter? We refuse to count self-mirrors as mirrored pairs.

Input
Input is a list of lines with two characters on each line. Your program should end immediately when it
encounters a line with two spaces.

Output
The first line of output should contain only Ready. For each pair of characters (prior to a pair of
spaces), print the line Mirrored pair if the characters are mirrors, otherwise print the line Ordinary pair.

Example
Input:
Fr
qp
HH
db
pq
Output:
Ready
Ordinary
Mirrored
Ordinary
Mirrored

pair
pair
pair
pair

Added by:

Daniel Gmez Didier

Date: 2008-11-17
Time limit:
1s
Source limit: 50000B
Memory limit:256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages:
Resource:

All except: ERL JS NODEJS PERL 6


2007 PUJ - Circuito de Maratones ACIS / REDIS

SPOJ Problem Set (tutorial)

3440. Enormous Input and Output Test


Problem code: INOUTEST

Similar to that of the problem Enormous


Input Test, the purpose of this problem is to determine whether your method of reading input data and
writing output data is fast enough to process extremely large test cases.

Input
The first line of input contains a single integer N (1 N 10 6), denoting the number of lines to follow.
Each of these lines contains two space-separated integers a and b (|a|, |b| 40,000).

Output
For each pair a and b, write a single line of output containing the value of a b.

Example
Input:
5
-1 -1
1 1
0 999
654 321
39999 -39999
Output:
1
1
0
209934
-1599920001

Added by:

Neal Wu

Date: 2008-11-30
Time limit: 20s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: ERL JS

SPOJ Problem Set (tutorial)


4073. The 3n plus 1 problem
Problem code: PROBTNPO
Background: Problems in Computer Science are often classified as belonging to a certain class of problems (e.g.,
NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is
not known for all possible inputs.
The Problem:
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n = 3n + 1
5. else n = n / 2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite
the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all
integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is
called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less
than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers
between and including i and j.
You can assume that no operation overflows a 32-bit integer.
The Output:
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between
and including i and j. These three numbers should be separated by at least one space with all three numbers on
one line and with one line of output for each line of input. The integers i and j must appear in the output in the
same order in which they appeared in the input and should be followed by the maximum cycle length (on the
same line).
Sample Input:
1 10
100 200
201 210
900 1000
Sample Output:
1 10 20
100 200 125
201 210 89
900 1000 174

Added by:

Coach UTN FRSF

Date: 2009-03-18
Time limit:
3s-10s
Source limit:
50000B
Memory limit: 256MB
Cluster:Pyramid (Intel Pentium III 733 MHz)
Languages:

All except: ERL JS NODEJS PERL 6

Resource:

http://icpcres.ecs.baylor.edu/onlinejudge/

SPOJ Problem Set (tutorial)

4171. DIOPHANTINE
Problem code: DIOEQ

Given a,b,c (where a,b not both zero) your task is to find if there exist any integers x,y that satisify the
equation a*x+b*y=c.

Input
The input consists of several test cases. Each test case contains a line with the three integers a,b,c
(a,b,c <= 10000000). The test case 0 0 0 indicates the end of input.

Output
For each test case you must output "YES" if there exists a solution,otherwise output "NO". (quotes only
for clarity)

Example
Input:
1 9 1
5 10 19
0 0 0
Output:
YES
NO

Added by: abhijith reddy d


Date: 2009-04-06
Time limit: 1s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: ERL JS NODEJS PERL 6
Resource:
own

SPOJ Problem Set (tutorial)

4190. A plus B
Problem code: APLUSB2

Given 2 integers, A and B, find their sum.


Beware: There are no limits on A and B

Input
Two integers up to 100000 digits in length

Output
Output their sum

Example
Input:
1
4
Output:
5

Added by:

Saravannan Shaan

Date: 2009-04-09
Time limit: 1s-60s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: C99 strict ERL JAVA JS NODEJS PERL 6 PYTH 2.7 RUBY
Resource:
Classic

SPOJ Problem Set (tutorial)

4768. Cool Numbers


Problem code: COOLNUM
Eric likes interesting numbers like 64. It turns out that 64 is both a square and a cube, since 64 = 8^2
and 64 = 4^3 . Eric calls these numbers cool.
Write a program that helps Eric figure out how many integers in a given range are cool.

Input
On the first line of input, you are given an integer a such that a 1 and a 10^8 . On the second
line of input, you are given an integer b such that a b and b 10^8 .

Output
The output should be the number of cool numbers in the range a to b (inclusively: that is, a and b
would count as cool numbers in the range if they were actually cool).

Example
Input:
1
100
Output:
2
Input:
100
1000
Output:
1

Added by: Mislav Balunovi


Date: 2009-09-02
Time limit: 0.100s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: ERL JS NODEJS PERL 6
Resource:
Canadian Computing Competition

SPOJ Problem Set (tutorial)

4770. Add and Reverse Sums


Problem code: ADDREVUT
The Problem The "reverse and add" method is simple: choose a number, reverse its digits and add it
to the original. If the sum is not a palindrome (which means, it is not the same number from left to right
and right to left), repeat this procedure.
For example:
195 Initial number
591
----786
687
----1473
3741
----5214
4125
----9339 Resulting palindrome
In this particular case the palindrome 9339 appeared after the 4th addition. This method leads to
palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the
first number for which no palindrome has been found. It is not proven though, that there is no such a
palindrome.
Task :
You must write a program that give the resulting palindrome and the number of iterations (additions) to
compute the palindrome.
You might assume that all tests data on this problem:
- will have an answer ,
- will be computable with less than 1000 iterations (additions),
- will yield a palindrome that is not greater than 4,294,967,295.
The Input
The first line will have a number N with the number of test cases, the next N lines will have a number P
to compute its palindrome.
The Output
For each of the N tests you will have to write a line with the following data : minimum number of
iterations (additions) to get to the palindrome and the resulting palindrome itself separated by one
space.
Sample Input
3
195
265
750
Sample Output
4 9339
5 45254
3 6666

Added by: Coach UTN FRSF


Date: 2009-09-03
Time limit: 1s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: ERL JS NODEJS PERL 6
Resource:
http://online-judge.uva.es/p/v100/10018.html

SPOJ Problem Set (tutorial)

4872. Good Sequence


Problem code: KZGD
It is good to hav an auspicious start to any event.The kruzade OPC team felt that online coding event
should
also have an auspicious start.As a mark of auspiciousness, we define good sequence as follows:
A good number is defined as a non-negative number that has an odd number of 1s in its binary
expansion(that is when the decimal number is converted to base 2).
for eg.
1=1 num of 1s in binary equiv=1(odd) so,1 is a good number
2=10 num of 1s in binary equiv=1(odd) so,2 is a good number
3=11 num of 1s in binary equiv=2(even) so,3 is not a good number
The good sequence is the collection of good numbers.
The good sequence goes like this:
1,2,4,7,8,11,13,14,16,19...
You have been hired to find out the nth good number in the sequence.

Input
First line contains an integer T, representing the
follow each containing one integer n, 1 <= n <= 500.

number

of

Output
For each test case output on a line the nth good number in the sequence.

Example
Input:
3
10
5
20
Output:
19
8
38

Added by:

balaji

Date: 2009-09-28
Time limit:

4s

Source limit: 50000B


Memory limit:
Cluster:

256MB

Pyramid (Intel Pentium III 733 MHz)

Languages: All except: ERL


Resource:

kruzade 09 main

test-cases.

Then

lines

SPOJ Problem Set (tutorial)

4906. Pythagorean Triples


Problem code: PYTHTRIP

A Pythagorean triple (A, B, C) is defined


as three positive integers that satisfy the Pythagorean Theorem: A2 + B2 = C2. Given two positive
integers A and B, your task is to verify whether they are the "legs" in a Pythagorean triple, i.e. if an
integer C exists such that (A, B, C) is a Pythagorean triple.

Input
The first line will contain a single integer N (0 < N <= 10000). Each of the next N lines will contain two
integers A and B (0 < A, B <= 100).

Output
For each test case, output a single line. If a valid C exists, output a line containing the word YES and
the value of C, separated by a space. Otherwise, output the single word NO.

Example
Input:
4
2 2
4 3
4 5
5 12
Output:
NO
YES 5
NO
YES 13

Added by: Miorel-Lucian Palii


Date: 2009-10-04
Time limit: 2s
Source limit: 4096B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: C++ 4.3.2 ERL NODEJS PERL 6
Resource:
University of Florida Local Contest - April 13, 2009

SPOJ Problem Set (tutorial)

5313. Root of the Problem


Problem code: MCPC06G

Given positive integers B and N, find an integer A such that A^N is as close as possible to B. (The
result A is an approximation to the Nth root of B.) Note that A^N may be less than, equal to,
or greater than B.

Input
The input consists of one or more pairs of values for B and N. Each pair appears on a single line,
delimited by a single space. A line specifying the value zero for both B and N marks the end of the
input. The value of B will be in the range 1 to 1,000,000 (inclusive), and the value of N will be in the
range 1 to 9 (inclusive).

Output
For each pair B and N in the input, output A as defined above on a line by itself.

Example
Input:
4 3
5 3
27 3
750 5
1000 5
2000 5
3000 5
1000000 5
0 0
Output:
1
2
3
4
4
4
5
16

Added by:

Tamer

Date: 2009-11-16
Time limit: 1s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: NODEJS PERL 6
Resource:
ACM Mid-Central Regional Programming Contest 2006

SPOJ Problem Set (tutorial)

5848. Filip
Problem code: FILIP

Mirko has a younger brother, Filip, who just started going to school and is having trouble with
numbers. To help him get the hang for number scale, his teacher writes two three digit numbers. She
asks Filip to compare those numbers, but instead of interpreting them with the leftmost most
significant digit, he needs to interpret them the other way around, with the most significant digit being
the rightmost one. He than has to tell the teacher the larger of the two numbers.
Write a program that will check Filips answers.

Input
The first and only line of input contains two three digit numbers, A and B. A and B will not be equal
and will not contain any zeroes.

Output
First and only line of output should contain the larger of the numbers in the input, compared as
described in the task. The number should be written reversed, to display to Filip how he should
read it.

Example
Input:
734 893
Output:
437
Input:
221 231
Output:
132
Input:
839 237
Output:
938

Added by: .:: Pratik ::.


Date: 2010-01-12
Time limit: 1s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All
Resource:
COCI 2009/2010 CONTEST #3

SPOJ Problem Set (tutorial)

5873. Rnumber
Problem code: RNUM

In this problem your task is to reduce a given number 'N' to a non-positive number in as little moves
as possible. The moves allowed are : Given an integer 'N' you can subtract one of its factors (excluding
'N' itself) from 'N' and continue the same process with the resulting number until you reach a nonpositive number

Input
First line contains the number of test cases 'T'. 'T' lines follow containing a single integer 'N'
2<=N<100,000.

Output
A single integer denoting the minimum number of moves necessary.

Example
Input:
1
10
Output:
5

Added by: .:: Pratik ::.


Date: 2010-01-13
Time limit: 2s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: NODEJS PERL 6

SPOJ Problem Set (tutorial)

7207. Alchemy
Problem code: ALCHE

Alchemy is a discipline that is believed to span at least 2500 years of human history.
It is most known for its intention of transforming matter, typically trying to come up
with a recipe to make gold based on much less valued metals, aided by some non-metal
components.
Most scientists and scholars think that alchemy has failed. They surely do not know that
a particular alchemist named Albert Ainstain, managed to create gold from a simple
combination of ordinary iron (much more common and cheap than gold) and some goodold-fashioned water. However, the combination must have the exact proportion of grams
of iron and centiliters of water to work, otherwise the alchemist would end up with useless
rusty iron.
Many alchemists and bussinessman had tried to recreate Albert Ainstains findings to
achieve recognition, fame, prestige or economical welfare, but none of them have suceeded.
Since you know very little about chemistry, alchemy and ancient practices, you believe
that your chances for success in this task are bounded to using a computer in your benefit.
Therefore, you decide to create a program that automatically tests a given combination
of iron and water, and informs whether that combination has the correct proportion
to produce gold. Of course, once you find the right proportion, you can double both
amounts and get double the gold, cut both in half and get half the gold, or multiply both
by any other real number to obtain the amount of gold you want. Your task is then,
given the number of grams of iron and the number of centiliters of water, say whether
the proportion between both components is the right one.

Input
The input contains several test cases, each one described in a single line. The line
contains two integers I and W separated by a single space, representing grams of iron
and centiliters of water, respectively (1 I, W 106 ). The last line of the input contains
the number 1 twice separated by a single space and should not be processed as a test
case.

Output
For each test case output a single line containing an uppercase Y if the combination
produces gold, or an uppercase N otherwise.

Example
Input:
1000 37
999 37
10000 370
10001 370
-1 -1
Output:
Y
N
Y
N

Added by: Pablo Ariel Heiber


Date: 2010-08-22
Time limit: 1s
Source limit: 50000B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: NODEJS PERL 6

Resource:

FCEyN UBA ICPC Selection 2009

SPOJ Problem Set (tutorial)

7236. The next odd number


Problem code: NEXTODD
Given an integer 1 <= N <= 10000,you have to print the next odd number.
Score is the length of your source.

Input
The input file consists of series of numbers one in each line.The input is terminated by EOF.

Output
The next odd number.

Example
Input:
5
2
6
17
Output:
7
3
7
19

PS:Take care about your fingers, do not use more than 100 bytes of code.

Added by: .:: Debanjan ::.


Date: 2010-08-25
Time limit: 10s
Source limit: 100B
Memory limit:
256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All except: NODEJS
Resource:
From a shortening contest.

SPOJ Problem Set (tutorial)

9111. Prime Time


Problem code: NSUJ02A

Maria really loves prime times. In her


definition, a time is called prime time, if both numbers in hour and minute are primes. For example
07:11 is a prime time, since both of them are prime numbers, but 04:17 or 07:09 are not prime time.
You decided to write a bot that will call her in prime times, now you have to write a program that can
understand if a time is a prime time or not.

Input
First number in the input is t, number of test cases.
After that t lines will follow each with a time in hh:mm format, where hh will mean the hour and mm
will mean the minute. hh will be an integer within 0 to 23, and mm will be an integer within 0 to 59.

Output
Print "Yes" (without quotes) if it's a prime time, "No" (without quotes) if it's not.

Example
Input:
3
07:11
04:17
07:09
Output:
Yes
No
No
Added by:
Iqram Mahmud
Date: 2011-07-03
Time limit:
1s
Source limit: 50000B
Memory limit:256MB
Cluster:
Pyramid (Intel Pentium III 733 MHz)
Languages: All
Resource:
Own

You might also like