Acm Icpc Bicol2013 Problemset Final

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Problem A: Honey Bees

Input from file: pa.in


Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: seconds
!"e #e"avioral activity of $or%er "oney #ees can #e divided into t"e follo$in& reco&ni'a#le tas%s:
restin& (Re), patrollin& (Pt), Cell(cleanin& (Cc), eatin& pollen (Ea), tendin& #rood (Tb), com#(#uildin&
and maintenance (Cm), and external activities (Ex) suc" as &uardin&, fora&in& and dance(follo$in&.
!"e tas%s carried out #y a "oney #ee $ere recorded at "ourly intervals for a day, resultin& in a data set
consistin& of $"itespace separated sym#ols from t"e set )Re, Pt, Cc, Ea, Tb, Cm, Ex*.
INPUT FORMAT
!"e input consists of sym#ols from t"e set )Re, Pt, Cc, Ea, Tb, Cm, Ex*. !"e sym#ols are $"itespace
separated and could span for several lines. !"ere are at most + sym#ols.
OUTPUT FORMAT
!"e output is a ta#le consistin& of , lines. -ines . to / are values in t"e follo$in& format:
Task Count Proportion
!"e tas%s s"ould #e ordered t"is $ay: Re, Pt, Cc, Ea, Tb, Cm, and Ex. !"e count is t"e total fre0uency
count of t"e tas% $"ile proportion is t"e t"e fre0uency count divided #y t"e total num#er of recorded
tas%s. !"e proportion s"ould #e rounded off to t"e nearest "undret"s and s"ould s"o$ exactly t$o
di&its after t"e decimal point.
!"e ,t" line s"ould al$ays #e 1Total <total> 1.002 $"ere <total> is t"e total num#er of
recorded tas%s.
SAMPLE INPUT
Cc Pt Pt Re Tb Re Cm Cm Re Pt Pt Re Ea Ea Pt Pt
Pt Re Re Cb Cb Pt Pt Cb
SAMPLE OUTPUT
Re 6 0.25
Pt 9 0.38
Cc 1 0.04
Ea 2 0.08
Tb 1 0.04
Cm 2 0.08
Ex 0 0.00
Total 24 1.00
33
Copyri&"t 4 5.6, 7afael Salda8a and 9C: IC;C ;"ilippines :ulti(;rovincial ;ro&rammin& Contest
ACM ICPC Problem 1
Problem B: Repeating Decimals
Input from file: pb.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Eecution time limit: ! seconds
" routine problem in basic mat#ematics is to convert fractions to decimals. $or eample,
%
&
is 'ust
(.! )#ile
%
*
is (.***..., )#ic# is simply )ritten as (.

* . +#e decimal (.! is said to be terminatin,


)#ile (.

* is repeatin,. Ot#er eamples of fractions )#ic# convert to repeatin, decimals are:


