problem set 2013 acm icpc

20
7/23/2019 Problem Set 2013 Acm Icpc http://slidepdf.com/reader/full/problem-set-2013-acm-icpc 1/20 The  Asia Spo Spo Spo Spo Hosted b D 30 30 30 30 Y 013 ACM ICPC egional Contest Dhaka Site nsored by IBM nsored by IBM nsored by IBM nsored by IBM y North South University aka, Bangladesh h  November 2013 November 2013 November 2013 November 2013 u get 20 Pages u get 20 Pages u get 20 Pages u get 20 Pages 11 Problems 11 Problems 11 Problems 11 Problems & 300 Minutes 300 Minutes 300 Minutes 300 Minutes

Upload: sukanta60

Post on 19-Feb-2018

249 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 120

The

Asia

SpoSpoSpoSpo

Hosted b

D

30303030t tt t

YYYY

013 ACM ICPC

egional Contest

Dhaka Site

nsored by IBMnsored by IBMnsored by IBMnsored by IBM

y North South University

aka Bangladesh

hhhh November 2013November 2013November 2013November 2013u get 20 Pagesu get 20 Pagesu get 20 Pagesu get 20 Pages

11 Problems11 Problems11 Problems11 Problemsampampampamp

300 Minutes300 Minutes300 Minutes300 Minutes

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 220

2

983122983157983148983141983155 983142983151983154 983105983107983117983085983113983107983120983107 983090983088983089983091 983105983155983145983137 983122983141983143983145983151983150983137983148 983108983144983137983147983137 983123983145983156983141983098

a) Solutions to problems submitted for judging are called runs Each run is judged as accepted orrejected by the judge and the team is notified of the results Submitted codes should not contain

team or University name and the file name should not have any white space

b) Notification of accepted runs will NOT be suspended at the last one hour of the contest time to keepthe final results secret Notification of rejected runs will also continue until the end of the contest Butthe teams will not be given any balloon and the public rank list will not be updated in the last onehour

c) A contestant may submit a clarification request to judges If the judges agree that an ambiguity orerror exists a clarification will be issued to all contestants

d) Contestants are not to converse with anyone except members of their team and personnel designatedby the organizing committee while seated at the team desk But they cannot even talk with theirteam members when they are walking around the contest floor to have food or any otherpurpose Systems support staff may advise contestants on system-related problems such as explainingsystem error messages Coaches should not try to enter the contest area under any

circumstances

e) While the contest is scheduled for a particular time length (five hours) the contest director has theauthority to alter the duration of the contest in the event of unforeseen difficulties Should the contestduration be altered every attempt will be made to notify contestants in a timely and uniform manner

f) A team may be disqualified by the Contest Director for any activity that jeopardizes the contestsuch as dislodging extension cords unauthorized modification of contest materials distractingbehavior of communicating with other teams

g) Nine ten or eleven problems will be posed So far as possible problems will avoid dependence ondetailed knowledge of a particular applications area or particular contest language Of these problemsat least two will be solvable by a first year computer science student another two will be solvable by asecond year computer science student and rest will determine the winner

h) Contestants will have foods available in their contest room during the contest So they cannot leavethe contest room during the contest without permission The contestants are not allowed tocommunicate with any contestant (Even contestants of his own team) or coach while are outsidethe contest floor

i) Team can bring up to 200 pages of printed materials with them but they can also bring threeadditional books But they are not allowed to bring calculators mobile phones or any machine-readabledevices like CD DVD Pen-drive IPOD MP3MP4 players floppy disks etc

j) With the help of the volunteers the contestants can have printouts of their codes for debuggingpurposes Passing of printed codes to other teams is strictly prohibited

k) The decision of the judges is final

l) Teams should inform the volunteers if they donrsquot get reply from the judges within 10 minutesof submission Volunteers will inform the judges for further action Teams should also notify thevolunteers if they cannot log in into the PC^2 system This sort of complains will not beentertained after the contest

m) If you want to assume that judge data is weaker than what is stated then do it at your ownrisk )

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 320

A

In general small objects have less

areavolume compared to larger objec

volume is 1 So the ratio is 61 and for

the ratio is 610 Given the shape of

of gravitation on it

For simplicity we will assume the fol

problem

1 All ants are described as a box sshaped object is described with three i

which denotes the length width and

So the volume of the ant is ( W L timestimes

2 The density (Mass per unit volume)

the above mentioned ant is also ( Ltimes

weight is g H W L timestimestimes )( (Here

caused by gravitation)

3 When an ant freely falls four sides a

faces at the top and bottom are paral

So the area of the plane facing botto

For any ant the upward force put by t

to the area of the of the bottom face

proved that the downward acceleratio

solely on the value of H (as g is same

Falling AntsInput Standard Inpututput Standard Output

Ants make up 10 of the total world

total biomass of all the ants on Earth is

total biomass of all the people on Eart

the people on Earth when they fall from

die for two reasons

- They have so little mass relative to the

they fall slowly and therefore hav

dissipate when they hit the ground

- Their bodies are tiny deformable tan

absorb blows

impact of gravitation on them because they

ts For example consider a (1x1x1) cube Its s

a (10x10x10) cube the surface area is 600 and

any ants you will have to find out which ant h

lowing things in this

aped object A boxntegers L W and H

height of the object

) H

is 1 So the mass of

) H W times and so the

is the acceleration

re upright and so the

lel with the horizon

is always W Ltimes

e air is proportional

o be specific it is2

gW L timestimes

After some ma

n H

gg f

2minus= So the downward accelerati

or all ants)

animal tissue The

oughly equal to the

h However unlike

a height they do not

ir air resistance that

e little energy to

s well designed to

have more surface

rface area is 6 and

volume is 1000 So

s the highest effect

nipulation it can be

n actually depends

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 420

4

Given the dimension of several ants report the volume of the ant that has the highest downward

acceleration If there is a tie report the one with the largest volume

InputThe input file contains at most 500 test cases The description of each test case is given below

First line of each test case contains an integer T (T le 100) which denotes the total number of ants to

consider Each of the next T lines contains three integers which denote the value of L W and H (1 le L

W H le 50) of an ant respectively

Input is terminated by a line containing a single zero

OutputFor each set of input produce one line of output This line contains the volume of the ant that has the

highest downward acceleration If there is a tie report the ant with the highest volume

Sample Input Output for Sample Input3

3 4 5

12 1 5

20 10 4

3

3 4 5

20 30 5

1 2 4

0

60

3000

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 520

