2023ICPC Problemset Regional
2023ICPC Problemset Regional
Problem Set
Please check that you have 12 problems that are spanned across 22 pages in total (including this
cover page).
Sam has some apricot seeds, and he wants to sort them in non-decreasing order based on size. He uses a unique
method to sort the apricot seeds, described as follows:
Given 𝑛 apricot seeds, Sam performs a total of 𝑛 − 1 steps to sort them. For each step 𝑘 from 1 to 𝑛 − 1:
- He compares the first seed with the second seed. If the second seed is smaller, he swaps their positions.
- He then compares the second seed with the third seed. If the third seed is smaller, he swaps their
positions.
- He continues this process until he compares the (𝑛 − 𝑘)-th seed with the (𝑛 − 𝑘 + 1)-th seed and
swaps their positions if the (𝑛 − 𝑘 + 1)-th seed is smaller.
Sam's friend Tom quickly realizes that this is the famous bubble sorting algorithm. To illustrate the inefficiency
of this algorithm to Sam, Tom decides to ask Sam 𝑞 questions. A question is represented as a tuple [𝑠, 𝑒, 𝑚, 𝑙, 𝑟].
For given a sequence of 𝑛 seeds, each question [𝑠, 𝑒, 𝑚, 𝑙, 𝑟] asks for the sum of the sizes of seeds from position
𝑙 to 𝑟 of the (partially) sorted subsequence after applying the first 𝑚 steps of Sam's method to the subsequence
of seeds from position 𝑠 to 𝑒 of the initial sequence.
For instance, consider four (𝑛 = 4) seeds with sizes of (1,3,4,2) and two (𝑞 = 2) questions [2,4,1,2,2] and
[1,4,2,3,4]. For the first question, the subsequence of the sizes from the second (𝑠 = 2) seed to the fourth (𝑒 =
4) seed is (3,4,2). After applying one step (𝑚 = 1) of Sam’s method, it becomes (3,2,4). The sum of the sizes
of seeds from the second position (𝑙 = 2) to the second position (𝑟 = 2) in this (partially) sorted subsequence
is 2. For the second question, the subsequence is (1,3,4,2). After applying two steps, it becomes (1,2,3,4). The
sum of the sizes of seeds from position 3 to 4 in this (partially) sorted sequence is 3 + 4 = 7.
Given a sequence of 𝑛 seeds and 𝑞 questions, write a program that computes the answer for each question.
Input
Your program is to read from standard input. The input starts with a line containing two integers, 𝑛 and 𝑞 (2 ≤
𝑛 ≤ 1,000,000, 1 ≤ 𝑞 ≤ 500,000), where 𝑛 represents the number of seeds and 𝑞 represents the number of
questions. The second line contains 𝑛 integers, separated by spaces, representing the sizes of the apricot seeds
in their initial order. Each size is between 1 and 109 , both inclusive. Each of the next 𝑞 lines contains five
positive integers 𝑠, 𝑒, 𝑚, 𝑙, 𝑟 of query [𝑠, 𝑒, 𝑚, 𝑙, 𝑟], separated by spaces, representing a question, where 1 ≤ 𝑠 <
𝑒 ≤ 𝑛, 1 ≤ 𝑚 ≤ 𝑒 − 𝑠, and 1 ≤ 𝑙 ≤ 𝑟 ≤ 𝑒 − 𝑠 + 1.
Output
Your program is to write to standard output. For each of the 𝑞 questions, output one line with the answer. The
answer for a question [𝑠, 𝑒, 𝑚, 𝑙, 𝑟] is the sum of the sizes of seeds from position 𝑙 to 𝑟 of the partially sorted
subsequence after applying the first 𝑚 steps of Sam's method to the subsequence of seeds from position 𝑠 to 𝑒
of the input sequence.
The following Python-like pseudo code for function BlackBox()takes a list of positive integers and shuffles
the integers in the list in a specific way, and returns the result as a list.
Three list methods are used below; For a list L, len(L)returns the number of items in L. L.append(x) adds
the item x to the end of L. L.pop(idx) removes the item at the specified index idx from the list L and
returns the removed item.
Given a list Z of positive integers, write a program to reconstruct a list I such that Z = BlackBox(I).
Apple.append( Banana[ 0 ] )
Pear = len( Apple ) - 1
Orange = Apple[ Pear ]
Lime = Apple[ 0 ]
Coconut = Orange % Pear
Melon = Apple[ Coconut ]
Apple[ 0 ] = Melon
Apple[ Coconut ] = Lime
return ( Apple )
# end of function BlackBox
Output
Your program is to write to standard output. Print 𝑛 integers of the list I where Z = BlackBox(I), one per
line; the i-th line should contain the i-th integer of I.
The following shows sample input and output for two test cases.
Problem C
Farm
Time Limit: 0.7 Seconds
There is a farm that borders a straight road. Suppose the road is on the 𝑥𝑥-axis. Each boundary edge of the farm
field is either horizontal or vertical. The leftmost and the rightmost edges are vertical and adjacent to the base
edge which lies on the road. The length of the base edge is equal to the sum of the lengths of all other horizontal
edges. See Figure C.1 (a).
(a) (b)
Figure C.1. A farm field and the pest infestation locations
In Figure C.1, the dots on the boundary or in the interior of the farm field represent the locations where the pests
have infested. To effectively eradicate the infestation, a farmer tries to divide the infested area into several
rectangular areas that satisfy the following conditions
Each rectangular area must be contained within the farm. It is allowed for the edges of a rectangle to
overlap the boundary of the farm.
Each edge of a rectangular area is either horizontal or vertical.
Rectangular areas are completely separated from each other, including their boundaries.
Each pest infestation location must be contained within one of the rectangular areas. It is allowed for a
pest infestation location to lie on an edge of a rectangle.
Figure C.1 (b) shows four rectangular areas covering all pest infestation locations. The farmer wants to minimize
the number of rectangular areas for efficient pest management.
Given the boundary of a farm and the pest infestation locations, write a program to compute the minimum
number of rectangular areas that satisfy the above conditions.
Input
Your program is to read from standard input. The input starts with a line containing two integers, 𝑚𝑚 (4 ≤ 𝑚𝑚 ≤
100,000) and 𝑛𝑛 (0 ≤ 𝑛𝑛 ≤ 100,000), where 𝑚𝑚 is the number of edges of the farm field and 𝑛𝑛 is the number of
the pest infestation locations. In the second line, 𝑚𝑚 integers 𝑣𝑣1 , 𝑣𝑣2 , ⋯ , 𝑣𝑣𝑚𝑚 (𝑣𝑣1 = 𝑣𝑣𝑚𝑚 = 0, 0 ≤ 𝑣𝑣𝑖𝑖 ≤ 106 ) are
given, which represent the 𝑥𝑥-coordinates of the vertical edges and the 𝑦𝑦-coordinates of the horizontal edges.
These vertical and horizontal edges are met alternately when traversing the upper boundary of the farm field
clockwise from the left end of the base edge to the right end. From the third line, each of the 𝑛𝑛 lines has two
integers 𝑥𝑥 and 𝑦𝑦, representing the coordinate (𝑥𝑥, 𝑦𝑦) of a pest infestation location. All locations are on the
boundary or in the interior of the farm field.
The following shows sample input and output for three test cases.
Problem D
Fraction
Time Limit: 0.5 Seconds
𝑏𝑏
A basic fraction can be represented by three integers (𝑎𝑎 𝑏𝑏 𝑐𝑐) which denotes 𝑎𝑎 + where 1 ≤ 𝑎𝑎, 𝑏𝑏, 𝑐𝑐 ≤ 9. An
𝑐𝑐
extended fraction has the form of (𝑎𝑎′ 𝑏𝑏 ′ 𝑐𝑐 ′ ) where 𝑎𝑎′ , 𝑏𝑏 ′ and 𝑐𝑐 ′ may be integers between one and nine or
other extended fractions. Note that a basic fraction is also an extended fraction, and the length of the fraction
is finite.
Given an extended fraction, we want to express its value as irreducible fraction. For example, the irreducible
fraction of �(1 2 4)(5 2 3)�4 3 (2 7 3)�� is as follows.
2
2 5+3 991
�1 + � + =
4 3 366
4+ 7
2+3
Given a string form of an extended fraction, write a program that converts the extended fraction into the
irreducible fraction.
Input
Your program is to read from standard input. The input starts with a line containing one integer 𝑛𝑛 (2 ≤ 𝑛𝑛 ≤
100), where 𝑛𝑛 is the number of symbols which are parentheses and digits between 1 and 9. The second line
contains symbols, separated by a space, which represent an extended fraction.
Output
Your program is to write to standard output. Print exactly one line. If the answer is 𝑥𝑥/𝑦𝑦, the line should
contain two integers 𝑥𝑥 and 𝑦𝑦, which are relatively prime to each other. Otherwise, (for example, when the
input is not valid) print -1. You will need 64-bit integers to get the correct answer.
The following shows sample input and output for three test cases.
𝐾-Lottery awards only one winner in each round. For each round, 𝐾! tickets are produced and each ticket
contains 𝐾 different numbers from 1 to 𝐾, and no two tickets are identical. Among the tickets produced each
round, 𝑀 tickets are sold. The draw is conducted as follows each round. While randomly generating 𝑁 (𝑁 ≥ 𝐾)
distinct numbers one by one, if the relative order of the last 𝐾 consecutive numbers matches the numbers on
any of the sold tickets, the draw ends immediately, and the corresponding ticket wins. Some rounds may not
have any winning tickets.
For instance, let's consider a round where 6 tickets are produced (𝐾 = 3). The ticket sequences produced are
(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), and (3, 2, 1). Among them, let's say (1, 2, 3) and (1, 3, 2) are
sold (𝑀 = 2). Let's assume that the following 10 random numbers (20, 35, 10, 7, 99, 53, 72, 33, 88, 16) are
scheduled to be generated (𝑁 = 10). Then the relative order of (7, 99, 53), say (1, 3, 2) matches the sold ticket
(1, 3, 2), so that ticket wins.
In another scenario, let's consider a round where 24 tickets are produced ( 𝐾 = 4). The ticket sequences
produced are (1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4) , ..., and (4, 3, 2, 1). Among them, let's assume (1, 2, 3, 4),
(1, 2, 4, 3), (3, 4, 1, 2), (4, 1, 2, 3), and (4, 2, 3, 1) are sold (𝑀 = 5). Let's assume that the following 10 random
numbers (19, 31, 9, 1, 89, 48, 63, 30, 78, 12) are scheduled to be generated (𝑁 = 10). Then the relative order
of (89, 48, 63, 30), say (4, 2, 3, 1) matches the sold ticket (4, 2, 3, 1), so that ticket wins.
Given information about a round of the 𝐾-Lottery, including the number of produced tickets, the number
sequences of the sold tickets, and the sequence scheduled to be randomly generated for the winning ticket, write
a program to output the number sequence of the winning ticket.
Input
Your program is to read from standard input. The input starts with a line containing three integers, 𝐾, 𝑀, and 𝑁
( 3 ≤ 𝐾 ≤ 10,000 , 1 ≤ 𝑀 ≤ min(𝐾!, 1,000) , 𝐾 ≤ 𝑁 ≤ 1,000,000 , 3 ≤ 𝐾𝑀 ≤ 100,000 ), where 𝐾 is the
number of numbers in each ticket, 𝑀 is the number of tickets sold, and 𝑁 is the number of numbers in the
randomly generated sequence of the round. In each of the following 𝑀 lines, 𝐾 integers of a ticket sold in the
round are given. The final line contains 𝑁 different positive integers 𝑁𝑖 (1 ≤ 𝑁𝑖 ≤ 100,000,000, 1 ≤ 𝑖 ≤ 𝑁)
which is the number sequence for determining a winner.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the number sequence
of the winning ticket. If there is no winning ticket, print 0.
The following shows sample input and output for three test cases.
The ICPC committee is planning a surprise event to cheer on the participating teams. The committee provides
each team with a pair of two numbers, 𝐴 and 𝐵 (𝐴 ≤ 𝐵), before the competition, which will be used for the
lucky draws after the competition. The committee wants to hold 𝐾 draws. In each draw, a single number 𝐶 is
chosen by the committee, and all teams with a pair (𝐴, 𝐵) such that 𝐴 ≤ 𝐶 ≤ 𝐵 win in this draw. To make
more teams happy, the committee wants to choose the 𝐾 numbers used in the 𝐾 draws in advance so that the
most teams win. A team can win multiple times but is considered to have won once.
For example, five teams are participating in ICPC and their pairs are (1, 2), (1, 4), (3, 6), (4, 7), (5, 6), and
𝐾 = 2. When the committee chooses two numbers 2 and 4, four teams with (1, 2), (1, 4), (3, 6) and (4, 7)
win. The team with (1, 4) wins twice because the pair contains both chosen numbers. In fact, all five teams
can win if 2 and 5 are chosen. The maximum number of winning teams is five.
Given 𝑛 pairs of two integers for teams and the number of lucky draws 𝐾, write a program to output the
maximum number of winning teams.
Input
Your program is to read from standard input. The input starts with a line containing two integers, 𝑛 and 𝐾
(1 ≤ 𝑛 ≤ 10,000, 1 ≤ 𝐾 ≤ 𝑛, 1 ≤ 𝑛 × 𝐾 ≤ 500,000), where 𝑛 is the number of teams and 𝐾 is the number
of lucky draws. Each of the following 𝑛 lines contains two integers 𝐴 and 𝐵 that represent the pair of a team,
where −106 ≤ 𝐴 ≤ 𝐵 ≤ 106 .
Output
Your program is to write to standard output. Print exactly one line. The line should contain the maximum
number of winning teams. Teams that win more than once should only be counted once.
The following shows sample input and output for four test cases.
Chansu and Junsu are friends in International College of Programming Convergence. One day, Chansu met
Junsu and said that “I’ll do a magic trick for you. Pick any number between 1 and 12, and don’t tell me your
number. Just keep it in your mind.” Junsu chose 11 in mind. Chansu then showed Junsu the following four
cards one by one, asking “Is there your number in this card?” at each time.
So, Junsu answered “Yes, yes, no, yes” in this order. After Chansu did some magically looking gestures with
his arms and legs for a while, he finally shouted, “I’ve got your number. It is 11.” And Junsu was quite
surprised because it was exactly the number he kept in mind.
Chansu didn’t tell Junsu the secret of the trick, but only “These cards have a great magic power, so they can
read your mind and tell me something only in a magical language, which only I can understand.”
How does this work? Can you figure out the secret?
Now, you are to write a program that answers the number in your friends’ minds. We can generalize the magic
trick as follows: You have 𝐾 magic cards in each of which exactly 𝑀 integers between 1 and 𝑁, possibly with
some redundancy, are written, and you perform the magic trick to 𝐹 friends. From the yes/no-sequences from
the 𝐹 friends, you will be able to pick out the correct numbers.
Input
Your program is to read from standard input. The input starts with a line containing four integers, 𝑁, 𝐾, 𝑀,
and 𝐹 (1 ≤ 𝑁 ≤ 500,000, 1 ≤ 𝐾 ≤ 100, 1 ≤ 𝑀 ≤ 5,000, 1 ≤ 𝐹 ≤ 50,000). In each of the following 𝐾 lines,
there are 𝑀 integers between 1 and 𝑁, which represent the 𝑀 numbers written in each magic card. In each of
the following 𝐹 lines, you are given a string of length 𝐾 over {Y, N}, which represents the answer of each
friend such that a Y means a “yes” and an N means a “no”. You can assume that all the answers from the
friends are correctly given according to their numbers chosen in mind.
Output
Your program is to write to standard output. Print exactly 𝐹 lines. For each 𝑖 = 1,2, … , 𝐹, the 𝑖-th line should
consists of the number in the 𝑖-th friend’s mind. If it is impossible to identify the one and only number of the
𝑖-th friend, print out 0 in the 𝑖-th line.
We are given a 2 × 𝑛𝑛 matrix 𝑀𝑀 of positive integers, and each row of 𝑀𝑀 does not contain duplicate numbers.
For 𝑖𝑖-th row 𝑟𝑟𝑖𝑖 of 𝑀𝑀, 𝑖𝑖 = 1, 2, we find the maximum sum 𝑠𝑠𝑖𝑖 of increasing subsequence contained in 𝑟𝑟𝑖𝑖 . For
example, if 𝑀𝑀 is given as the figure below, 𝑠𝑠1 is 1 + 2 + 3 + 4 + 5 + 6 and 𝑠𝑠2 is 2 + 3 + 5. We call 𝑠𝑠1 + 𝑠𝑠2
the maximum sum of increasing subsequences, MSIS.
Once we permute the columns of 𝑀𝑀, MSIS can change. For example, if we permute the columns of the above
matrix 𝑀𝑀 = [𝑐𝑐1 𝑐𝑐2 𝑐𝑐3 𝑐𝑐4 𝑐𝑐5 𝑐𝑐6 ] to [𝑐𝑐2 𝑐𝑐3 𝑐𝑐4 𝑐𝑐5 𝑐𝑐6 𝑐𝑐1 ] as the figure below, MSIS becomes 36.
Given a 2 × 𝑛𝑛 matrix 𝑀𝑀, write a program to output the maximum of MSIS among all possible permutations of
the columns of 𝑀𝑀.
Input
Your program is to read from standard input. The input starts with a line containing an integer, 𝑛𝑛 (1 ≤ 𝑛𝑛 ≤
10,000), where 𝑛𝑛 is the number of columns of the input matrix 𝑀𝑀. In the following two lines, the 𝑖𝑖-th line
contains 𝑛𝑛 positive integers of the 𝑖𝑖-th row of 𝑀𝑀, for 𝑖𝑖 = 1, 2. The integers given as input are between 1 and
50,000, and each row does not contain duplicate numbers.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the maximum of
MSIS among all possible permutations of columns of 𝑀𝑀.
The following shows sample input and output for two test cases.
There is only one railway line connecting (𝑛𝑛 + 1) cities developed along the coastline. When cities along the
coast are sequentially identified by numbers between 0 and 𝑛𝑛 , city (𝑖𝑖 − 1) and city 𝑖𝑖 (1 ≤ 𝑖𝑖 ≤ 𝑛𝑛) are
connected by rail, but other cities are not connected by rail.
Since every city except city 0 is famous as a tourist destination, every city 𝑖𝑖(1 ≤ 𝑖𝑖 ≤ 𝑛𝑛) excluding city 0 is
preparing a variety of goods to welcome travelers ahead of the tourist season. Worldwide famous goods BFB is
the most popular item in every city. However, the supplier of this product is located in city 0.
There is only one store that sells BFB in each city 𝑖𝑖(1 ≤ 𝑖𝑖 ≤ 𝑛𝑛). Let 𝑆𝑆𝑖𝑖 be the BFB specialty store in city 𝑖𝑖. In
each 𝑆𝑆𝑖𝑖 , the number of BFBs expected to be sold in the tourist season is analyzed and reported to the supplier
in the form of [𝑙𝑙𝑖𝑖 , 𝑚𝑚𝑖𝑖 ]. Here, 𝑙𝑙𝑖𝑖 and 𝑚𝑚𝑖𝑖 represent the minimum and the maximum number of expected required
products, respectively.
The BFB supply company in city 0 collects request information from stores in every city and supplies products
according to the rules described below.
Select a city, say city 𝑘𝑘(1 ≤ 𝑘𝑘 ≤ 𝑛𝑛). Then, take a train departing from city 0, travel to city 𝑘𝑘, and
supply BFBs only to the stores along the route. In other words, the BFB supplier supplies products to
𝑆𝑆1 , 𝑆𝑆2 , … , 𝑆𝑆𝑘𝑘 .
Let 𝑐𝑐𝑖𝑖 be the number of BFBs supplied to 𝑆𝑆𝑖𝑖 (1 ≤ 𝑖𝑖 ≤ 𝑘𝑘) while moving along the route, the condition
𝑐𝑐𝑖𝑖 ≤ 𝑐𝑐𝑖𝑖+1 (1 ≤ 𝑖𝑖 ≤ 𝑘𝑘 − 1) must be satisfied.
If the supplier supplies products according to the supply rules described above, it may be impossible for every
store to supply the desired number of items with a single supply procedure. Therefore, the supplier must go
through several supply procedures to deliver the products but must comply with the supply rules described
above each time. After completing all supply procedures, each 𝑆𝑆𝑖𝑖 will have at least 𝑙𝑙𝑖𝑖 and at most 𝑚𝑚𝑖𝑖 items.
For example, suppose 𝑛𝑛 = 4 and the number of items required by each store 𝑆𝑆𝑖𝑖 (1 ≤ 𝑖𝑖 ≤ 4) are [13,15], [5,8],
[6,14], and [3,7], respectively. In order for each store to supply the desired quantity of goods, there must be at
least two delivery procedures. In the first delivery procedure, 6 items can be supplied to each of the 4 stores.
Once delivery is completed in this first procedure, all stores' requests except 𝑆𝑆1 are satisfied. Since 6 items have
already been delivered to 𝑆𝑆1 , 𝑟𝑟(7 ≤ 𝑟𝑟 ≤ 9) additional products will be delivered to 𝑆𝑆1 in the second delivery
procedure. Of course, there may be other delivery methods. However, at least two delivery procedures are
required.
Write a program to calculate the minimum number of supply procedures in order to supply the number of BFBs
required by each store according to the above rules.
Input
Your program is to read from standard input. The input starts with a line containing an integer 𝑛𝑛 (1 ≤ 𝑛𝑛 ≤ 106 ),
where 𝑛𝑛 is the number of cities in which the BFB specialty stores locate. In the following 𝑛𝑛 lines, the 𝑖𝑖-th line
contains two integers 𝑙𝑙𝑖𝑖 and 𝑚𝑚𝑖𝑖 (1 ≤ 𝑙𝑙𝑖𝑖 ≤ 𝑚𝑚𝑖𝑖 ≤ 109 ) which indicate the minimum and the maximum number
of expected required products by 𝑆𝑆𝑖𝑖 .
The following shows sample input and output for three test cases.
Number theorist Dr. J is attracted by the beauty of numbers. When we are given a natural number 𝑎𝑎 =
𝑎𝑎1 𝑎𝑎2 ⋯ 𝑎𝑎𝑛𝑛 of 𝑛𝑛 digits and a natural number 𝑘𝑘, 𝑎𝑎 is called 𝑘𝑘-special if the product of all the digits of 𝑎𝑎, i.e. 𝑎𝑎1 ∙
𝑎𝑎2 ∙ 𝑎𝑎3 ⋯ 𝑎𝑎𝑛𝑛 is divisible by 𝑘𝑘. Note that the number 0 is always divisible by a natural number.
For example, if 𝑎𝑎 = 2349 and 𝑘𝑘 = 12, then the product of all the digits of 𝑎𝑎, 2 ∙ 3 ∙ 4 ∙ 9 = 216 is divisible by
𝑘𝑘 = 12, so the number 2349 is 12-special. If 𝑎𝑎 = 2349 and 𝑘𝑘 = 16, then the product of all the digits of 𝑎𝑎, 2 ∙
3 ∙ 4 ∙ 9 = 216 is not divisible by 𝑘𝑘 = 16, so the number 2349 is not 16-special.
Given three natural numbers 𝑘𝑘, 𝐿𝐿, and 𝑅𝑅, write a program to output 𝑧𝑧 % (109 + 7) where 𝑧𝑧 is the number of 𝑘𝑘-
special numbers among numbers in the range [𝐿𝐿, 𝑅𝑅].
Input
Your program is to read from standard input. The input has one line containing three integers, 𝑘𝑘, 𝐿𝐿, and 𝑅𝑅(1 ≤
𝑘𝑘 ≤ 1017 , 1 ≤ 𝐿𝐿 ≤ 𝑅𝑅 ≤ 1020).
Output
Your program is to write to standard output. Print exactly one line. The line should contain 𝑧𝑧 % (109 + 7)
where 𝑧𝑧 is the number of 𝑘𝑘-special numbers among the numbers in the range [𝐿𝐿, 𝑅𝑅], where both 𝐿𝐿 and 𝑅𝑅 are
inclusive in the range.
The following shows sample input and output for three test cases.
Tandem copy is an operation on a DNA where a consecutive sequence of one or more nucleotides is repeated,
and the repetitions are directly adjacent to each other; in other words, the tandem copy operation makes a copy
of a consecutive sequence of nucleotides and pastes the copy right after the copied sequence. For example,
𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 is resulted from the tandem copy of 𝐴𝐴𝐴𝐴𝐴𝐴 in 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴. Furthermore, we can continue another tandem
copy on the resulted sequence 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 and obtain 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴. The following example illustrates a series of
tandem copies from 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴, where the underlined sequence is copied at each step.
We say that 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 produces all these sequences by tandem copy. It is easy to see that 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 can produce
different sequences by selecting a different portion of the sequence to tandem copy at each step. Furthermore,
in principle, 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 can produce infinitely many sequences by continuing tandem copies as many as it needs.
Since it is easy to tandem copy a single nucleotide, it is practical for the ICPC lab to store sequences such that
two consecutive nucleotides in a sequence is always different; this helps the lab to reduce the storage space.
For instance, since 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 can be produced by tandem copying 𝑇𝑇 twice from 𝐴𝐴𝐴𝐴𝐴𝐴, it is better for the lab to
only store the shorter sequence 𝐴𝐴𝐴𝐴𝐴𝐴 instead of 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴.
Because of a recent budget cut, the ICPC lab can only perform the tandem copy on at most two nucleotides at
one time; namely, the length of a copied portion is at most two at each step. On the other hand, the lab can
continue to repeat the tandem copy as many as it desires. For example, given a sequence 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴, we can apply
the tandem copy operation on 𝐵𝐵 and obtain 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴, or apply it on the sequence 𝐵𝐵𝐵𝐵 and obtain 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴. But
we cannot tandem copy the consecutive sequence 𝐴𝐴𝐴𝐴𝐴𝐴 because its length is longer than two.
Given a source string 𝑠𝑠 and a target string 𝑡𝑡, your task is to count the number of all valid substrings 𝑠𝑠 ′ of 𝑠𝑠,
where one can obtain a string 𝑥𝑥 from 𝑠𝑠 ′ by applying an appropriate number of the tandem copy operations such
that 𝑥𝑥 contains 𝑡𝑡 as a substring. Please note that no two consecutive nucleotides in the source string are the same,
whereas two consecutive nucleotides in the target string can be the same. For example, 𝐶𝐶𝐶𝐶𝐶𝐶 or 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 cannot
be source strings, but they can be target strings.
It is easy to verify that the total number of valid substrings of 𝑠𝑠 is 14. Note that both the first 𝐶𝐶𝐶𝐶𝐶𝐶 and the
second 𝐶𝐶𝐶𝐶𝐶𝐶 in 𝑠𝑠 are counted as different valid substrings. Thus, you need to consider all substrings of 𝑠𝑠 and
count all valid substrings individually.
Here is another example. When 𝑠𝑠 = 𝐴𝐴𝐴𝐴 and 𝑡𝑡 = 𝐵𝐵𝐵𝐵, you can take the substring 𝐴𝐴𝐴𝐴 and tandem copy 𝐴𝐴𝐴𝐴. Then,
the resulting string is 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴, which contains 𝐵𝐵𝐵𝐵 as its substring. All other substrings of 𝑠𝑠 are unable to produce
𝐵𝐵𝐵𝐵 as a substring, and therefore the number of valid substrings is one.
Given a source string 𝑠𝑠 and a target string 𝑡𝑡, where no two consecutive characters in 𝑠𝑠 are the same character,
write a program that outputs the number of valid substrings 𝑠𝑠 ′ of 𝑠𝑠. 𝑠𝑠′ is a valid substring of 𝑠𝑠 if a series of
tandem copies on 𝑠𝑠′ can produce a string that contains 𝑡𝑡 as its substring, where a tandem copy is restricted to at
most two consecutive characters at each step.
Input
Your program is to read from standard input. The input consists of two lines. The first line is the source string
𝑠𝑠, and the second line is the target string 𝑡𝑡. Each input consists of uppercase letters 𝐴𝐴 to 𝑍𝑍, and 1 ≤ |𝑠𝑠|, |𝑡𝑡| ≤
2 × 104 .
Output
Your program is to write to standard output. Print exactly one line. The line should print the number of valid
substrings in which a series of tandem copies can produce a string that contains the target string as its substring.
The following shows sample input and output for four test cases.
A cycle 𝐶𝐶 with 𝑛𝑛 vertices is a graph such that the vertices 𝑖𝑖 and 𝑖𝑖 + 1, for 𝑖𝑖 = 1, … , 𝑛𝑛 − 1, are connected by an
edge and also the vertices 𝑛𝑛 and 1 are connected by an edge, where the vertices of 𝐶𝐶 are numbered 1 to 𝑛𝑛.
There are 𝑛𝑛 coins each of which is numbered as one of {1, 2, … , 𝑛𝑛} and the numbers of two coins can be same.
Initially, each vertex of 𝐶𝐶 has a coin among those coins. Then two vertices can swap their coins with each
other. For a walk 𝑤𝑤 = (𝑣𝑣1 , 𝑣𝑣2 , … , 𝑣𝑣𝑘𝑘 ) in 𝐶𝐶, a walk swapping is to swap two coins on 𝑣𝑣𝑖𝑖 and 𝑣𝑣𝑖𝑖+1 in the order
of 𝑖𝑖 = 1, 2, … , 𝑘𝑘 − 1. Here, a walk 𝑤𝑤 is a sequence of 𝑘𝑘(≥ 1) vertices whose consecutive two vertices are
different and adjacent in 𝐶𝐶, and it can be considered as the vertices visited when you traverse 𝐶𝐶. Also, 𝑘𝑘 is
called the length of 𝑤𝑤. For a walk of length 𝑘𝑘 ≥ 2, 𝑘𝑘 − 1 swaps in its walk swapping occur, and for a walk of
length one, there is no swap. The figure below shows the progress of the walk swapping for the walk
(2, 1, 4, 1) in the cycle with 4 vertices.
There are given the initial configuration of coins that the vertices of 𝐶𝐶 have in the first time and the final
configuration of coins that the vertices should have in the end. You have to find a walk swapping that results
in the final configuration of coins from the initial one, minimizing the total number of swaps of coins in the
walk swapping.
For example, in the above figure, the number of swaps of coins in the walk swapping of the walk (2, 1, 4, 1) is
3, however, the final configuration of coins is also achieved by the walk swapping of the walk (1, 2) with
only one swap.
Given the number of vertices of a cycle 𝐶𝐶 and the initial and final configurations of coins on the vertices,
write a program to output the minimum number of swaps of coins in a walk swapping resulting in the final
configuration of coins from the initial one.
Input
Your program is to read from standard input. The input starts with a line containing one integer 𝑛𝑛 (1 ≤ 𝑛𝑛 ≤
3,000), where 𝑛𝑛 is the number of vertices in a cycle 𝐶𝐶. The vertices are numbered from 1 to 𝑛𝑛, and the coins
on the vertices are numbered as ones of {1, 2, … , 𝑛𝑛} . The second line contains 𝑛𝑛 integers, allowed for
duplication, between 1 and 𝑛𝑛, where the 𝑖𝑖-th integer is the coin lying on the vertex 𝑖𝑖 in the initial configuration
of coins, for 𝑖𝑖 = 1, … , 𝑛𝑛. The third line contains 𝑛𝑛 integers, allowed for duplication, between 1 and 𝑛𝑛, where
the 𝑖𝑖-th integer is the coin lying on the vertex 𝑖𝑖 in the final configuration of coins, for 𝑖𝑖 = 1, … , 𝑛𝑛.
The following shows sample input and output for three test cases.