&
-
=(. &.!-%/
%-
00
=(.&!-!
&!
0
=/.%0
*/(%
***
=%(. &%*
1our tas2 is to determine t#e fraction in lo)est terms t#at is e3ual to a ,iven repeatin, decimal.
INPUT FORMAT
+#e input consists of one or more input lines. Eac# input line consists of a repeatin, decimal. +#e di,its
t#at repeat are enclosed in a pair of parent#eses. +#e total number of di,its in t#e decimal is at most 4.
OUTPUT FORMAT
+#e output consists of one or more lines )it# eac# line consistin, of t#e ,iven input follo)ed by t#e
e3ual si,n and follo)ed by t#e fraction in lo)est terms. +#e fraction is displayed in t#e form x 5 y,
)#ere x is t#e numerator and y is t#e denominator.
SAMPLE INPUT
0.(285714)
0.25(75)
4.1(6)
SAMPLE OUTPUT
0.(285714) = 2 / 7
0.25(75) = 17 / 66
4.1(6) = 25 / 6
66
Copyri,#t 7 &(%*, 8ernard 9ar3ue: and "C9 IC;C ;#ilippines 9ulti<;rovincial ;ro,rammin, Contest
2 ACM ICPC Problem
Problem C: Solutions of Cubic Equation
Input from file: pc.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: seconds
!anna" as#ed you to determine all distinct real solutions of a cu$ic e%uation in t"e follo&in' form:
(x
)
+ *x

+ Cx + + , -
."e tas# is %uite complicated especially if one solution is irrational. *efore you complained, !anna"
told you t"at s"e/ll only 'ive cu$ic e%uations &it" at least one inte'er solution.
0our tas# t"en is to find all distinct real solutions of t"e cu$ic e%uation 'iven t"e coefficients (, *, C,
and + &"ere ( is not -.
."e ran'e of values of (, *, C, and + is 1,---,--- to ,---,---. ."e ran'e of values of x is
12,---,--- to 2,---,---.
INPUT FORMAT
."e input starts &it" an inte'er N (&"ere - 3 N 3 2--). It is t"en follo&ed $y N input lines. Eac" input
line consists of four num$ers (, *, C, and +, not necessarily inte'ers. ( is never -.
OUTPUT FORMAT
4or eac" input line, print all distinct real solutions of t"e cu$ic e%uation in one line, sorted in increasin'
order. Eac" real solution must $e rounded off to t"e nearest ten t"ousandt" and must $e printed &it"
exactly four di'its after t"e decimal point. .&o or more distinct real solutions must $e separated $y
exactly one space.
SAMPLE INPUT
3
1.5 -5 2 -1.5
2 -7 7 -2
2 0 0 0
SAMPLE OUTPUT
3.0000
0.5000 1.0000 2.0000
0.0000
55
Copyri'"t 6 -2), (llan Sioson and (C7 IC8C 8"ilippines 7ulti18rovincial 8ro'rammin' Contest
ACM ICPC Problem 3
Problem D: Multiplication in a Dihedral Group
Input from file: pd.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: seconds
! di"edral #roup is a set $ %it" a multiplication operation & defined on $. '"e set $ is #enerated (y
t%o elements a and (. '"e multiplication operation & %ill not (e explicitly %ritten, so t"at a&a %ill (e
indicated (y aa or (y a
)
. Similarly, a&a&( %ill (e indicated (y a
)
(
*
or simply a
)
(.
'"e di"edral #roup $ "as t%o inte#er parameters m and n, %it" m + ) and n + ), and so %e %rite t"e
di"edral #roup as $
m,n
. '"e relations t"at define $
m,n
are:
a
m
, a
-
, *, (
n
, (
-
, *, and (a , a
(m.*)
(.
'"us $
m,n
"as (mn) elements, namely
$
m,n
, / a
0
(
1
2 - 3 0 4 m, - 3 1 4 n 5 , / a
-
(
-
, a
*
(
-
, a
)
(
-
, 6 , a
(m.*)
(
-
, . . . , a
(m.*)
(
(n.*)
5.
7or example, select m , 8 and n , ). '"e elements of $
8,)
are
/ a
-
(
-
, a
*
(
-
, a
)
(
-
, a
9
(
-
, a
:
(
-
, a

(
-
, a
;
(
-
, a
-
(
*
, a
*
(
*
, a
)
(
*
, a
9
(
*
, a
:
(
*
, a

(
*
, a
;
(
*
5.
<ultiplication is not commutative, and so (a = a(. In fact (a , a
;
(, accordin# to t"e definin# relations
of $
8,)
. !lso, a
0
a
1
, a
(0+1)>m
, and (
0
(
1
, (
(0+1)>n
. '"us
(a
0
(
1
)(a
p
(
?
) = (a
(0+p)>m
(
(1+?)>n
).
'o properly multiply t"e t%o elements (a
9
(
*
) and (a
)
(
*
) of $
8,)
, %e proceed as follo%s,
(a
9
(
*
)(a
)
(
*
) , (a
9
(
-
)((a)(a
*
(
*
) , (a
9
(
-
)(a
;
(
*
)(a
*
(
*
),
since (a , a
;
(.
!lso, since a
9
(
-
a
;
, a
9
a
;
, a
(9+;)>8
, a
)
, %e can multiply t"e first t%o factors and #et (a
9
(
-
)(a
;
(
*
) , (a
)
(
*
).
'"us
(a
9
(
-
)(a
;
(
*
)(a
*
(
*
) , (a
)
(
*
)(a
*
(
*
).
@o% %e can re%rite t"is as follo%s,
(a
)
(
*
)(a
*
(
*
) , (a
)
(
-
)((a)(a
-
(
*
) , (a
)
(
-
)(a
;
(
*
)(a
-
(
*
) , (a
A
(
)
) , (a
*
(
-
).
'"us, multiplyin# t"e t%o elements (a
9
(
*
) and (a
)
(
*
) #ives, (a
9
(
*
)(a
)
(
*
) , (a
*
(
-
).
4 ACM ICPC Problem
Bour pro(lem is to %rite a computer pro#ram, #iven m, n, and any t%o elements (a
0
(
1
) and (a
p
(
?
) of t"e
di"edral #roup $
m,n
t"at computes t"e correct product of t"e t%o.
INPUT FORMAT
'"e input consists of several pro(lem sets. Eac" pro(lem set consists of several lines of input. '"e first
line %ill contain t"e pro(lemI$, m, n, and p, separated (y spaces. '"e inte#ers m and n %ill not exceed
*--- in value. '"e num(er p is t"e num(er of multiplication pro(lems for t"is pro(lem set. '"e first
line is follo%ed (y p lines of input, eac" line containin# t%o elements of $
m,n
to multiply. Eac" element
%ill (e #iven in t"e form a9(* instead of a
9
(
*
. $ata for t"e next pro(lem set %ill immediately follo%
t"e previous pro(lem set. ! set pro(lemI$ of CZZD %ill indicate end of input.
OUTPUT FORMAT
7or eac" multiplication pro(lem, print one line of t"e form
ProblemID id: aj(k & ap(q = ar(s
%"ere id is t"e pro(lemI$ #iven in t"e input, aj(k and ap(q are t"e elements to multiply, and ar(s is
t"e correct ans%er.
SAMPL INPUT
C1 7 2 3
a3b1 a2b1
a5b1 a3b1
a2b1 a4b1
C2 9 3 2
a3b1 a2b1
a4b1 a3b1
ZZ 0 0 0
SAMPL OUTPUT
ProblemID C1: a3b1 * a2b1 = a1b0
ProblemID C1: a5b1 * a3b1 = a2b0
ProblemID C1: a2b1 * a4b1 = a5b0
ProblemID C2: a3b1 * a2b1 = a1b2
ProblemID C2: a4b1 * a3b1 = a1b2
EE
Copyri#"t F )-*9, Ga(lo <analastas and !C< ICGC G"ilippines <ulti.Grovincial Gro#rammin# Contest
ACM ICPC Problem 5
Problem E: Area Between Outer Hull and Inner Hull
Input from file: pe.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: seconds
!iven a set S of N points on t"e plane, namely S # $(x%&', y%&'), (x%(', y%('), ... , (x%N)(', y%N)(')*, t"e
outer hull +o of S is simply ,ust t"e convex "ull of S. It is a su-set of points of S ."ic" ."en ,oined -y
line se/ments .ill form a convex poly/on P of minimum area t"at includes all points of S inside P or
on P. 0oints of S fallin/ on P -ut are not corner vertices of P are not considered part of t"e convex "ull.
1"e inner hull +i of S is o-tained -y /ettin/ t"e convex "ull of S 2 +o. 1"at is, remove from set S all
points t"at -elon/ to t"e outer "ull +o, to /et t"e set (S 2 +o). 1"en compute t"e inner "ull +i as t"e
convex "ull of (S 2 +o).
3or example /iven t"e set S of 4 points 5(6.&, .&), 7(6.&, 8.&),
C(6.&, 6.&), 9((.&, (.&), E(8.&, (.&), 3(&.&, &.&), !(:.&, &.&), and
+(.&, &.&), t"e outer "ull +o of S can -e computed as t"e set
+o#$3, +, 5, 3*. If .e remove +o from S, .e /et S 2 +o # $7, C,
9, E, !*. 1"e inner "ull +i of S is computed as t"e convex "ull of
S 2 +o, namely +i # $9, !, E, 7, 9*. ;o. t"e area inside +o is
area(+o) # (6.. 1"e area inside +i is area(+i) # <.&. 1"us t"e area
-et.een t"e outer "ull +o and t"e inner "ull +i is (6. 2 <.& # <..
=our pro-lem is, /iven a set S of N points, to .rite a pro/ram t"at
computes t"e area -et.een t"e outer "ull +o of S, and t"e inner "ull
+i of S.
INPUT FORMAT
1"e input consists of several pro-lem sets. 1"e first line of a pro-lem set .ill contain t"e pro-lemI9
and t"e value of N. 1"e value of N .ill not exceed (,&&&. 1"e next N lines of t"e pro-lem set .ill
contain t"e value of x) and y)coordinate (separated -y a space) of a point, at one point per line. Eac"
coordinate x or y .ill -e a real num-er not exceedin/ (&&.& in a-solute value. 1"e lines of t"e next
pro-lem set .ill immediately follo. t"e previous pro-lem set. 5 line .it" pro-lemI9 of >ZZ? and a
value of N of & indicates t"e end of input.
OUTPUT FORMAT
3or eac" pro-lem set, print one line of t"e form >ProblemID id: area? ."ere id is t"e pro-lemI9 /iven
in t"e input, and area is t"e area -et.een t"e outer "ull +o and t"e inner "ull +i t"at you computed.
Express t"e area .it" 8 decimal places.
See sample input and corresponding output on the next page.
6 ACM ICPC Problem
SAMPLE INPUT
A1 8
2.0 5.0
2.0 4.0
2.0 2.0
1.0 1.0
4.0 1.0
0.0 0.0
3.0 0.0
5.0 0.0
A2 11
1.0 5.0
5.0 5.0
2.0 4.0
3.0 4.0
2.0 3.0
2.0 2.0
3.0 2.0
1.0 1.0
4.0 1.0
0.0 0.0
4.0 0.0
ZZ 0
SAMPLE OUTPUT
ProblemID A1: 6.5000
ProblemID A2: 14.0000
@@
Copyri/"t A 6&(:, 0a-lo Banalastas and 5CB IC0C 0"ilippines Bulti)0rovincial 0ro/rammin/ Contest
ACM ICPC Problem 7
Problem F: Sharkovskis Ordering
Input from file: pf.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: seconds
In a !"#$ paper on continuous mappin%s of t&e reals into t&e reals, 'lexandr S&ar(ovs(i used t&e
follo)in% orderin% of t&e positive inte%ers:
* + , + - + " + ... + *. + ,. + -. + ... + *.

+ ,.

+ ... +
*
+

+ + !
's Ciesiels(i and /o%oda (001) descri2e it:
34irst come t&e odd num2ers, 2e%innin% )it& *, arran%ed in increasin% order. 5&is se6uence is
repeated )it& eac& odd inte%er multiplied 2y . 5&e initial se6uence is a%ain repeated )it& eac& odd
inte%er multiplied 2y

, and so on. 5&e terminal se6uence consists of t&e nonne%ative po)ers of


arran%ed in decreasin% order (note t&at ! 7
0
).8
9rite a pro%ram t&at reads an input containin% a list of up to ,, unsi%ned inte%ers )it& values less
t&an or e6ual to #,,,*, (not necessarily distinct) separated 2y )&ite space and terminated 2y :0;. 5&e
pro%ram s&ould display on t&e screen t&e num2ers arran%ed in S&ar(ovs(i;s orderin% in one line. 5&e
num2ers in t&e line are separated exactly 2y one space.
INPUT FORMAT
5&e input starts )it& an inte%er N (0 < N < ,,). 5&is is follo)ed 2y N input cases. Eac& input case
is a non-empty list of up to ,, unsi%ned inte%ers (not necessarily distinct) )it& values not exceedin%
#,,,*,. Eac& pair of num2ers is separated 2y )&ite space. Eac& input case is terminated 2y :0;.
OUTPUT FORMAT
4or every input case, print t&e re6uired num2ers arran%ed in S&ar(ovs(i;s orderin% in one line. 5&e
num2ers in t&e line are separated exactly 2y one space.
SAMPLE INPUT
2
3 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 0
SAMPLE OUTPUT
3
3 5 7 9 11 13 15 17 19 6 10 14 18 12 16 8 4 2 1 1
==
Copyri%&t > 0!*, Joel ?oc&e and 'C@ IC/C /&ilippines @ultiA/rovincial /ro%rammin% Contest
8 ACM ICPC Problem


Problem G: M M Detour
Input from file: pg.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: 5 seconds

Metro Manila currently has five circumferential roads arranged in concentric semicircles, with a sixth
to be constructed linking the north and the south. From the center (Manila), there are ten radial roads
that link the inner and outer areas of Metro Manila.















Figure G-1 Ideal road network of Metro Manila

In this problem, we are to find the shortest distance to get from point A to point B. Each point is
defined by the intersection of a circumferential and radial road pair (C#, R#) with the center
represented as (C0, R0). For calculation purposes, the distance for each segment in the radial road is
equal to 1 unit, while each arc is equal to the circumferential road number. For instance, the distance
from (C1, R3) to (C2, R3) is one unit, and the distance from (C3, R1) to (C3, R2) is three units. The
shortest distance from (C1, R2) to (C5, R1) is five units (See darkened path). It is assumed that C6 is
passable for this problem.
However with the new number coding scheme, vehicles are not allowed to use the radial roads with
the same last digit of the plate numbers. For instance, a vehicle with plate number of ABC-123 cannot
use R3. Furthermore, the circumferential roads (except for C6) also have restrictions. Plate numbers
ending in 1 and 2 are not allowed in C1, 3 and 4 in C2, 5 and 6 in C3, 7 and 8 in C4, and 9 and 0 in
C5. Thus the shortest path from (C1, R2) to (C5, R1) for vehicle with plate number ABC-123 is five
units, but nine units for vehicle with plate number CBA-321. Note that there will be instances that a
vehicle cannot reach the destination at all.
(C6,R1) southernmost
intersection
R3 Sergio Osmea, Sr. Highway - SLEX
R1 Roxas Boulevard- Manila-Cavite (Coastal) Highway
R2 Taft Avenue E. Quirino Highway
R4 Tejeron St. J.P. Rizal St.
R5 Ramon Magsaysay Shaw Boulevard
R6 Ramon Magsaysay Aurora Boulevard

R7 Espaa Quezon Avenue
R8 Dimasalang A. Bonifacio - NLEX
R9 Rizal Avenue MacArthur Highway
R10 Marcos Highway
C1 Recto - Ayala Boulevard
C2 Tayuman Nagtahan - Quirino
C3 5
th
Ave Araneta -Ayala Gil Puyat
C4 EDSA
C5 C.P. Garcia Katipunan Tandang Sora C5
Ext
C6 Bulacan-Rizal-Manila-Cavite Regional Expressway
ExpresswayExpressway
ACM ICPC Problem 9


INPUT FORMAT
The input consists of multiple test cases. For simplicity, each test case will have 5 single digit
numbers that are whitespace separated. The first two numbers represent the starting point, the next
two numbers represent the destination point, and the last number is the ending digit of the plate
number. There are no blank lines in between test cases, and the last test case is followed by a line
containing a single zero.
OUTPUT FORMAT
For each test case, print the case number (starting with 1) followed by the shortest distance needed.
Output not possible if the destination cannot be reached.

SAMPLE INPUT
1 2 5 1 3
1 2 5 1 1
0
SAMPLE OUTPUT
Case 1: 5
Case 2: 9


--
Copyright 2013, Caslon Chua and ACM ICPC Philippines Multi-Provincial Programming Contest

10 ACM ICPC Problem


Problem H: More Fun in Bicol
Input from file: ph.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: 5 seconds

The Bicol region (Region 5) is composed of six provinces. They are Albay, Camarines Norte,
Camarines Sur, Sorsogon and the island-provinces of Catanduanes and Masbate. The cities in the
Bicol region are Iriga City, Legazpi City, Ligao City, Masbate City, Naga City, Sorsogon City, and
Tabaco City.





















Figure H-1: Map of Bicol Region (Location are for illustration purposes only)

Most of the contestants could not wait to complete the competition and had begun planning on
exploring the region. They had started asking where certain places were and are in the process of
listing down the relative location between places.

INPUT FORMAT
The input contains several test cases. Each test case contains two data sets. The first data set describes
the relative location of the first place to the second, and the second data set contains queries on the
relative location of the first place to the second. For example

Data set 1
Place 1 Place 2 Relative Location
Naga City City of Iriga Northwest
Masbate City Naga City Southeast
Tabaco City City of Ligao Northeast
Sorsogon City Legazpi City Southeast
City of Ligao Legazpi City West

Naga City
Tabaco
City
Legazpi City
City of
Ligao

Sorsogon City
Masbate City
City of
Iriga
ACM ICPC Problem 11

Data set 2
Place 1 Place 2
Naga City City of Iriga
City of Iriga Naga City
Sorsogon City Naga City
Sorsogon City City of Ligao

Thus in the above example, there are 5 and 4 entries in data set 1 and 2 respectively.

Data set 1 is composed of a set of city pair values and a relative location. Each entry indicates that
relative location of place 1 to 2. For instance, the entry (Naga City, City of Iriga, Northwest) means
that the Naga City is northwest of City of Iriga. However, the data set will not have reverse
relationship entry. For instance, (City of Iriga, Naga City, Southeast)

The data starts with two integers, m and n, representing the number of entries in data sets 1 and 2
respectively, where (1 m 100) and (1 n 50). This is followed by m entries of position
description and n entries of queries.

The data entries are comma delimited and may contain mixed cases. The allowed relative positions
are north, south, east, west, northeast, northwest, southeast and southwest. It is assumed that the each
individual relative position has a value of 1. These are 1 north, 1 south, 1 east, 1 west, 1 north and 1
east, 1 north and 1 west, 1 south and 1 east, and 1 south and 1 west respectively. Thus, the distance
from Masbate City to Naga City is the same as Sorsogon City to Legazpi City.

There is a blank line between test cases and the last test case is followed by a single integer zero.

OUTPUT FORMAT
For each test case, output the relative position of the places being queried. The system will output
Relative location cannot be determined if there is insufficient information; however the system
should attempt to use all necessary information to obtain a result.

SAMPLE INPUT
5 4
Naga City,City of Iriga,Northwest
Masbate City,Naga City,Southeast
Tabaco City,City of Ligao,Northeast
Sorsogon City,Legazpi City,Southeast
City of Ligao,Legazpi City,West
Naga City,City of Iriga
City of Iriga, Naga City
Sorsogon City, Naga City
Sorsogon City, City of Ligao

0

SAMPLE OUTPUT
Case 1
Naga City is northwest of City of Iriga
City of Iriga is southeast of Naga City
Relative location cannot be determined
Sorsogon City is southeast of City of Ligao

--
Copyright 2013, Caslon Chua and ACM ICPC Philippines Multi-Provincial Programming Contest
12 ACM ICPC Problem
Problem I: Interior Lattice Points
Input from file: pi.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Execution time limit: seconds
! lattice point is a point "#ose coordinates on a rectan$ular coordinate system are inte$ers. !n
interior lattice point is a lattice point t#at is inside a $iven poly$on and not on its %oundary. &or
example, t#e dra"in$ %elo" s#o"s a trian$le #avin$ six interior lattice points.
'rite a pro$ram t#at reads an input containin$ t#ree pairs of coordinates (x
A
, y
A
), (x
B
, y
B
), (x
C
, y
C
),
"#ere eac# coordinate is an unsi$ned inte$er "it# a value less t#an or e(ual to )**. +#e num%ers in
t#e line are separated exactly %y one space and are in t#e order x
A
y
A
x
B
y
B
x
C
y
C
. +#e coordinates
descri%e t#ree distinct (%ut possi%ly collinear) lattice points. If t#e $iven coordinates descri%e a trian$le
"it# non,-ero area, t#en t#e pro$ram s#ould display on t#e screen t#e num%er of interior lattice points
of t#e trian$le. Ot#er"ise, t#e pro$ram s#ould display on t#e screen t#e num%er -ero. (If t#e t#ree
points are collinear, t#en t#ere are no interior lattice points.)
INPUT FORMAT
+#e input starts "it# an inte$er N (* . N . /). +#is is follo"ed %y N input cases. Eac# input case is
a list of six unsi$ned inte$ers "it# values not exceedin$ )**. +#e six inte$ers x
A
y
A
x
B
y
B
x
C
y
C

correspond to t#e coordinates (x
A
, y
A
), (x
B
, y
B
), (x
C
, y
C
).
OUTPUT FORMAT
&or every input case, print t#e num%er of interior lattice points in one line.
SAMPLE INPUT
4
0 0 100 0 100 100
0 0 98 100 100 100
0 0 99 100 100 100
0 0 99 99 100 100
SAMPLE OUTPUT
4851
49
0
0
00
Copyri$#t 1 /*)2, Joel 3oc#e and !C4 IC5C 5#ilippines 4ulti,5rovincial 5ro$rammin$ Contest
ACM ICPC Problem 13
Problem J: Permutations
Input from file: pj.in
Output to console: stdout (in C), cout (in C++), System.out (in Java)
Eecution time limit: ! seconds
" permutation of a set is just an arran#ement of all t$e distinct elements of t$e set. %or eample, all t$e
permutations of t$e set & ', (, !) are #iven *elo+.
' ( !
' ! (
( ' !
( ! '
! ' (
! ( '
If eac$ permutation is re#arded as a num*er, t$e permutations in t$e a*ove list are arran#ed in
increasin# ma#nitude. ,$e permutations are said to *e listed in leico#rap$ic order. -ote t$at t$e
permutation in position . is ( ! ' +$ile t$e permutation in t$e last position, +$ic$ is /, is ! ( '.
%or anot$er eample, all t$e permutations of t$e set & b, e, i, n ) are listed in ta*ular form *elo+.
b e i n e b i n i b e n n b e i
b e n i e b n i i b n e n b i e
b i e n e i b n i e b n n e b i
b i n e e i n b i e n b n e i b
b n e i e n b i i n b e n i b e
b n i e e n i b i n e b n i e b
,$e permutations $ere are listed in dictionary order, +$ic$ is just anot$er +ay of descri*in#
leico#rap$ic order. -ote t$at t$e permutation in position . is b i n e +$ile t$e permutation in position
'0 is n b i e.
1iven a set of distinct di#its or letters, your tas2 is to determine t$e permutation of t$e set in a #iven
position +$en t$e permutations are assumed to *e listed in leico#rap$ic order.
Format of I/O and sample I/O are provided on the next page.
14 ACM ICPC Problem
INPUT FORMAT
,$e input consists of one or more input lines. Eac$ input line consists of a strin# t$at is composed
eit$er entirely of distinct letters or entirely of distinct di#its. ,$e letters or di#its are already arran#ed in
order. ,$e len#t$ of t$e strin# is at most 30 and is follo+ed *y a space and a num*er t$at #ives t$e
position of t$e re4uired permutation.
OUTPUT FORMAT
,$e output consists of one or more lines. Eac$ output line consists of t$e #iven input follo+ed *y t$e
e4ual si#n and follo+ed *y t$e permutation in t$e re4uired position. If t$e #iven position is *eyond t$e
total num*er of permutations, t$en t$e output s$ould indicate 5No permutation6.
SAMPLE INPUT
235 4
bein 20
123456 700
mnpqr 130
tuvwxyz 4000
SAMPLE OUTPUT
235 4 = 352
bein 20 = nbie
123456 700 = 651342
mnpqr 130 = No permutation
tuvwxyz 4000 = ywuxvzt
77
Copyri#$t 8 '03(, 9ernard :ar4ue; and "C: IC<C <$ilippines :ulti=<rovincial <ro#rammin# Contest
ACM ICPC Problem 15

You might also like