BM and J are playing a game The desc

several digits will be shown There ar

ith

button of a display once the value

displays has one fault in common T

button is broken

Each of these displays can be describ

the number of digits shown by the di

side (we can consider it like a 3-digit

Firstly it limits the number of times

display will be interpreted as a B-ba

numbers shown by the display will b

can press each of the three working

display is shown in Figure 1

Figure 1M and J have N of these displays Ea

playing goes like this

1 M plays first J plays second

2 In each move a player selects

only be selected if it hasnrsquot re

is a digit which can be selec

broken you can not choose thi

3 After a move if the summati

displays is divisible by 3 then

4 If there is no move possible th

Given description of N displays you

optimally Initially every digit of ever

Game of MJInput Standard Inpututput Standard Output

ription of the game is very simple They have a

buttons under each digit of every display If s

of the ith

digit of that display will increase by

e 0th

digit (least significant digit) canrsquot be in

d using two parameters L and B L is the len

play If L is 3 then the display can show thr

umber) B is the base of the display It is impo

you can press a button Secondly the numb

sed number with L digits Suppose B is 8 an

octal numbers and the length of the numbers

(not broken) buttons at most seven (7) times

A display with L = 4 and B = 8 h of the display has its own L and B Now th

fter that they alternate moves

a display Then selects a digit And presses its

ched its limit B-1 already A display can only

ed However since the switch for the rightm

button for any display

n of numbers (After converting it to decimal

he player making that move loses and the other

n the game ends in draw

need to find out the outcome of the game

display is set to 0 (zero)

few displays where

omeone presses the

one Each of these

creased because its

th of the display or

e (3) digits side by

tant in two aspects

er displayed in the

d L is 4 Then the

will be 4 Also you

Example of such a

game M and J are

button A digit can

be selected if there

ost digit is already

base) shown in the

player wins

and J both plays

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 620

6

InputFirst line of input consists of an integer T (T le 100) the number of test cases Each test case starts with

an integer N (0 lt N lt10) the number of displays Next N line each contains two integers L (0 lt L lt 109)

and B (1 lt B lt 109) which are the parameters that describes the i

th display

OutputFor each case print one line ldquoCase X Srdquo where X is the case number S is either ldquoMrdquo ldquoJrdquo or

ldquoDrawrdquo based on the outcome of the game in that case

Sample Input Output for Sample Input3 1 5 2 2 3 2 2 6 1 2 2

Case 1 M

Case 2 J

Case 3 Draw

Explanation

Case 1

00000 =gt 00010 =gt 01010 =gt 01110 =gt 11110 which is 30 in decimal and divisible by 3 So J loses and

M wins However this is one possible valid game sequence But if two players play optimally J will

always lose and thus M will always win

Case 2

(00000) =gt (00010) Sum = 0 + 6 = 6

(00000) =gt (10000) =gt (10010) =gt (11010) Sum = 6 + 6 = 12

(00000) =gt (10000) =gt (10010) =gt (10020) =gt (10030) =gt (10040) =gt (10050) =gt (11050) Sum = 6

+ 30 = 36

In this way in every game sequence M is doomed to reach such a configuration where the sum will be

divisible by 3 Hence in this scenario J will win and M will lose

Case 3

(00) =gt (10) which is 2 in decimal and not divisible by 3 There is no move left So the game ends in a

draw

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 2: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 220

2

983122983157983148983141983155 983142983151983154 983105983107983117983085983113983107983120983107 983090983088983089983091 983105983155983145983137 983122983141983143983145983151983150983137983148 983108983144983137983147983137 983123983145983156983141983098

a) Solutions to problems submitted for judging are called runs Each run is judged as accepted orrejected by the judge and the team is notified of the results Submitted codes should not contain

team or University name and the file name should not have any white space

b) Notification of accepted runs will NOT be suspended at the last one hour of the contest time to keepthe final results secret Notification of rejected runs will also continue until the end of the contest Butthe teams will not be given any balloon and the public rank list will not be updated in the last onehour

c) A contestant may submit a clarification request to judges If the judges agree that an ambiguity orerror exists a clarification will be issued to all contestants

d) Contestants are not to converse with anyone except members of their team and personnel designatedby the organizing committee while seated at the team desk But they cannot even talk with theirteam members when they are walking around the contest floor to have food or any otherpurpose Systems support staff may advise contestants on system-related problems such as explainingsystem error messages Coaches should not try to enter the contest area under any

circumstances

e) While the contest is scheduled for a particular time length (five hours) the contest director has theauthority to alter the duration of the contest in the event of unforeseen difficulties Should the contestduration be altered every attempt will be made to notify contestants in a timely and uniform manner

f) A team may be disqualified by the Contest Director for any activity that jeopardizes the contestsuch as dislodging extension cords unauthorized modification of contest materials distractingbehavior of communicating with other teams

g) Nine ten or eleven problems will be posed So far as possible problems will avoid dependence ondetailed knowledge of a particular applications area or particular contest language Of these problemsat least two will be solvable by a first year computer science student another two will be solvable by asecond year computer science student and rest will determine the winner

h) Contestants will have foods available in their contest room during the contest So they cannot leavethe contest room during the contest without permission The contestants are not allowed tocommunicate with any contestant (Even contestants of his own team) or coach while are outsidethe contest floor

i) Team can bring up to 200 pages of printed materials with them but they can also bring threeadditional books But they are not allowed to bring calculators mobile phones or any machine-readabledevices like CD DVD Pen-drive IPOD MP3MP4 players floppy disks etc

j) With the help of the volunteers the contestants can have printouts of their codes for debuggingpurposes Passing of printed codes to other teams is strictly prohibited

k) The decision of the judges is final

l) Teams should inform the volunteers if they donrsquot get reply from the judges within 10 minutesof submission Volunteers will inform the judges for further action Teams should also notify thevolunteers if they cannot log in into the PC^2 system This sort of complains will not beentertained after the contest

m) If you want to assume that judge data is weaker than what is stated then do it at your ownrisk )

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 320

A

In general small objects have less

areavolume compared to larger objec

volume is 1 So the ratio is 61 and for

the ratio is 610 Given the shape of

of gravitation on it

For simplicity we will assume the fol

problem

1 All ants are described as a box sshaped object is described with three i

which denotes the length width and

