(acm international collegiate programming...

15
The 2003 27 th Annual acm International Collegiate Programming Contest World Finals sponsored by IBM Problem B Light Bulbs Input File: bulbs.in Hollywood’s newest theater, the Atheneum of Culture and Movies, has a huge computer-operated marquee composed of thousands of light bulbs. Each row of bulbs is operated by a set of switches that are electronically controlled by a computer program. Unfortunately, the electrician installed the wrong kind of switches, and tonight is the ACM’s opening night. You must write a program to make the switches perform correctly. A row of the marquee contains n light bulbs controlled by n switches. Bulbs and switches are numbered from 1 to n, left to right. Each bulb can either be ON or OFF. Each input case will contain the initial state and the desired final state for a single row of bulbs. The original lighting plan was to have each switch control a single bulb. However the electrician’s error caused each switch to control two or three consecutive bulbs, as shown in Figure 1. The leftmost switch (i = 1) toggles the states of the two leftmost bulbs (1 and 2); the rightmost switch (i = n) toggles the states of the two rightmost bulbs (n – 1 and n). Each remaining switch (1 < i < n) toggles the states of the three bulbs with indices i – 1, i, and i + 1. (In the special case where there is a single bulb and a single switch, the switch simply toggles the state of that bulb.) Thus, if bulb 1 is ON and bulb 2 is OFF, flipping switch 1 will turn bulb 1 OFF and bulb 2 ON. The minimum cost of changing a row of bulbs from an initial configuration to a final configuration is the minimum number of switches that must be flipped to achieve the change. n th switch first switch second switch third switch Figure 1 (n-1) st bulb n th bulb third bulb second bulb first bulb You can represent the state of a row of bulbs in binary, where 0 means the bulb is OFF and 1 means the bulb is ON. For instance, 01100 represents a row of five bulbs in which the second and third bulbs are both ON. You could transform this state into 10000 by flipping switches 1, 4, and 5, but it would be less costly to simply flip switch 2. You must write a program that determines the switches that must be flipped to change a row of light bulbs from its initial state to its desired final state with minimal cost. Some combinations of initial and final states may not be feasible. For compactness of representation, decimal integers are used instead of binary for the bulb configurations. Thus, 01100 and 10000 are represented by the decimal integers 12 and 16. Input The input file contains several test cases. Each test case consists of one line. The line contains two non-negative decimal integers, at least one of which is positive and each of which contains at most 100 digits. The first integer represents the initial state of the row of bulbs and the second integer represents the final state of the row. The binary equivalent of these integers represents the initial and final states of the bulbs, where 1 means ON and 0 means OFF.

Upload: vuongtram

Post on 31-Jul-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

The 2003 27th Annual acm International Collegiate

Programming Contest World Finalssponsored by IBM

Problem B Light Bulbs

Input File: bulbs.in

Hollywood’s newest theater, the Atheneum of Culture and Movies, has a huge computer-operated marquee

composed of thousands of light bulbs. Each row of bulbs is operated by a set of switches that are electronically

controlled by a computer program. Unfortunately, the electrician installed the wrong kind of switches, and tonight is

the ACM’s opening night. You must write a program to make the switches perform correctly.

A row of the marquee contains n light bulbs controlled by n switches. Bulbs and switches are numbered from 1 to n,

left to right. Each bulb can either be ON or OFF. Each input case will contain the initial state and the desired final

state for a single row of bulbs.

The original lighting plan was to have each switch control a single bulb. However the electrician’s error caused each

switch to control two or three consecutive bulbs, as shown in Figure 1. The leftmost switch (i = 1) toggles the states

of the two leftmost bulbs (1 and 2); the rightmost switch (i = n) toggles the states of the two rightmost bulbs

(n – 1 and n). Each remaining switch (1 < i < n) toggles the states of the three bulbs with indices i – 1, i, and i + 1.

(In the special case where there is a single bulb and a single switch, the switch simply toggles the state of that bulb.)

Thus, if bulb 1 is ON and bulb 2 is OFF, flipping switch 1 will turn bulb 1 OFF and bulb 2 ON. The minimum cost

of changing a row of bulbs from an initial configuration to a final configuration is the minimum number of switches

that must be flipped to achieve the change.

nth switchfirst switch second switch third switch

Figure 1

(n-1)st bulb nth bulbthird bulbsecond bulbfirst bulb

You can represent the state of a row of bulbs in binary, where 0 means the bulb is OFF and 1 means the bulb is ON.

For instance, 01100 represents a row of five bulbs in which the second and third bulbs are both ON. You could

transform this state into 10000 by flipping switches 1, 4, and 5, but it would be less costly to simply flip switch 2.

You must write a program that determines the switches that must be flipped to change a row of light bulbs from its

initial state to its desired final state with minimal cost. Some combinations of initial and final states may not be

feasible. For compactness of representation, decimal integers are used instead of binary for the bulb configurations.

Thus, 01100 and 10000 are represented by the decimal integers 12 and 16.

InputThe input file contains several test cases. Each test case consists of one line. The line contains two non-negative

decimal integers, at least one of which is positive and each of which contains at most 100 digits. The first integer

represents the initial state of the row of bulbs and the second integer represents the final state of the row. The binary

equivalent of these integers represents the initial and final states of the bulbs, where 1 means ON and 0 means OFF.

The 2003 ACM Programming Contest World Finals sponsored by IBM

To avoid problems with leading zeros, assume that the first bulb in either the initial or the final configuration (or

both) is ON. There are no leading or trailing blanks in the input lines, no leading zeros in the two decimal integers,

and the initial and final states are separated by a single blank.

The last test case is followed by a line containing two zeros.

OutputFor each test case, print a line containing the case number and a decimal integer representing a minimum-cost set of

switches that need to be flipped to convert the row of bulbs from initial state to final state. In the binary equivalent

of this integer, the rightmost (least significant) bit represents the nth switch, 1 indicates that a switch has been

flipped, and 0 indicates that the switch has not been flipped. If there is no solution, print “impossible”. If there is

more than one solution, print the one with the smallest decimal equivalent.

Print a blank line between cases. Use the output format shown in the example.

Sample Input Output for the Sample Input 12 16 1 1 3 0 30 5 7038312 7427958190 4253404109 657546225 0 0

Case Number 1: 8

Case Number 2: 0

Case Number 3: 1

Case Number 4: 10

Case Number 5: 2805591535

Case Number 6: impossible

Problem A: Gopher II

The gopher family, having averted the canine threat, must face a new predator.

The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does

not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. All the

gophers run at the same velocity v. The gopher family needs an escape strategy that minimizes the number of

vulnerable gophers.

The input contains several cases. The first line of each case contains four positive integers less than 100: n, m,

s, and v. The next n lines give the coordinates of the gophers; the following m lines give the coordinates of

the gopher holes. All distances are in metres; all times are in seconds; all velocities are in metres per second.

Output consists of a single line for each case, giving the number of vulnerable gophers.

Sample Input

2 2 5 10

1.0 1.0

2.0 2.0

100.0 100.0

20.0 20.0

Output for Sample Input

1

Olympiad of Informatics

Kingdom of Magic

ngdom of Magic has had a network of bidirectional magic portals between cities since ancient times. Each portalmagically connects a pair of cities and allows fast magical communication and travel between them. Cities thatare connected by a magic portal are called neighboring.

Prince Albert and Princess Betty are living in the neighboring cities. Since their childhood Albert and Betty werealways in touch with each other using magic communication Orbs, which work via a magic portal between thecities.

Albert and Betty are in love with each other. Their love is so great that they cannot live a minute without eachother. They always carry the Orbs with them, so that they can talk to each other at any time. There is somethingstrange about their love — they have never seen each other and they even fear to be in the same city at the sametime. People say that the magic of the Orbs have affected them.

Traveling through the Kingdom is a complicated affair for Albert and Betty. They have to travel through magicportals, which is somewhat expensive even for royal families. They can simultaneously use a pair of the portals tomove to a different pair of cities, or just one of them can use a portal, while the other one stays where he or she is.At any moment of their travel they have to be in a neighboring cities. They cannot simultaneously move throughthe same portal.

Write a program that helps Albert and Betty travel from one pair of the cities to another pair. It has to find thecheapest travel plan — with the minimal number of times they have to move though the magic portals. When they

move through the portals simultaneously it counts as two moves.

InputThe input consists of the description of a series of test cases, terminated by the end of the file. The first line of eachtest case contains integer numbers n, m, a1, b1, a2, b2. Here n (3 ≤ n ≤ 100) is a number of cities in the Kingdom(cities are numbered from 1 to n); m (2 ≤ m ≤ 1000) is a number of magic portals; a1, b1 (1 ≤ a1, b1 ≤ n, a1 6= b1)are the neighboring cities where Albert and Betty correspondingly start their travel from; a2, b2 (1 ≤ a2, b2 ≤ n,a2 6= b2) are the neighboring cities where Albert and Betty correspondingly want to get to (a1 6= a2 or b1 6= b2).

The following m lines describe the portals. Each line contains two numbers pi1 and pi2 (1 ≤ pi1, pi2 ≤ n, pi1 6= pi2)— cities that are connected by the portal. There is at most one portal connecting two cities.

OutputPrint out one line per test case, each containing one number c, the minimal number of moves in the travel plan forthat test case.

Sample input and output

4 5 1 2 2 1

1 2

2 3

3 4

4 1

1 3

3

Page 1 of 1

2487 - Lollies

Oceania - South Pacific - 2002/2003

Every day on his way home, little Billy passes by his great aunt Clara Mitchum's house. Generally he stops in

for a chat with the great ACM (as he lovingly refers to her) and sometimes he asks for some lollies. When he

does, she generally gives him some, but then adds now don't be asking for any more for another N days where

N is some positive integer. If N = 1 that means he can ask for some on the next day, but for example if it is

April 6 and N = 4 then he must wait until April 10 or later before asking for more lollies.

One day Billy happened to catch sight of the great ACM's calendar, and noted that each day was marked with

two integers. He also noted that the first of these referred to the number of lollies the great ACM would give

him on a particular day, and the second to the delay that would then be required before making another

request. He copied down as much of the information as he could, and has passed it to you to analyse. His

objective, of course, is to get as many lollies as he can.

Your task is to write a program which will report the total number of lollies that can be obtained by Billy, and

provide a schedule for obtaining that amount. In the event that there are two or more ways to obtain the

maximum number of lollies, Billy will choose the one where his first collection is as late as possible, and

among all collections with that first date, his second collection is as late as possible, and so on.

Input

The input text consists of a number of sets of unrelated problems. The first line of a set is a problem title

consisting of a string of 1 to 20 letters. A single `#' on a line indicates the end of input.

The title line is followed by a sequence of day lines. Each problem set contains between 1 and 100 days,

including the limits. In the given order, the first day line corresponds to day number 1, the second line to day

number 2, the n-th line to day number n. Each day line consists of two integers separated by a single space:

an integer L, which is the number of lollies available on that day ( 1 L 100),•

an integer N, which is the associated delay ( 1 N 100).•

Conventionally, a delay N pointing to a day beyond the end of the current problem refers to a day with zero

lollies and zero further delays ( L = 0, N = 0).

Output

Each report must follow the following format (use single spaces for spacing):

In problem total lollies can be obtained:

On day day collect day lollies.

On day day collect day lollies.

...

2487 - Lollies 1/2

In this notation, problem represents the actual problem title, total , day , and

day are numbers with self-described meaning, and lollies stands for either `lolly' or `lollies',

as required by the context (the singular and plural forms must be used appropriately). Days must be given in

increasing sequence numbers. Each group report should be separated from the next by a blank line.

Sample Input

January

1 1

2 2

3 3

February

10 3

7 1

5 2

1 1

March

2 3

1 1

3 7

2 7

#

Sample Output

In January 4 lollies can be obtained:

On day 1 collect 1 lolly.

On day 3 collect 3 lollies.

In February 12 lollies can be obtained:

On day 2 collect 7 lollies.

On day 3 collect 5 lollies.

In March 4 lollies can be obtained:

On day 2 collect 1 lolly.

On day 3 collect 3 lollies.

South Pacific 2002-2003

2487 - Lollies 2/2

����� ����� ������������������������������������ �������!"!"�����#�����$���%&�(')�*+',-/.103254603768�9�:<;>=�?A@B7C0D4C0�EGF

HJI�K�L5MON�P>QSRTMOUWVIXN�KZY�I\[�]^U_KTN�P`HaU_K�L5IXb<L�c�d�$eS��f! � gi� ���J

o 25.qp5F�r1st8Du&�»z|� ��yBz|{}~p5FC.5p5F�r1st8Du&�»z|����y����5�

� 7603�D7�����rOs�83u&�¹z|����y��qwq��0D7��»z|���ÜyB�|�<�Âk�58���� � ?t�D�<F � ?t.18�st?t258���0D�>.O��2<;+46FB��7CF68�9`F60.57C8�.1��7C8�:18�2<Fk.5?t.O8�4�EG037�F6�58�258�¥áFC7���2q46�¤��sA�D@BFC?�@_st?��3�<F�.5?t.18�s�?t2583�

� p576?t25�`F6�58�9<8�4C?t�D2S.5�1��4C8$0�EWF6�q8�258�¥ .q?�.O846�1��.18F6�58�@B03�>.1��2¤;`7���2©?t2<F60`F6�58.57603:5st8��(0�E�9<8�FC8�76�`?t25?t25���50�¥ EÐ��7F6�q8+st?��3�<F�@���2�768��3@6�Ñ?�2q46?A9<8�8��D@C�n@B03�>.O0D258�2<Fk0�EÜF6�q8+.5?t.183� Ó 03F68�F6�O��FkF6�q8+����F68�7C?���s1¥k�q?�@C��F6�q8+.5?t.18�?t4k���39<8kE�760D�?t4k250DFkFC7���2546.1��768�2<F���219�250DFkst?t�D�<F�768BÈ18|@BF6?t25�O�

� � � � � � � � � ��� � � � ��

� � � � � � � � � � � � � � ��

��

�����������

��� � ����� ��� �������

��� � �����

¨��3@C�>.5?t.O8ê@�0D�>.O0D2q8�2<FW@B0D2q46?t46FC4k0�EÜ����2<;+4CF67���?t�D�<FW.q?�.O8�4T@B0325258�@�F68�9`F6?t�3�¤FCst;$FC0D�D8�F6�58�7|� � 0D7�F6�58�.57C0D�37����`�>?³25�.5p57C.103468�4|�5FC�58 @B03�>.1��2<;ê9/8��D8�st03.18�9�F6�58�9<8�4B@B76?t.5FC?�032>0�EX8��D@C��@�0D�>.O0D2q8�2<FW��4W�ê468��3p58�2O@B8+0�E».O0D?t2<F64 � � � � � �6� ��� � �����6�à|à�à � ��� �������6�»¥k�58�7C8 � � � ��� ��à�à|à ���¹��Âk�q8�468>��768�F6�q8$p5.q.18�7f.103?�2<FC4T0�E�F6�58�.5?t.O8�@�0D2<F603p57��fÂk�58$:O0DFCF603�Á.103?�2<FC40�EkF6�q8�.5?t.18>@B032<F60Dpq7ê@�0D254C?�4CF$0�EW.O0D?t2<F64+¥k?tF6� � 6@B0<0D7B9<?t21��FC8>9<8�@B7C8���4C8�9Ë:<;ʦD��Â�0�8��3@6�Jpq.5.18�7+.103?t2¤F ��� �������WFC�58�768?t4 �`@B0D7C768�4C.103219<?t25�>:O0DFCF603�Á.103?�2<F � � � � �! ¦��kÍG4C8�8.5?A@BF6p57C8���:10��38�Ï��_Âk�58�@�0D�>.O��2<;�¥���2<F64TFC0�r12O9»�qEG0D7k8��3@C�©.5?t.O8@B03�>.O0D258�2<F|��F6�58k.O0D?t2<F�¥k?tF6�����Ô5?��>��s ��6@B0<0D7B9<?t21��FC8kF6�1��F�F6�q8ks�?t�3�¤F�¥k?tsts<768��3@6�&��Âk�58kst?��3�<FÜ?t4�8��`?�FCF68�9f:<;��~4C8��D�`8�2<F4603p57�@�8�¥k?�FC�n8�219/.103?�2<FC4 ����� ��� ¦"�W��219 ����� ���#�kÍG8�219<.O0D?t2<F64+��7C8$8��`?tF6F6?t25��st?t�D�<F~F60<0¤Ï����T464Cp5�>8F6�O��FTFC�58�st?t�D�<Fk?t42503Fk:18�2<FT��FkF6�58+.q?�.O8�:18�2¤Fk.O0D?t2<F64T��219`F6�58�:O8�2<FT.O0D?t2<F64T9<02503Fk46F603.�F6�58+st?t�D�<F�:O8����©�å�æ�ç�è�éÂk�58f?�2q.5p5FkrOs�8+@B032<F���?t254~468��38�7���s�:5st0/@C�<4T8|�D@C�©9<8�4B@B76?t:5?t25�`0D258f.5?t.18$@�0D�>.O0D2q8�2<F��W¨d�D@C�n:5st05@C�>46FB��7CF64�¥k?tF6��F6�58

2<p5��:O8�7k0�EW:O8�2<F�.103?t2¤FC4 « � ý �á«D¬�0D2n4C8�.1��7B��FC8$st?t258D��¨��D@C�n0�EWF6�q8+258BÔ5F+ýSst?�2q8�4 @B032<F���?t254���.1��?t7T0�E�7C8���s��D��stp58�4� � �*� � 4C8�.1��7���FC8�9�:<;>4C.1�D@�8D��Âk�58�sA��4CFk:5st05@6��?t4T9<8�250DFC8�9�¥k?tF6��ý # ¬5�þèÜé|ç�è�éÂk�58~0Dp5FC.5p5Fdr1st8 @B032<F���?t254�st?t258�4k@B0D7C768�4C.103219<?t25�F60:5st0/@C�<4�?�2�?t25.qp5F�rOs�83��ÂÜ0f8��D@C�>:qs�05@C�?�2`F6�58~?t25.5p5Fdr1st8TFC�58�768

?t4�0D258�st?�2q8W?t2F6�58�0Dp5FC.5p5F�rOs�83�ܨ��3@C�$4Cp1@C��s�?t258k@B032¤FB��?t254�8�?tF6�q8�7W�~768���s5�D��stp583��¥k7C?tF6F68�2�¥k?�FC�$.q768�@B?t4C?�032$0�E¹F^¥�0+9<8�@B?t����s.5sA�D@�8�4��q0D7kF6�q8+�>8�4C4����D8�$ � �5���&% �âw('('Î� ���J�»z|� ��y���Âk�58�768|��sX�D��stp58�?�4�F6�q8$9<8�4C?�7C8�9����|Ô5?t���|s � �@�0¤037�9<?t21��F68�0�E�F6�58.O0D?t2<FT¥k�58�7C8�F6�58st?t�D�<FT@���2S768|�D@C�nE�760D��F6�q8$4C0Dp57B@B8EG0D7�@B0D7C768�4C.103219<?t25�n.q?�.O8$@B03�>.O0D258�2¤F|� o E�F6�5?t4��D��stp58+8 �DpO��st4TFC0� � �qF6�58�2�FC�58+�`8�464B���D8)$ � �5���&% � w('*' � ���J�»z|����y ¥k?tstsX��.5.O8���7k?�2�FC�58�0Dp5FC.5p5F�r1st8D�ÿ������Jç���o 25.qp5F�r1st8�� �� �

� �+��+� �

��� � y +� � � y ������ � y � �� ��� � � y �� ��� � + y ����

}~p5FC.5p5FkrOs�8� y + �$ � �5���&% �áw('*'â� ���J�»z�����y

2589 - Twenty Questions

North America - North Central - 2002/2003

In the game of ``twenty questions" I think of an item (like a fish) from a set of N items, and you get to ask me

at most twenty questions that can only be answered ``yes" or ``no" to identify the item. For example, you

might ask ``Is it living?" If I answer ``yes", then you might ask, ``Does it have fur?" If I answer ``no", then

you might then ask, ``Does it have fins?" This continues until you either guess the item (in which case you

win), or youve asked twenty questions without identifying the item (in which case I win).

With just 20 questions you could identify any one of 524,288 items, assuming you can distinguish among

them by asking 19 ``yes/no" questions and then with your 20th question ask, ``Is it X?" Of course it might take

fewer than 20 questions if you have a good idea about the identity of the item, but in this problem well assume

all the questions are used.

Suppose you could ask questions that could be answered with more than just a ``yes" or ``no". For example,

suppose you could ask, ``Does it weight less than, equal to, or greater than 10 pounds?" This question has

three possible answers. Then how many questions would you need to ask in order to distinguish among N

items?

In this problem you will be told how many different answers can be given to each of your questions, and the

number of items in the set of possible choices for the item. All you need to do is determine the maximum

number of questions that must be asked to identify the item. This assumes that your questions are chosen in

such a way as to divide the remaining candidates for the item into suitably sized groups.

Input

There will be multiple cases to consider. For each case there will be two integers in the input. The first

integer, K , is the number of possible answers to each question (no larger than 10). The second integer, N , is

the number of items in the set of possible choices (no larger than 2,147,483,647). A pair of zeroes will follow

the last case.

Output

For each input case, display a single line that looks like this:

N items, K answers per question, M questions

where N and K are the values from the input, and M is the maximum number of questions required.

Sample Input

2 524288

3 524288

4 524288

3 9

10 1000

0 0

2589 - Twenty Questions 1/2

Sample Output

524288 items, 2 answers per question, 20 questions

524288 items, 3 answers per question, 13 questions

524288 items, 4 answers per question, 11 questions

9 items, 3 answers per question, 3 questions

1000 items, 10 answers per question, 4 questions

North Central 2002-2003

2589 - Twenty Questions 2/2

2006 East Central Regional Contest 9

Problem F: Roofing It

Bill Eaves owns the Shingle Minded roofing company which is a sub-contracting firm specializing inputting roofs on buildings. Often, Bill will receive a design for a roof from some young hot-shotarchitect which, though it may have some aesthetic appeal, is totally impractical. In these cases, Billwill begin negotiations with the architect and the client to find a simpler design. Bill’s only concern isthat the roof be convex, to allow rain, snow and frisbees to roll off easily. The architect’s main concernis that the maximum height between his original roof and the compromise roof be as small as possible.The client’s main concern is to get out of this meeting as soon as possible and get back to watching TV.

The architect’s plans for the roof come in the form of a series of n points (xi, yi) specifying the outlineof the roof as seen from the side of the house. The roofs are always symmetrical, so the architect onlyshows the front side of the roof (from its start at the front of the house to its peak). Once Bill getsthese plans and a decision is made on how many sections the convex roof should have, he must decidehow to place the sections so as to 1) make sure that all the original (xi, yi) points lie on or below thenew roof, and 2) to minimize the maximum vertical distance between any of the original (xi, yi) pointsand the new roof. All sections must lie on at least two of the initial points specified by the architect.An example is shown below. On the left are the initial points from the architect. The next two picturesshow an approximation of the roof using only two sections. While both of these are convex, the secondof the two is the one which minimizes the maximum distance.

r

(0,0)

r

(1,3)

r

(3,6)

r

(6,9)

r

(8,10)

r

(17,12)

r

r

r

r

r

r

���������

�����

max

r

r

r

r

r

r

�������

max

Input

Input will consist of multiple test cases. Each case will begin with two positive integers n and k

(2 ≤ n ≤ 100, 1 ≤ k < n) indicating the number of points used in the original roof plan and the numberof sections used in the compromise roof. These will be followed by n lines each containing two floatingpoints numbers xi yi, specifying the n points in the roof plan. These values will be given in increasingorder of x, and the last point will be guaranteed to have the highest y value of any of the points. Allvalues will be between 0.0 and 10000.0. The last case is followed by a line containing 0 0 which indicatesend-of-input and should not be processed.

Output

For each test case, output the maximum distance between the best compromise roof and the initialpoints, rounded to the nearest thousandth.

2006 East Central Regional Contest 10

Sample Input

6 2

0.0 0.0

1.0 3.0

3.0 6.0

6.0 9.0

8.0 10.0

17.0 12.0

0 0

Sample Output

1.500

2596 - Acceptable Strings

North America - North Central - 2002/2003

A deterministic finite automaton has a finite set of states, with directed edges leading from one state to

another. Each edge is labeled with a symbol. In this problem, we are only concerned about automata (the

plural of automaton) that use the binary digits 0 and 1 as symbols. Each edge is thus labeled with 0 or 1. One

state is identified as the start state, and one or more states are identified as final states.

A finite automaton is usually represented by a graph. For example, consider the finite automaton represented

by the graph shown below; the states are shown as circles, and are named 1 and 2 for ease of identification. In

this automaton, state 1 is the start state, and state 2 is the final state.

Each automaton in this problem accepts or rejects a string as follows. Beginning in the start state, for each

symbol (0 or 1) in the input string (working from left to right in sequence), the automaton follows the one

edge labeled with the input symbol from the current state to the next state. After making the transition

associated with the last symbol in the input string, if the automaton is in a final state, then the input is

accepted. Otherwise (that is, if the automaton is not in a final state), the input is rejected.

For the string 0101 and the automaton shown above, we start in state 1 (the start state). Since the first input

symbol is 0, the edge labeled 0 from state 1 back to state 1 is followed, leaving us in state 1. The next input

symbol, 1, causes a transition to state 2. The next symbol, 0, moves us back to state 1. The last input symbol,

1, causes the last transition, from state 1 to state 2. Since state 2 is a final state, the automaton accepts the

string 0101. Note that the string 010 would have been rejected, since the automaton would have been in state

1 (which is not a final state) at the end of the input. This automaton happens to accept all binary strings that

end with 1.

In this problem you will be given one or more automata and an integer N . For each of these, you are to find

the number of binary strings having each length less than or equal to N that are accepted by the automaton.

For example, for N = 3 with the automaton above, the output would specify 0 strings of length 0 (since state 1

is not a final state), 1 string of length 1 (1), 2 strings of length 2 (01 and 11), and 4 strings of length 3 (001,

011, 101, and 111).

Input

There will be multiple input cases. For each case the input begins with three integers N , S , and F . N (no

larger than 10) specifies the maximum length of the strings that are sought. S (no larger than 100) specifies the

number of states in the automaton. F (no larger than 10) specifies the number of final states. Following these

three integers are S pairs of integers. Each pair specifies the labels on the edges from the states, in order,

starting with state 1. The first integer in each pair specifies the state to which the edge labeled 0 connects; the

second integer specifies the state to which the edge labeled 1 connects. Finally, the last F integers identify the

final states. State 1 will always be the start state. The input for the last case is followed by three zeroes.

2596 - Acceptable Strings 1/2

Output

For each case, display the case number (they are numbered sequentially, and start with 1). Then display the

number of strings of each length (from 0 to N ) accepted by the automaton, using a separate line for each

length. The output must be identical in format to that shown in the examples below.

Sample Input

3 2 1

1 1

1 1

2

3 2 1

1 2

1 2

2

10 7 1

2 2

3 3

4 4

5 5

6 6

7 7

7 7

6

0 0 0

Sample Output

Case 1:

Length = 0, 0 strings accepted.

Length = 1, 0 strings accepted.

Length = 2, 0 strings accepted.

Length = 3, 0 strings accepted.

Case 2:

Length = 0, 0 strings accepted.

Length = 1, 1 string accepted.

Length = 2, 2 strings accepted.

Length = 3, 4 strings accepted.

Case 3:

Length = 0, 0 strings accepted.

Length = 1, 0 strings accepted.

Length = 2, 0 strings accepted.

Length = 3, 0 strings accepted.

Length = 4, 0 strings accepted.

Length = 5, 32 strings accepted.

Length = 6, 0 strings accepted.

Length = 7, 0 strings accepted.

Length = 8, 0 strings accepted.

Length = 9, 0 strings accepted.

Length = 10, 0 strings accepted.

North Central 2002-2003

2596 - Acceptable Strings 2/2

����� ����� ������������������������������������ �������!"!"�����#�����$���%&�(')�*+',-/.103254603768�9�:<;>=�?A@B7C0D4C0�EGF

HJI�K�L5MON�P>QSRTMOUWVIXN�KZY�I\[�]^U_KTN�P`HaU_K�L5IXb<L� ���eS��f! �(g���������6% �������

o 25.qp5F�r1st8Du����O z��kyBz|{}~p5FC.5p5F�r1st8Du����O z|�ky����5�

� 7603�D 7�����rOs�83u����Oz|�ky��qwq�� 0D 7k���Oz|��yB�|�<�� ?�FCF6 st8��D 03�5252<;J�1 ��4$�30DF$�Ñ258�¥ä@���7��JÃ_8�9<8� @� ?� 9/8� 9JFC0a9<7C?t�D8� ��7603 p5219SF6�q8�FC0�¥k2JF60n �< ?t 46 ?tF �5?t 4�EG 7C?�8�219< 4����30D �5252<;

¥k��2<F6 8� 9ÑF60� �< ?t 46 ?tF+��stsX �q?�4~EG 7C?t8�219< 4|�X:5pqF FC �58�7C8�¥���4 ���� 2<;�0� EWFC�58��S � o 2S8��D@C �S 46 F67C8�8�F�58��1 �39�0D 258E� 76 ?t8�219»�Ãk8� 4CF���7CF6 8� 9F6�q?�2q�¤?t 25� �50�¥ F60����� �D8~�5?t 4kF67C?�.S ��4k46�q0D 76 F� ��4k.O 0D 4C46 ?t:5st8D ���8�7C;> 460< 032n�58+7C8��� st ?t��8� 9`F6�O ��FTFC �58+:O8�46 F~¥��|;`F60>9< 0�?tFk¥k��4kFC0F67B�|�D8�sdF6�57C0Dp5 �3�n8��D@C �J4CF676 8�8�F+0�EkF60� ¥k2Ñ0D 25st;�0D 21@�8D � Ó ��FCp57��� sts�;<�5 �q8$ ¥k��2<F6 8|9nFC0>rO 25?t 46�S �5?t 4 FC76 ?t.© �� F FC�58�4����>8�.5sA�D@�8$�5846 FB��7CF6 8� 9»�»�� FW �5?t 4k.1 ��76 8�2<FC4�5�q0Dp5 4C8D �

Âk�58�4CF67C8�8�F64 ?t 2� �30D �52q2¤; 4+FC0�¥k2©¥�8� 76 8�2O ���>8|9n:<;n?t 2<F6 8��38�7+2<p5��:O8�764~EG 7C0D � ¦$ FC0ný��Ü ý��ä¦|§D§3 5� Âk�58TÇB p52O@B F6 ?t 0D 2q4¥�8� 76 8�?t 219<8�.18� 2O9<8� 2<F6 st;n21 ���>8� 9Å:¤;Ñ?�2<FC8��D8� 7� 2<p5��:O8�764_EG 7603��¦F60� n� �� ÆDÆ1� Ó 0+ÇB p521@B FC?t 0D 2©@B0325258� @�F64 �> 0D 7C8+F6�O ��2nÆ3Æ46 FC76 8�8�F64����~s�sDÇ�p5 21@B FC?�03254k?t 2� F6�58�F60� ¥k2��1 �39>9< ?��»8�76 8�2¤F~2<p5��:18� 764���¨d�D@C ��46 F67C8�8�F�¥k��4k@B0D 252q8� @B F6 ?t 25�`8BÔ1 �D@B FCst;>F^¥� 0�ÇB pq21@B F6 ?t 03254��Ó 0`F^¥W0 4̀6 FC76 8�8�FC4+?t 2nF6�q8+F60� ¥k2n�O �D9� FC�58$4B���`8 2<p5��:O8�7| �TÃk8 ?t �> ��8� 9< ?A�� F6 8�st; 46 F���76 FC8� 9nFC0>.5sA�� 2n�5?t 4T 7C0Dp5 219ÅF676 ?t.X � o EWFC�58�76 8¥k��4k�> 0376 8kFC�1 ��2�0D 2q8 4Cp1@C�n7603p5219�F676 ?t.X�q �58�¥W03p5sA9��1 �|�38 @C�50346 8�2nFC�58�0D 258�¥k�5?A@C�X�q¥k �58�2� ¥k76 ?tF6 FC8�2n9< 0�¥k2n��4k� 46 8��3p58�21@B80�E� 46 F67C8�8�F2<p5��:18�764� ?t 4 st8BÔ5?A@B03�D 7��� .5�5?A@��� s�st;`F6�58�4C����stst8� 4CF�� dp5F� �D 0D �q252<;n¥���4� 250DF+ ��:qs�8F60`r12O9n8��38�2J0D 2q8$4Cp1@C�J760Dpq 219F67C?t.X �

Ã_8�st.� �30D �5252<;© �� 219� ¥k7C?�FC8>��.57C0D �37����Á¥k�q?� @C�Ñr1 219< 4�FC�58>9<8�4C?�7C8� 9S 46�q0D 76 FC8�46 Ff760Dpq 219nFC76 ?t.X � o EWF6�58 7603 p5219ÑF67C?�.J9<0<8�42503FT8BÔ5?t 46 F~F6�58+.q760D �37����Á46�503p5sA9>¥k7C?tF6 8$�� �>8� 4C4����38D ���T 4C46 p5�`8+F6�1 �� F��30D �5252<;� st ?t�D8� 4T �� FTF6�q8�ÇB pq21@B F6 ?t 032�8�219< ?t 25�>FC �58+4CF676 8�8�FÓ 0O ��¦$ ¥k?tFC�© 4C����stst8� 7~2¤pq��:18�7| ��Tsts� 46 F67C8�8�FC4�?�2SF6�q8$ FC0�¥k2Ê ��76 8F^¥� 0�¥k�|;< �+Âk�58�7C8>8BÔ5?t 46 FC4$�`¥��|;ÅEG 7603�Á8��D@C �J46 FC76 8�8�F$FC0��2q0DF6�q8�7k46 FC76 8�8�F~?t 2>F6�q8 FC0�¥k2X �� Âk�58�46 FC76 8�8�F64� ?t 2>F6�q8 FC0�¥k2���76 8��D8� 7C;> 21 ��767C0�¥á ��219`FC�58�76 8f?�4� 250.1 03464C?�:q?�st ?tF^;�F60F6 pq76 2� :1 �D@C�F6�q8ê@� ��7T 0321@B 8� �58+?t 4� ?t 2�FC�58�46 FC76 8�8�F|�

å�æ�ç�è�éo 25.qp5F�r1st8>@B032546 ?t 46 FC4$0� ET 46 8��38�7��� sW:qs�05@C�<4��n̈��3@C�J:5st0/@C�J9<8�4B @B76 ?t:O8� 4�0D 258�FC0�¥k2X �� ¨��D@C �Ê st ?t 258 ?t 2JF6�58�:5st05@C�a@�0D 2<F���?�2q4

F6�q76 8�8�?t 2<F6 8��38�764� ���������q�<¥k�58�7C8�����¬��219�� � ¬ ��7C8�F6�58~2¤pq��:18�7C4W 0� E/ÇB pq21@B F6 ?t 03254W¥k�q?� @C����76 8+@B0325258� @�F6 8� 9� :<;F6�58�4CF676 8�8�F2<p5��:O8�7!�q�+Âk�588�219Ñ0�EWFC�58�:5st05@C��?t 4� ����76 �38� 9`:¤;ÑF6�58st ?�2q8�@B032<F���?t 25?t 25�"�� #$�"# ¬5���kF�FC�58�8�219Ñ0�EWFC�58 ?�2q.5p5F~r1st8F6�q8�76 8+?t 4T �� 2�8��>.5F^;:5st 05@6 �»�%��#&�'# ¬5�

þèÜé|ç�è�éÂk�58 0DpqF6 .5p5F_r1st8$ @B032546 ?t 46 FC4+0�EW«�s� ?t 258f:5st0/@C�< 4+@B0D 7C76 8�4C.103219< ?t 25�� F60`F6�58:5st05@C�¤4~0�EWFC�58 ?�2q.5p5FkrOs�83�T Âk�58fr17C46 F�st ?�2q8+0�E

8��3@6�`:5st05@6 �>@� 0D 2<F��� ?�2q4WFC�58T 4C8��3p58�21@B 8 0�EX 4CF67C8�8�FT2<p5��:O8�7C4kÍG 4C?�2q�Dst8T �`8���:18� 764�0� EXF6�58~46 8��3p58�2O@B 8���76 8~46 8�.1��7��� F6 8� 9`:<;�46 .1 �3@B 8�Ï9<8�4B @B76 ?t:5?t 25���D 03�5252<; 4k7C0Dp5 2O9>FC 76 ?t.X � o E�F6�58�7C0Dp5 219ÅF676 ?t.n@��� 25250DF~:18êEG 0Dp5 2O9�F6�q8$ @B0D 7C76 8�4C.103219< ?t 25��0DpqF6 .5p5Fk:qs�05@C��@�0D 2<F���?�2q4F6�q8 �`8�464B���3 8!(5���<{*)����1z|�&)5� �<�J{1�3� � �1z<���»�X Âk�q8 4C8� @B03219� st ?t 258�0�EÜ8| �D@C �� :5st0/@C� ?̀�4k8��>.5F^;< �

ÿ������Jç���?t 25.5pqF�r1st8Du� � �� �

�,+� � �� � �� �� � �� �

� � ���� �� �03p5F6 .5pqF�r1st8Du� � � �-+

(5���<{.)��Oz|�&)5� �<�J{1�D� � �Oz<���