So the volume of the ant is ( W L timestimes

2 The density (Mass per unit volume)

the above mentioned ant is also ( Ltimes

weight is g H W L timestimestimes )( (Here

caused by gravitation)

3 When an ant freely falls four sides a

faces at the top and bottom are paral

So the area of the plane facing botto

For any ant the upward force put by t

to the area of the of the bottom face

proved that the downward acceleratio

solely on the value of H (as g is same

Falling AntsInput Standard Inpututput Standard Output

Ants make up 10 of the total world

total biomass of all the ants on Earth is

total biomass of all the people on Eart

the people on Earth when they fall from

die for two reasons

- They have so little mass relative to the

they fall slowly and therefore hav

dissipate when they hit the ground

- Their bodies are tiny deformable tan

absorb blows

impact of gravitation on them because they

ts For example consider a (1x1x1) cube Its s

a (10x10x10) cube the surface area is 600 and

any ants you will have to find out which ant h

lowing things in this

aped object A boxntegers L W and H

height of the object

) H

is 1 So the mass of

) H W times and so the

is the acceleration

re upright and so the

lel with the horizon

is always W Ltimes

e air is proportional

o be specific it is2

gW L timestimes

After some ma

n H

gg f

2minus= So the downward accelerati

or all ants)

animal tissue The

oughly equal to the

h However unlike

a height they do not

ir air resistance that

e little energy to

s well designed to

have more surface

rface area is 6 and

volume is 1000 So

s the highest effect

nipulation it can be

n actually depends

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 420

4

Given the dimension of several ants report the volume of the ant that has the highest downward

acceleration If there is a tie report the one with the largest volume

InputThe input file contains at most 500 test cases The description of each test case is given below

First line of each test case contains an integer T (T le 100) which denotes the total number of ants to

consider Each of the next T lines contains three integers which denote the value of L W and H (1 le L

W H le 50) of an ant respectively

Input is terminated by a line containing a single zero

OutputFor each set of input produce one line of output This line contains the volume of the ant that has the

highest downward acceleration If there is a tie report the ant with the highest volume

Sample Input Output for Sample Input3

3 4 5

12 1 5

20 10 4

3

3 4 5

20 30 5

1 2 4

0

60

3000

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 520

BM and J are playing a game The desc

several digits will be shown There ar

ith

button of a display once the value

displays has one fault in common T

button is broken

Each of these displays can be describ

the number of digits shown by the di

side (we can consider it like a 3-digit

Firstly it limits the number of times

display will be interpreted as a B-ba

numbers shown by the display will b

can press each of the three working

display is shown in Figure 1

Figure 1M and J have N of these displays Ea

playing goes like this

1 M plays first J plays second

2 In each move a player selects

only be selected if it hasnrsquot re

is a digit which can be selec

broken you can not choose thi

3 After a move if the summati

displays is divisible by 3 then

4 If there is no move possible th

Given description of N displays you

optimally Initially every digit of ever

Game of MJInput Standard Inpututput Standard Output

ription of the game is very simple They have a

buttons under each digit of every display If s

of the ith

digit of that display will increase by

e 0th

digit (least significant digit) canrsquot be in

d using two parameters L and B L is the len

play If L is 3 then the display can show thr

umber) B is the base of the display It is impo

you can press a button Secondly the numb

sed number with L digits Suppose B is 8 an

octal numbers and the length of the numbers

(not broken) buttons at most seven (7) times

A display with L = 4 and B = 8 h of the display has its own L and B Now th

fter that they alternate moves

a display Then selects a digit And presses its

ched its limit B-1 already A display can only

ed However since the switch for the rightm

button for any display

n of numbers (After converting it to decimal

he player making that move loses and the other

n the game ends in draw

need to find out the outcome of the game

display is set to 0 (zero)

few displays where

omeone presses the

one Each of these

creased because its

th of the display or

e (3) digits side by

tant in two aspects

er displayed in the

d L is 4 Then the

will be 4 Also you

Example of such a

game M and J are

button A digit can

be selected if there

ost digit is already

base) shown in the

player wins

and J both plays

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 620

6

InputFirst line of input consists of an integer T (T le 100) the number of test cases Each test case starts with

an integer N (0 lt N lt10) the number of displays Next N line each contains two integers L (0 lt L lt 109)

and B (1 lt B lt 109) which are the parameters that describes the i

th display

OutputFor each case print one line ldquoCase X Srdquo where X is the case number S is either ldquoMrdquo ldquoJrdquo or

ldquoDrawrdquo based on the outcome of the game in that case

Sample Input Output for Sample Input3 1 5 2 2 3 2 2 6 1 2 2

Case 1 M

Case 2 J

Case 3 Draw

Explanation

Case 1

00000 =gt 00010 =gt 01010 =gt 01110 =gt 11110 which is 30 in decimal and divisible by 3 So J loses and

M wins However this is one possible valid game sequence But if two players play optimally J will

always lose and thus M will always win

Case 2

(00000) =gt (00010) Sum = 0 + 6 = 6

(00000) =gt (10000) =gt (10010) =gt (11010) Sum = 6 + 6 = 12

(00000) =gt (10000) =gt (10010) =gt (10020) =gt (10030) =gt (10040) =gt (10050) =gt (11050) Sum = 6

+ 30 = 36

In this way in every game sequence M is doomed to reach such a configuration where the sum will be

divisible by 3 Hence in this scenario J will win and M will lose

Case 3

(00) =gt (10) which is 2 in decimal and not divisible by 3 There is no move left So the game ends in a

draw

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 3: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 320

A

In general small objects have less

areavolume compared to larger objec

volume is 1 So the ratio is 61 and for

the ratio is 610 Given the shape of

of gravitation on it

For simplicity we will assume the fol

problem

1 All ants are described as a box sshaped object is described with three i

which denotes the length width and

So the volume of the ant is ( W L timestimes

2 The density (Mass per unit volume)

the above mentioned ant is also ( Ltimes

weight is g H W L timestimestimes )( (Here

caused by gravitation)

3 When an ant freely falls four sides a

faces at the top and bottom are paral

So the area of the plane facing botto

For any ant the upward force put by t

to the area of the of the bottom face

proved that the downward acceleratio

solely on the value of H (as g is same

Falling AntsInput Standard Inpututput Standard Output

Ants make up 10 of the total world

total biomass of all the ants on Earth is

total biomass of all the people on Eart

the people on Earth when they fall from

die for two reasons

- They have so little mass relative to the

they fall slowly and therefore hav

dissipate when they hit the ground

- Their bodies are tiny deformable tan

absorb blows

impact of gravitation on them because they

ts For example consider a (1x1x1) cube Its s

a (10x10x10) cube the surface area is 600 and

any ants you will have to find out which ant h

lowing things in this

aped object A boxntegers L W and H

height of the object

) H

is 1 So the mass of

) H W times and so the

is the acceleration

re upright and so the

lel with the horizon

is always W Ltimes

e air is proportional

o be specific it is2

gW L timestimes

After some ma

n H

gg f

2minus= So the downward accelerati

or all ants)

animal tissue The

oughly equal to the

h However unlike

a height they do not

ir air resistance that

e little energy to

s well designed to

have more surface

rface area is 6 and

volume is 1000 So

s the highest effect

nipulation it can be

n actually depends

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 420

4

Given the dimension of several ants report the volume of the ant that has the highest downward

acceleration If there is a tie report the one with the largest volume

InputThe input file contains at most 500 test cases The description of each test case is given below

First line of each test case contains an integer T (T le 100) which denotes the total number of ants to

consider Each of the next T lines contains three integers which denote the value of L W and H (1 le L

W H le 50) of an ant respectively

Input is terminated by a line containing a single zero

OutputFor each set of input produce one line of output This line contains the volume of the ant that has the

highest downward acceleration If there is a tie report the ant with the highest volume

Sample Input Output for Sample Input3

3 4 5

12 1 5

20 10 4

3

3 4 5

20 30 5

1 2 4

0

60

3000

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 520

BM and J are playing a game The desc

several digits will be shown There ar

ith

button of a display once the value

displays has one fault in common T

button is broken

Each of these displays can be describ

the number of digits shown by the di

side (we can consider it like a 3-digit

Firstly it limits the number of times

display will be interpreted as a B-ba

numbers shown by the display will b

can press each of the three working

display is shown in Figure 1

Figure 1M and J have N of these displays Ea

playing goes like this

1 M plays first J plays second

2 In each move a player selects

only be selected if it hasnrsquot re

is a digit which can be selec

broken you can not choose thi

3 After a move if the summati

displays is divisible by 3 then

4 If there is no move possible th

Given description of N displays you

optimally Initially every digit of ever

Game of MJInput Standard Inpututput Standard Output

ription of the game is very simple They have a

buttons under each digit of every display If s

of the ith

digit of that display will increase by

e 0th

digit (least significant digit) canrsquot be in

d using two parameters L and B L is the len

play If L is 3 then the display can show thr

umber) B is the base of the display It is impo

you can press a button Secondly the numb

sed number with L digits Suppose B is 8 an

octal numbers and the length of the numbers

(not broken) buttons at most seven (7) times

A display with L = 4 and B = 8 h of the display has its own L and B Now th

fter that they alternate moves

a display Then selects a digit And presses its

ched its limit B-1 already A display can only

ed However since the switch for the rightm

button for any display

n of numbers (After converting it to decimal

he player making that move loses and the other

n the game ends in draw

need to find out the outcome of the game

display is set to 0 (zero)

few displays where

omeone presses the

one Each of these

creased because its

th of the display or

e (3) digits side by

tant in two aspects

er displayed in the

d L is 4 Then the

will be 4 Also you

Example of such a

game M and J are

button A digit can

be selected if there

ost digit is already

base) shown in the

player wins

and J both plays

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 620

6

InputFirst line of input consists of an integer T (T le 100) the number of test cases Each test case starts with

an integer N (0 lt N lt10) the number of displays Next N line each contains two integers L (0 lt L lt 109)

and B (1 lt B lt 109) which are the parameters that describes the i

th display

OutputFor each case print one line ldquoCase X Srdquo where X is the case number S is either ldquoMrdquo ldquoJrdquo or

ldquoDrawrdquo based on the outcome of the game in that case

Sample Input Output for Sample Input3 1 5 2 2 3 2 2 6 1 2 2

Case 1 M

Case 2 J

Case 3 Draw

Explanation

Case 1

00000 =gt 00010 =gt 01010 =gt 01110 =gt 11110 which is 30 in decimal and divisible by 3 So J loses and

M wins However this is one possible valid game sequence But if two players play optimally J will

always lose and thus M will always win

Case 2

(00000) =gt (00010) Sum = 0 + 6 = 6

(00000) =gt (10000) =gt (10010) =gt (11010) Sum = 6 + 6 = 12

(00000) =gt (10000) =gt (10010) =gt (10020) =gt (10030) =gt (10040) =gt (10050) =gt (11050) Sum = 6

+ 30 = 36

In this way in every game sequence M is doomed to reach such a configuration where the sum will be

divisible by 3 Hence in this scenario J will win and M will lose

Case 3

(00) =gt (10) which is 2 in decimal and not divisible by 3 There is no move left So the game ends in a

draw

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 4: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 420

4

Given the dimension of several ants report the volume of the ant that has the highest downward

acceleration If there is a tie report the one with the largest volume

InputThe input file contains at most 500 test cases The description of each test case is given below

First line of each test case contains an integer T (T le 100) which denotes the total number of ants to

consider Each of the next T lines contains three integers which denote the value of L W and H (1 le L

W H le 50) of an ant respectively

Input is terminated by a line containing a single zero

OutputFor each set of input produce one line of output This line contains the volume of the ant that has the

highest downward acceleration If there is a tie report the ant with the highest volume

Sample Input Output for Sample Input3

3 4 5

12 1 5

20 10 4

3

3 4 5

20 30 5

1 2 4

0

60

3000

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 520

BM and J are playing a game The desc

several digits will be shown There ar

ith

button of a display once the value

displays has one fault in common T

button is broken

Each of these displays can be describ

the number of digits shown by the di

side (we can consider it like a 3-digit

Firstly it limits the number of times

display will be interpreted as a B-ba

numbers shown by the display will b

can press each of the three working

display is shown in Figure 1

Figure 1M and J have N of these displays Ea

playing goes like this

1 M plays first J plays second

2 In each move a player selects

only be selected if it hasnrsquot re

is a digit which can be selec

broken you can not choose thi

3 After a move if the summati

displays is divisible by 3 then

4 If there is no move possible th

Given description of N displays you

optimally Initially every digit of ever

Game of MJInput Standard Inpututput Standard Output

ription of the game is very simple They have a

buttons under each digit of every display If s

of the ith

digit of that display will increase by

e 0th

digit (least significant digit) canrsquot be in

d using two parameters L and B L is the len

play If L is 3 then the display can show thr

umber) B is the base of the display It is impo

you can press a button Secondly the numb

sed number with L digits Suppose B is 8 an

octal numbers and the length of the numbers

(not broken) buttons at most seven (7) times

A display with L = 4 and B = 8 h of the display has its own L and B Now th

fter that they alternate moves

a display Then selects a digit And presses its

ched its limit B-1 already A display can only

ed However since the switch for the rightm

button for any display

n of numbers (After converting it to decimal

he player making that move loses and the other

n the game ends in draw

need to find out the outcome of the game

display is set to 0 (zero)

few displays where

omeone presses the

one Each of these

creased because its

th of the display or

e (3) digits side by

tant in two aspects

er displayed in the

d L is 4 Then the

will be 4 Also you

Example of such a

game M and J are

button A digit can

be selected if there

ost digit is already

base) shown in the

player wins

and J both plays

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 620

6

InputFirst line of input consists of an integer T (T le 100) the number of test cases Each test case starts with

an integer N (0 lt N lt10) the number of displays Next N line each contains two integers L (0 lt L lt 109)

and B (1 lt B lt 109) which are the parameters that describes the i

th display

OutputFor each case print one line ldquoCase X Srdquo where X is the case number S is either ldquoMrdquo ldquoJrdquo or

ldquoDrawrdquo based on the outcome of the game in that case

Sample Input Output for Sample Input3 1 5 2 2 3 2 2 6 1 2 2

Case 1 M

Case 2 J

Case 3 Draw

Explanation

Case 1

00000 =gt 00010 =gt 01010 =gt 01110 =gt 11110 which is 30 in decimal and divisible by 3 So J loses and

M wins However this is one possible valid game sequence But if two players play optimally J will

always lose and thus M will always win

Case 2

(00000) =gt (00010) Sum = 0 + 6 = 6

(00000) =gt (10000) =gt (10010) =gt (11010) Sum = 6 + 6 = 12

(00000) =gt (10000) =gt (10010) =gt (10020) =gt (10030) =gt (10040) =gt (10050) =gt (11050) Sum = 6

+ 30 = 36

In this way in every game sequence M is doomed to reach such a configuration where the sum will be

divisible by 3 Hence in this scenario J will win and M will lose

Case 3

(00) =gt (10) which is 2 in decimal and not divisible by 3 There is no move left So the game ends in a

draw

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 5: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 520

BM and J are playing a game The desc

several digits will be shown There ar

ith

button of a display once the value

displays has one fault in common T

button is broken

Each of these displays can be describ

the number of digits shown by the di

side (we can consider it like a 3-digit

Firstly it limits the number of times

display will be interpreted as a B-ba

numbers shown by the display will b

can press each of the three working

display is shown in Figure 1

Figure 1M and J have N of these displays Ea

playing goes like this

1 M plays first J plays second

2 In each move a player selects

only be selected if it hasnrsquot re

is a digit which can be selec

broken you can not choose thi

3 After a move if the summati

displays is divisible by 3 then

4 If there is no move possible th

Given description of N displays you

optimally Initially every digit of ever

Game of MJInput Standard Inpututput Standard Output

ription of the game is very simple They have a

buttons under each digit of every display If s

of the ith

digit of that display will increase by

e 0th

digit (least significant digit) canrsquot be in

d using two parameters L and B L is the len

play If L is 3 then the display can show thr

umber) B is the base of the display It is impo

you can press a button Secondly the numb

sed number with L digits Suppose B is 8 an

octal numbers and the length of the numbers

(not broken) buttons at most seven (7) times

A display with L = 4 and B = 8 h of the display has its own L and B Now th

fter that they alternate moves

a display Then selects a digit And presses its

ched its limit B-1 already A display can only

ed However since the switch for the rightm

button for any display

n of numbers (After converting it to decimal

he player making that move loses and the other

n the game ends in draw

need to find out the outcome of the game

display is set to 0 (zero)

few displays where

omeone presses the

one Each of these

creased because its

th of the display or

e (3) digits side by

tant in two aspects

er displayed in the

d L is 4 Then the

will be 4 Also you

Example of such a

game M and J are

button A digit can

be selected if there

ost digit is already

base) shown in the

player wins

and J both plays

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 620

6

InputFirst line of input consists of an integer T (T le 100) the number of test cases Each test case starts with

an integer N (0 lt N lt10) the number of displays Next N line each contains two integers L (0 lt L lt 109)

and B (1 lt B lt 109) which are the parameters that describes the i

th display

OutputFor each case print one line ldquoCase X Srdquo where X is the case number S is either ldquoMrdquo ldquoJrdquo or

ldquoDrawrdquo based on the outcome of the game in that case

Sample Input Output for Sample Input3 1 5 2 2 3 2 2 6 1 2 2

Case 1 M

Case 2 J

Case 3 Draw

Explanation

Case 1

00000 =gt 00010 =gt 01010 =gt 01110 =gt 11110 which is 30 in decimal and divisible by 3 So J loses and

M wins However this is one possible valid game sequence But if two players play optimally J will

always lose and thus M will always win

Case 2

(00000) =gt (00010) Sum = 0 + 6 = 6

(00000) =gt (10000) =gt (10010) =gt (11010) Sum = 6 + 6 = 12

(00000) =gt (10000) =gt (10010) =gt (10020) =gt (10030) =gt (10040) =gt (10050) =gt (11050) Sum = 6

+ 30 = 36

In this way in every game sequence M is doomed to reach such a configuration where the sum will be

divisible by 3 Hence in this scenario J will win and M will lose

Case 3

(00) =gt (10) which is 2 in decimal and not divisible by 3 There is no move left So the game ends in a

draw

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 6: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 620

6

InputFirst line of input consists of an integer T (T le 100) the number of test cases Each test case starts with

an integer N (0 lt N lt10) the number of displays Next N line each contains two integers L (0 lt L lt 109)

and B (1 lt B lt 109) which are the parameters that describes the i

th display

OutputFor each case print one line ldquoCase X Srdquo where X is the case number S is either ldquoMrdquo ldquoJrdquo or

ldquoDrawrdquo based on the outcome of the game in that case

Sample Input Output for Sample Input3 1 5 2 2 3 2 2 6 1 2 2

Case 1 M

Case 2 J

Case 3 Draw

Explanation

Case 1

00000 =gt 00010 =gt 01010 =gt 01110 =gt 11110 which is 30 in decimal and divisible by 3 So J loses and

M wins However this is one possible valid game sequence But if two players play optimally J will

always lose and thus M will always win

Case 2

(00000) =gt (00010) Sum = 0 + 6 = 6

(00000) =gt (10000) =gt (10010) =gt (11010) Sum = 6 + 6 = 12

(00000) =gt (10000) =gt (10010) =gt (10020) =gt (10030) =gt (10040) =gt (10050) =gt (11050) Sum = 6

+ 30 = 36

In this way in every game sequence M is doomed to reach such a configuration where the sum will be

divisible by 3 Hence in this scenario J will win and M will lose

Case 3

(00) =gt (10) which is 2 in decimal and not divisible by 3 There is no move left So the game ends in a

draw

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 7: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 720

C G

General Broken Arrow and Lieutenant

year they fought several wars toget

happily until one day the king of G

Shadow Coder wants to become the

become king of a country at the same

other in the Great Chaos Jam (GCJ) fi

Colonel Dragoon friend of both Bro

war and comes up with a great soluti

into two disjoint sets of cities called

Shadow Coder will rule the rest of th

with these divisions To make peoples

build a sub-country of Gigaland calle

Gigaland so that peoples from set A

and B are disjoint cities might not be

set B Broken Arrow and Shadow Cod

The country of Gigaland consists of

which connects two different cities an

every city is connected to each other

le K le min(N 21)) cities 1 2 hellip K set B

Though the warriors have agreed to

Each city of set A should be incident

be incident to even number (It can

Megaland should be minimum that i

cities of Megaland may or may not be

In terms of graph theory you are give

and B = K+1 K+2 hellip N You ha

each node in A should have odd degreof S is sum of all the edge cost in S

Given N M K and description of M

Megaland

me of ThroneInput Standard Input

Output Standard Output

General Shadow Coder are two great warriors

er like ICPC NCPC etc The peoples of Gi

igaland General Rem (RIP) died Now both

new king of Gigaland As this is not possible

time they leave it to the field of war They d

ld and the winner will become the new king

en Arrow and Shadow Coder wants to avoid

n According to his proposed solution the cou

A and B Broken Arrow will rule the cities i

e cities But the people of Gigaland will get f

remain calm and happy Dragoon gave another

d Megaland that consists of some cities and

nd B can have some access to other cities Th

disjoint between Megaland and set A and bet

r both agreed with the idea of Dragoon

(numbered from 1 to N) cities and M bidirec

each road has a cost to travel Gigaland was b

ith sequence of one or more roads Set A will c

umbered cities of Gigaland and rest N ndash K citi

uild the new sub-country Megaland they ha

o odd number of roads of Megaland and each

e zero also) of roads of Megaland The cost

sum of all the road cost of Megaland should

connected to each other

a weighted graph G = (N M) two set of nod

e to find the minimum cost sub-graph S (subs

e in S and each node in B should have even de

roads of Gigaland find the minimum possible

of Gigaland Every

galand were living

Broken Arrow and

for two persons to

cided to fight each

the blood shedding

try will be divided

n the set of A and

rustrated and angry

proposal They will

onnecting roads of

ugh cities of set A

een Megaland and

ional roads each of

uilt such a way that

onsists of first K (2

s will belong to the

e some conditions

city of set B should

of road system in

be minimized The

s A = 1 2 hellip K

et of edges) where

gree in S Here cost

cost of sub-country

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 8: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 820

8

InputInput starts with a positive integer T (T le 250) denoting the number of test cases Each case starts with

three integers N (2 le N le 100) M (N-1 le M le N(N-1)2) and K (2 le K le min(N 21)) Each of the next

M lines contains three integers u v and c (1 le uv le N u ne v 0 lt c lt 10000) meaning that there is a bi-

directional road between city u and v of cost c No two roads will be same Please also note that for 90 of the test cases K is less than 20

OutputFor each test case print the test case number (starting from 1) and the minimum possible cost of

Megaland if it is possible to build the sub-country according to the conditions described above otherwise

print ldquoImpossiblerdquo (without the quotes)

Sample Input Output for Sample Input2

7 7 4

1 7 101 5 1

2 5 2

3 6 3

4 6 1

5 6 4

3 7 8

4 4 3

1 2 1

1 3 1

2 4 1

3 4 1

Case 1 7

Case 2 Impossible

Warning The judge input file is around 25 MB So use faster IO functionsExplanation

For test case 1

In Megaland degree of each node in set A = 1234 is 1 and degree nodes in set B = 5 6 7 are 2 2

and 0 respectively

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 9: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 920

D

P

Mr Anderson is in a relationship wit

Andersons phone logs and texts to fin

he decided to put a pattern locker on hi

This pattern locker comes with a 9 dot

several dots to record a pattern The

recorded before If we assume that ea

than a sequence of digits The patter

once

Here is an example of such a pattern l

Even after recording a hard to crack p

that his girlfriend might try all possi

different pattern sequences are possib

recorded in patterns

You have to help Mr Anderson in cou

ttern LockerInput Standard Input

Output Standard Output

a very suspicious and jealous girlfriend She

d out he is up to Feeling that his private spac

s phone

s arranged on a 3x3 square grid by default One

n to unlock the phone one needs to replicat

h dot is assigned a unique number then a patt

locker requires that no digit appears in the

cker and some valid and invalid recorded patte

attern Mr Anderson doesnt feel quite comfor

le sequences to break his pattern He wants t

le for a given grid size minimum and maxim

nting the number of possible such sequences

is always checking

is getting violated

has to drag through

e the same pattern

rn is nothing more

equence more than

rn

able He is worried

o know how many

m numbers of dots

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 10: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1020

10

InputThe first line of the input will give the number of test cases T (1 le T le 10000) Then T test cases follow

in separate lines Each test case consists of three numbers L M N separated by a single space in between

two numbers The first number L (1 lt L le 100) denotes the number of rows and columns in the grid The

second number M (1le

Mle

LL) denotes minimum number of dots to be included in a pattern and thethird number N (M le N le LL) denotes the maximum number of dots to be included in the pattern

OutputFor each test case you need to print the test case number X in the format ldquoCase X rdquo This will be

followed by the count of possible sequences for the given grid size minimum and maximum number of

dots in a sequence Since the count can be pretty big you need to print the value of the count modulo

10000000000007 (1 followed by 12 zeros followed by 7)

Sample Input Output for Sample Input2

3 4 9

3 1 9

Case 1 985824

Case 2 986409

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 11: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1120

EYou have pearl of 3 types

bull Type 1 pearl can be of color fr

bull Type 2 pearl can be of color fr

bull Type 3 pearl can be of color fr

You have unlimited supply of pearls

But you have 2 more additional constr

bull

The total number of Type 1 anbull The total number of Type 2 an

Given A B X Y and Z calculate ho

they have different length or there is a

InputFirst line of the input contains T (1 le

A B X Y and Z All of these 5 intege

OutputFor each test case output an integer d

huge output the result modulo 100000

Sample Input5

1 1 1 1 1

2 3 1 1 1

1 1 1 2 1

10 10 10 10 10

100 100 100 100 100

earl ChainsInput Standard Inpututput Standard Output

m 1 to X

m X+1 to X+Y

m X+Y+1 to X+Y+Z

of each type and each color You want to buil

ints

Type 3 pearls will be exactly AType 3 pearls will be exactly B

many different pearl chains are possible 2 c

position in which they have different colored p

le 100) the number of test cases Each test cas

rs are between 1 and 1017

inclusive

noting the number of possible pearl chains Si

Output for Sample Inp3

25

5

77069

329672

some pearl chain

ains are different if

arl

contains 5 integers

nce the result is too

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 12: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1220

F Two PI

O

Itrsquos a common phenomenon to mix u

that they just look at sky and can tell y

whole way they travelled ldquoFirst we

and so this one is helliprdquo and thus end u

each other and they do not always run

ends up with some ridiculous answer a

The city we are talking about is a sq

Although the city is nice square shape

rail track and it is a straight one

perpendicular to the existing one The

or not they just need location of twconnecting these two stations the line

you have to build the stations in intege

So to sum it up you will be given co-

to give any two distinct integer co-or

co-ordinates may coincide with the gi

Input

First line of input will contain an integ

be a line of four integers X1 Y1 X2

ordinates of two stations on the alread

You may assume that these two points

OutputFor each test case produce one line

integers where the first two integers

integers are the x and y co-ordinate

possible value of S and the line pa

(may not necessarily intersect the

answers you may output any of the

coordinates does not exceed9

102times F

Sample Input2

4 4 5 5

9 0 10 0

oints Revisitednput Standard Inputtput Standard Output

direction when you go to a new place Some

ou which one is north And some dumb people

ere east facing then we turned left then right

with a direction But the roads are not always

from north to south or from east to west So thi

lmost all the times

uare shaped one with vertices at (0 0) (S 0)

one the road network is already messed up

o city corporation decides to build another

do not care if the new track intersects with th

rail stations so that if a rail track is built alowould be perpendicular to the existing rail tr

r co-ordinates

rdinates of two stations on the already existing

inates within the city for building new rail tra

en co-ordinates if you want it to

er T the number of test cases (T le 15000) For

2 (0 le X1 Y1 X2 Y2 le 109) Here (X1 Y1) and

existing rail track

are different and within the city

of output This line contains the case numbe

are the x and y co-ordinate of the first statio

of the second station They should be withi

sing through them must be perpendicular

previous track within the city) Since the

but you have to make sure that absolute va

or details please go through the explanation of t

Output for Sample ICase 1 1 0 0 1

Case 2 10 0 10 2

eople are so clever

try to remember the

then again righthellip

in 90 degrees with

kind of calculation

(S S) and (0 S)

ut there is only one

rail track which is

previous rail track

ng the straight lineck The problem is

rail track you have

ck One of the new

each case there will

(X2 Y2) are the co-

r followed by four

n and the next two

the city for any

o the existing line

re can be multiple

lue of any of these

he sample cases

put

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 13: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1320

13

Explanation

First Sample

In the first sample the existing rail track goes through points (4 4) and (5 5) If you establish two new

rail stations at (1 0) and (0 1) the rail track passing through them will be perpendicular to the existingrail track Note if you output something like (5 5) and (6 4) you will get Wrong Answer since if S = 5

point (6 4) will not be within the city But for any value of S such that (4 4) and (5 5) is within the city

points (1 0) and (0 1) will also be within the city

Second Sample

In this case there are many correct answers One of them is (10 0) and (10 2) Note that unlike first

sample here the rail tracks intersect

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 14: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1420

G

Watc

Day by day number of Kangaroos is d

Now for the convenience of the spect

ldquoKangaroo appears in Screen 7rdquo an

position of Kangaroos and inform th

screen to display that animal based on

as follows

If the position x is outside the rangeOtherwise the coverage is minimum(

An example will make it clear

Suppose there are four screens coveri

Kangaroo appears at x = 10

First screen has coverage of 3 unit aro

15 ndash 10) = minimum(3 5) = 3

Second screen has coverage of 0 unit

100)

Third screen has coverage of 0 unit

minimum(10 ndash 8 10 ndash 10) = 0

Fourth screen has coverage of 1 unit a

1 11 ndash 10) = 1

ing the KangarooInput Standard Input

Output Standard Output

ecreasing just like tiger whale or lions So I de

a sanctuary where they will live peac

visitors go near them So I planted s

outside the sanctuary For this proble

the sanctuary to be a long line of 10000

The leftmost position is marked with

position with 1000000000 There ar

cameras on this line Each of the camer

(L R) which means that camera coposition L to position R inclusive Each

their associated display screens outside

can see the Kangaroos

ators we announce time to time ldquoKangaroo a

so on I have some employees who contin

system (here position is a marker) The syste

the coverage The coverage of a screen for a

of that screen (ie x lt L or x gt R) then thndash L R ndash x)

ng the range (7 15) (14 100) (8 10) and (1

nd x = 10 Because x = 10 is within (7 15) an

around x = 10 Because x = 10 does not belo

around x = 10 Because though x = 10 is

round x = 10 Because x = 10 is within (1 11)

cided to make them

fully I do not let

me display screen

you may assume

0000 unit distance

and the rightmost

at most 100000

as is described with

vers a range fromof the cameras has

where the visitors

pears in Screen 3rdquo

ously monitor the

m chooses the best

osition x is defined

coverage is zero

11) Now say one

minimum(10 ndash 7

g to the range (14

within (8 10) but

and minimum(10 ndash

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 15: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1520

15

So which one is better Obviously the first one as it has the maximum coverage

So you are given the ranges of the screens and the positions the kangaroo appears For each position of

the Kangaroo you are to tell me the maximum coverage you can have with any of the screens

Input

First line of the test file contains T (T le 3) number of test cases Hence T cases follow For each case

you are given N M in the first line N is the number of screens and M is the number of Kangaroo

appearance ( 1 le N M le 100000) In the next N lines you are given the range of screens L R ( 0 le L le R

le 1000000000 ) Next M lines contain the position of the Kangaroo- an integer x (0 le x le 1000000000)

OutputFor each case print the case number Hence print M lines ith containing the maximum coverage you can

have around irsquoth Kangaroo

Sample Input Output for Sample Input1

3 2

7 15

14 100

1 11

10

120

Case 1

3

0

Warning The judge input file is around 6 MB So use faster IO functions

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 16: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1620

HGiven an integer N find how many pa

A le N

Here gcd (A B) means the greatest c

of the bitwise xor operation on the bin

InputThe first line of the input contains a

following T lines contain an integer N

OutputFor each test case print the case nu

input) followed by a space and then th

Sample Input2

7

20000000

Explanation

Sample 1

For N=7 there are four valid pairs (3

GCD XORInput Standard Inpututput Standard Output

irs (A B) are there such that gcd (A B) = A x

mmon divisor of the numbers A and B And

ary representation of A and B

n integer T(T le 10000) denoting the number

1 le N le 30000000)

ber first in the format Case X (here X

answer for that case

Output for Sample InpCase 1 4

Case 2 34866117

2) (5 4) (6 4) and (7 6)

or B where 1 le B le

xor B is the value

of test cases The

is the serial of the

ut

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 17: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1720

INatasha always mixes up things in her

classical problem - finding shortest p

that in another class the professor t

looking but does very different things

to the Primrsquos algorithm (actually w

Pseudocode of her algorithm looks as

FiascoInput Standard Input

Output Standard Output

algorithm classes Last week Prof Chhaya Mu

th to all nodes from a specific node of a wei

aught them Primrsquos and Dijkstrarsquos algorithms

Always confused poor Natasha submitted so

should call it Natasharsquos algorithm) she did

ollows

thy gave the class a

hted graph Before

which are similar

ething very similar

rsquot test it properly

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 18: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1820

18

After submission she showed the code to a friend who is good at algorithms That friend immediately

found the flaw Natasha was really terrified and started to cry Then there came the guys the guys who

liked her They jumped at the opportunity and got hold of the dataset to be used to test the solutions Your

friend Rehan is one of those guys He really wanted to impress Natasha He asked you to change the

dataset in such a way that Natasharsquos algorithm gives the right result After the change Rehan will

somehow be able to put the modified data with the old timestamp Rehan told you that

1 The dataset has T test cases

2 Each case contains a connected weighted undirected graph with n nodes and m edges

3 Each case will contain a source node source

4 Edge weights are positive and distinct

To avoid suspicion Rehan asked you not to change which vertices the edges connect or the overall set of

edge weights You may only reorder which weights are assigned to which edges

InputThe first line of the input denotes the number of datasets T (1 le T le 15) T sets of case will follow Eachcase will start with a triplet of numbers n (2 le n le 2500) m (1 le m le 25000) and source (1 le source le

n) - the number of nodes the number of edges and the starting node respectively Each of the next m

lines will contain a triplet of numbers (u v w) meaning that there is an edge between node u and node v

with weight w (1 le w le m) Nodes are numbered from 1 to n It is guaranteed that there is no duplicate or

self-edges in the input and the graph is connected In each given graph all edge weights will be distinct

OutputFor each set of input print one set of output First line of a set should be of the format ldquoCase Xrdquo

where X is the case number Then print each edge triplet - one triplet ( u v and w) in each line separated

by a single space The edges should be printed in the order given in the input If there is more than one

solution to a dataset any one of them will do

Sample Input Output for Sample Input1

7 9 5

2 4 2

1 4 8

7 2 6

3 4 7

5 7 5

7 3 9

6 1 1

6 3 4

5 6 3

Case 1

2 4 7

1 4 9

7 2 3

3 4 8

5 7 1

7 3 4

6 1 5

6 3 6

5 6 2

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 19: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 1920

J Drom

Lets first define some terms

bull A string is palindromic if it re

madam and toot

bull A string is a dromicpalin if we

dromicpalin string is mmaad becau

palindrome

bull A substring is any contiguous

a c i icp acmicpc but acpc

empty substring so that means there a

Given a string you have to figure out

InputThe first line of input is an integer T

containing a string The strings will c

be positive and not greater than 1000

OutputFor each case first output the case n

Follow the samples for exact format

Sample Input4

acmicpc

aaaaa

isyoursolutionfastenough

abbabababbaba

cpalin SubstringsInput Standard Input

Output Standard Output

ds the same forward and backward Examples

can rearrange its letters to make it a palindro

e we can rearrange the letters to make it

equence of characters of a string Some substri

is not a substring For this problem we are

e2

)1( +nn substrings of a string of length n

ow many of its substrings are dromicpalin

(T lt 100) indicating the number of test cases

ntain only lowercase letters [a - z] The lengt

mber followed by the number of substrings t

Output for Sample InpCase 1 8

Case 2 15

Case 3 24

Case 4 67

of palindromes are

e An example of a

adam which is a

ngs of acmicpc are

not considering the

Each case is a line

of each string will

at are dromicpalin

t

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t

Page 20: Problem Set 2013 Acm Icpc

7232019 Problem Set 2013 Acm Icpc

httpslidepdfcomreaderfullproblem-set-2013-acm-icpc 2020

KCan you fill an (abc) cuboid with e

cell inside the (abc) cuboid should

dimensions and for each positive integ

a (223) cuboid can be filled with fi

filled with twelve (111) cubes

To make this problem slightly more di

filled by another cube Your job is to f

the cuboid can be filled by exactly n c

InputThere will be at most 300 test cases

abc le 125 0 le m lt abc)

Each of the next m lines contains thre

(x y z) is already filled

No cell will be mentioned twice Ther

The input is terminated by a line conta

OutputFor each test case print the case num

single space before each number in the

Sample Input2 2 3 0

2 2 3 2

1 1 1

2 2 3

0 0 0 0

ill the CuboidInput Standard Input

Output Standard Output

actly n cubes The cubes may touch but cann

be covered by exactly one cube You may us

er p you can use as many (ppp) cubes as yo

e cubes four (111) cubes and one (222)

fficult m of the cells in the cuboid are already

ind out the possible values of n such that the re

ubes

ach test case begins with four integers a b c

integers x y z (1 le x le a 1 le y le b 1 le z le c)

will be at least one non-filled cell

ining four zeroes

ber and the list of possible answers in increasi

output Look at the output for sample input for

Output for Sample InpCase 1 5 12

Case 2 10

ot overlap and each

cubes of different

like For example

cube It can also be

illed and cannot be

maining cells inside

(2 le a b c le 20

that means the cell

ng order There is a

details

t