0% found this document useful (0 votes)
132 views12 pages

Problem A. Artinals: 1 2 S I 1 2 S 1 2 S 1 2 T

The document describes problems for an ACM programming competition involving artinals, bus tickets, crankshaft design, and destroying graphs. It provides background information and definitions for each problem, describes the input and output format, and gives sample input/output. The problems involve operations on mathematical structures like sets and graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views12 pages

Problem A. Artinals: 1 2 S I 1 2 S 1 2 S 1 2 T

The document describes problems for an ACM programming competition involving artinals, bus tickets, crankshaft design, and destroying graphs. It provides background information and definitions for each problem, describes the input and output format, and gives sample input/output. The problems involve operations on mathematical structures like sets and graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem A. Artinals
Input le: Output le: Time limit: Memory limit: artinals.in artinals.out 5 seconds 64 megabytes

Nick has recently learned about a special kind of sets called artinian sets or simply artinals. These sets have the advantage of possessing a nite representation, so they can be processed by a computer. However, their formal denition is a bit complicated. Here it is: The only artinal of height 0 is the empty set . Artinals of height n are exactly the nite sets composed of artinals of height n 1. Here n 1 is an arbitrary natural number. Finally, A is an artinal if A is an artinal of height n for at least one integer n. The set of all artinals is denoted by U . It is immediate from the denition that an artinal of height n is also an artinal of height n + 1. Thus for any artinal A we can dene its height h(A) as the minimal integer n such that A is an artinal of height n. An artinal of height n is also called an n-artinal. There were two other denitions which took a lot of time to understand. They are the denition of canonical order on U (denoted by <) and the denition of canonical form of an artinal: The canonical form of an artinal A of height n is a representation A = A1 , A2 , . . . , As where Ai are artinals of height n 1 and A1 < A2 < < As . If A = A1 , A2 , . . . , As and B = B1 , B2 , . . . , Bt are two artinals of height n written in the canonical form, then we put A < B i there exists an integer k, 1 k min(s + 1, t), such that Aj = Bj for all integer j such that 1 j < k and either k = s + 1 or Ak < Bk . Now we can dene for any artinal A its canonical representation. It is a string repr(A) composed of characters {, } and , dened in the following way: repr() = {}, and if A is an artinal with canonical form A = A1 , A2 , . . . , As , then repr(A) = { + repr(A1 ) + , + + , + repr(As ) + }. The canonical representation is often rather lengthy. In order to shorten it, the following denition is introduced. For any integer n 0 an artinal n (called nite ordinal) is dened by induction on n: 0 := and n + 1 := {n} n for all n 0. Then we can dene the reduced canonical representation of an artinal in the following way: We take the canonical representation of this artinal and substitute n for any occurrence of the ordinal n that is not contained in an occurrence of m for some m > n. Then some operations on artinals are dened. These operations (from highest priority to lowest) are: Unary intersection : for a non-empty artinal A = A1 , A2 , . . . , As put A := A1 A2 As . Unary union : for any artinal A = A1 , A2 , . . . , As put A := A1 A2 As ; := . Binary intersection : A B := {x : x A & x B }. Binary union : A B := {x : x A x B }. Binary dierence : A B := {x A : x B }. Binary symmetrical dierence : AB := (A B ) (B A). Besides, some relations between artinals are dened: Equality = and inequality =. Inclusion and : A B B A (x A x B ). Element relations and : B A (equivalent to A B ) means that B is an element of A. Canonical order relations <, >, , described above (as usual, A B ((A < B ) (A = B )), A > B B < A and A B B A). Now Nick wants you to write a program that would make some computations with artinals. This program will consist of several operators, each on a separate line. There are ve kinds of operators: Assignment operator ident := expr sets variable ident to the value of expression expr . Evaluate operator ! expr evaluates expr and prints the result in reduced canonical representation on a separate line of output. Page 1 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003 Check condition operator ? expr relation expr checks the condition and outputs either FALSE or TRUE on a separate line of output. Comment operator # any characters the entire line is copied to the output. Empty operator an empty line (i.e. line consisting only of blank characters) does nothing. The following denitions are used: ident ::= alpha { alpha } alpha ::= letter | digit | digit ::= 0|1| . . . |9 letter ::= A|B| . . . |Z|a|b| . . . |z expr ::= { expr , expr } ident expr binop expr binop ::= +|*|-|^ unop ::= +|* relation ::= <|>|=|<=|>=|<>|->|<-|<<|>>

unop expr ( expr )

The binary operators (in the order they were listed in the denition of binop ) correspond to , , and ; the unary operators correspond to and ; nally, the relations correspond to <, >, =, , , =, , , , . Parentheses ( and ) are used to change the precedence of operations as usual. All tokens of input (except several alpha forming a single ident ) can be separated by an arbitrary number of blank characters (i.e. spaces and tabulation characters). Besides, before the execution of the program the variables with names that are decimal representations (without leading zeros) of non-negative integers n 29 are set to the nite ordinals n. All other variables are initialized with . All identiers are case-sensitive.

Input
The input le consists of not more than one hundred lines each containing a single operator. No line is longer than 254 characters.

Output
Produce one line of output for each ?, ! and # operator as described above. It is guaranteed that there will be no run-time errors (e.g. unary will never be applied to an empty set).

Example
artinals.in !2 + 2 !2*2 !3-4 # More examples! 00 := 5+3 ! 3-5 ! 00 ! (5-3)*(5+3) ? 3>9 A := {2,3,9} B := {1,7} ! A^ B ! +239 ? 2->00 ? 2<<00 ? A>>B ! {{{},{{}},{}},B,{A},{B},{A,B}}+B 2 2 0 # More examples! 0 5 {3,4} FALSE {1,2,3,7,9} 238 TRUE TRUE FALSE {1,2,7,{1,7},{{1,7}},{{1,7},{2,3,9}},{{2,3,9}}} artinals.out

Page 2 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem B. Banal Tickets


Input le: Output le: Time limit: Memory limit: banal.in banal.out 2 seconds 64 megabytes

Peter is fond of number theory. Thats why he is looking for interesting bus tickets. Ticket with the number of length 2N is called interesting if the product of the rst N digits of its number is equal to the product of the last N digits. Other tickets are called banal. Peter has found a used ticket in his pocket. Unfortunately the ticket was punched, so Peter cannot recognize some digits. He wonders whether this ticket was an interesting one. Moreover he wants to know how many dierent interesting and banal tickets could be punched to get this one.

Bus ticket
St. Petersburg Prague

2003
Help Peter to nd answers to his questions.

Input
The rst line of the input le contains an integer N (1 N 18). The next line contains a string representing the ticket number. If some digit is punched out it is denoted by ? otherwise it is denoted by itself.

Output
On the rst line of the output le print the number of interesting tickets. On the second line print the number of banal tickets.

Example
banal.in 2 2??3 3 2?6??3 4 96 46 954 banal.out

Page 3 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem C. Crankshaft
Input le: Output le: Time limit: Memory limit: cranksft.in cranksft.out 2 seconds 64 megabytes

One company develops a CAD system for crankshaft design. The crankshafts in question are combined of 0 < N 100 metal plates. All plates are made of the same alloy and have equal (small) thickness. Each plate has the shape of a polygon with 3 Vi 200 vertices. All plates in the crankshaft are parallel and rmly connected to each other. They are the only elements of the crankshaft. Among other features, this CAD system should have a routine for determining the rotation axis of the crankshaft. More precisely, customers sometimes need to nd a line, perpendicular to the plane of the crankshaft plates, rotation around which causes minimal vibration of the crankshaft. It is well known that such axis must pass through the mass center of the crankshaft. The crankshaft design is stored as a set of vertex coordinates for each plate in some Cartesian coordinate system. The plane OXY of the system is parallel to the plates surfaces. All the coordinates are integer, 104 Xij , Yij 104 , where 1 j Vi , 1 i N . The vertices are numbered clockwise (OX axis is directed rightwards, OY axis is directed upwards). Your program should print the rotation axis coordinates (XR and YR ). The correct answer should be given even for a partially designed crankshaft (consisting of two or more non-connected parts). You should suppose that these parts are rmly connected.

Input
The input le consists of integer numbers, delimited by spaces and/or line feeds. The numbers are specied in the following order: N V1 X11 Y11 ... X1V1 Y1V1 ... VN XN 1 YN 1 ... XN VN YN VN

Output
The output le should contain two real numbers: XR and YR . These values must be exact to four digits to the right of the decimal point.

Example
cranksft.in 2 4 4 2 3 3 -0.1 -0.1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 0 0 0 0 -1 0 0 -2 -1 -2 1 -1 0 2 1 2 -1 1 0 cranksft.out

Page 4 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem D. Destroying The Graph


Input le: Output le: Time limit: Memory limit: destroy.in destroy.out 2 seconds 64 megabytes

Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that Bob tries to destroy it. In a move he may take any vertex of the graph and remove either all arcs incoming into this vertex, or all arcs outgoing from this vertex. Alice assigns two costs to each vertex: Wi+ and Wi . If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi dollars. Find out what minimal sum Bob needs to remove all arcs from the graph.

Input
Input le describes the graph Alice has drawn. The rst line of the input le contains N and M (1 N 100, 1 M 5000). The second line contains N integer numbers specifying Wi+ . The third line denes Wi in a similar way. All costs are positive and do not exceed 106 . Each of the following M lines contains two integers describing the corresponding arc of the graph. Graph may contain loops and parallel arcs.

Output
On the rst line of the output le print W the minimal sum Bob must have to remove all arcs from the graph. On the second line print K the number of moves Bob needs to do it. After that print K lines that describe Bobs moves. Each line must rst contain the number of the vertex and then + or - character, separated by one space. Character + means that Bob removes all arcs incoming into the specied vertex and - that Bob removes all arcs outgoing from the specied vertex.

Example
destroy.in 3 1 4 1 1 3 1 3 2 6 2 3 2 1 2 1 2 2 1 3 5 3 1 + 2 2 + destroy.out

Page 5 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem E. Experiment X: Explosions Expected


Input le: Output le: Time limit: Memory limit: exp.in exp.out 2 seconds 64 megabytes

Unlucky scientist Vasya was tired of permanent mockeries of his colleagues. He built a time machine to go away into the future. But his machine had taken him into the past and then exploded! Vasya needed some way to earn money! He has taken the job of the Court Alchemist of King Arthur. His rst task is to create the philosophers stone. Vasya has found K ingredients and now conducts experiments which consist in mixing these ingredients in various proportions. More formally, each experiment consists in taking a1 ounces of the rst ingredient, a2 ounces of the second and so on. Then all the ingredients are put into the crucible, carefully mixed and heated. All ai are non-negative integers, and their sum does not exceed S the capacity of the crucible. In all Vasyas experiments at least two ingredients are taken, i.e. there are at least two ai greater than zero. But all his experiments have been unsuccessful up to now. All mixtures have exploded! The King is unpleased. Maids of honour complain to Queen Guinevere about these awful explosion sounds! The King has decided to give one last chance to Vasya. Tomorrow Vasya is to conduct his next experiment, which could become the last. If the mixture explodes again, Vasya will be executed. Fortunately, Vasya has just found a way to discard some denitely unsuccessful experiments. He has noted that if for some experiment plan (a1 , a2 , . . . , aK ) the mixture has exploded, it would explode for any plan (b1 , b2 , . . . , bK ) such that bi ai for all i. Now Vasya wants to count the number of possible experiments which are not denitely unsuccessful according to his unfortunate experience. He has taken out his pocket computer and now he is writing a program which will compute this number. Can you do this?

Input
The rst line of input le contains three positive integers 2 K 30, 2 S 10000, 0 M 20 where M is the number of experiments Vasya has already conducted. The next M lines contain K numbers each, one line per experiment.

Output
Output the number of possible experiment plans for which it is not evident that the mixture would explode.

Example
exp.in 2 4 2 1 3 2 1 2 exp.out

Page 6 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem F. Factoring a Polynomial


Input le: Output le: Time limit: Memory limit: factor.in factor.out 2 seconds 64 megabytes

Recently Georgie has learned about polynomials. A polynomial in one variable can be viewed as a formal sum an xn + an1 xn1 + . . . + a1 x + a0 , where x is the formal variable and ai are the coecients of the polynomial. The greatest i such that ai = 0 is called the degree of the polynomial. If ai = 0 for all i, the degree of the polynomial is considered to be . If the degree of the polynomial is zero or , it is called trivial, otherwise it is called non-trivial. What really impressed Georgie while studying polynomials was the fact that in some cases one can apply dierent algorithms and techniques developed for integer numbers to polynomials. For example, given two polynomials, one may sum them up, multiply them, or even divide one of them by the other. The most interesting property of polynomials, at Georgies point of view, was the fact that a polynomial, just like an integer number, can be factorized. We say that the polynomial is irreducible if it cannot be represented as the product of two or more non-trivial polynomials with real coecients. Otherwise the polynomial is called reducible. For example, the polynomial x2 2x + 1 is reducible because it can be represented as (x 1)(x 1), while the polynomial x2 + 1 is not. It is well known that any polynomial can be represented as the product of one or more irreducible polynomials. Given a polynomial with integer coecients, Georgie would like to know whether it is irreducible. Of course, he would also like to know its factorization, but such problem seems to be too dicult for him now, so he just wants to know about reducibility.

Input
The rst line of the input le contains n the degree of the polynomial (0 n 20). Next line contains n + 1 integer numbers, an , an1 , . . . , a1 , a0 polynomial coecients (1000 ai 1000, an = 0).

Output
Output YES if the polynomial given in the input le is irreducible and NO in the other case.

Example
factor.in 2 1 -2 1 2 1 0 1 NO YES factor.out

Page 7 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem G. Greatest Common Increasing Subsequence


Input le: Output le: Time limit: Memory limit: gcis.in gcis.out 2 seconds 64 megabytes

You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal possible length. Sequence S1 , S2 , . . . , SN of length N is called an increasing subsequence of a sequence A1 , A2 , . . . , AM of length M if there exist 1 i1 < i2 < . . . < iN M such that Sj = Aij for all 1 j N , and Sj < Sj +1 for all 1 j < N .

Input
Each sequence is described with M its length (1 M 500) and M integer numbers Ai (231 Ai < 231 ) the sequence itself.

Output
On the rst line of the output le print L the length of the greatest common increasing subsequence of both sequences. On the second line print the subsequence itself. If there are several possible answers, output any of them.

Example
gcis.in 5 1 4 2 5 -12 4 -12 1 2 4 2 1 4 gcis.out

Page 8 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem H. Highways
Input le: Output le: Time limit: Memory limit: highways.in highways.out 2 seconds 64 megabytes

In a distant country Lineland there are N cities and they are all located along the highway. The highway is a straight line; it starts from the rst city and runs through the second, third city and so on, ending in the N -th city. The i-th city is located at the distance of Xi miles from the rst one. The highway is wide and smooth, so it is a pleasure for all people to drive along it. But there is one problem all roads in Lineland, including the highway, are one-way. So people are only allowed to drive along the highway from the city with smaller number to the city with greater number and they have to use country roads to get back, and that is not such a great pleasure indeed. After the new president Mr. Pathwayson was elected in Lineland, he has decided that he would like to make it easier for people to get from one town to another. But he does not dare to change the traditions, and make the highway two-way. Therefore he has decided to build new highways to connect the cities, so that it would be possible to get from any city to any other one by highways. Traditionally, the new highways must be one-way. Of course, Mr. Pathwayson is a great president, and he wants people to remember him in years. After a thought he has decided that building just one highway would not be enough for that. Therefore he has decided that he must build two new highways. Each highway would connect two dierent cities. Since people are anxious about their health, and cars running along the highway produce dangerous wastes, each new highway must not pass through any cities, except the cities it connects. Also building two new highways in one city would disturb people too much, so all the cities that would be the ends of the new highways must be dierent. You are the assistant of the minister of transportation of Lineland, so you are asked to choose the cities to be connected by the new highways. Since the cost of building a highway is proportional to its length, the total length of the highways must be minimal possible. Write a program to solve this problem. You may assume that the distance between two cities along the new highway is equal to the distance between those cities along the main highway.

Input
The rst line of the input le contains N (2 N 50 000). Next line contains N 1 integer numbers: X2 , X3 , . . . , XN (1 X2 < X3 < . . . < XN 109 ).

Output
If it is impossible to build the highways satisfying all requirements, print number 0 on the rst line of the output le. In the other case on the rst line of the output le print the minimal possible total length of the highways to be built. On the second line print S1 , E1 , S2 and E2 the numbers of the cities to connect by the rst and the second highway, respectively. Note that highways are one-way and must run from S1 to E1 and from S2 to E2 .

Example
highways.in 4 3 5 10 12 3 1 4 2 highways.out

Page 9 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem I. Invariant Polynomials


Input le: Output le: Time limit: Memory limit: invar.in invar.out 2 seconds 64 megabytes

Consider a real polynomial P (x, y ) in two variables. It is called invariant with respect to the rotation by an angle if P (x cos y sin , x sin + y cos ) = P (x, y ) for all real x and y . Lets consider the real vector space formed by all polynomials in two variables of degree not greater than d invariant with respect to the rotation by 2/n. Your task is to calculate the dimension of this vector space. You might nd useful the following remark: Any polynomial of degree not greater than d can be uniquely written in form P (x, y ) = aij xi y j
i,j 0 i+j d

for some real coecients aij .

Input
The input le contains two positive integers d and n separated by one space. It is guaranteed that they are less than one thousand.

Output
Output a single integer M which is the dimension of the vector space described.

Example
invar.in 1 1 2 2 3 4 invar.out

Page 10 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem J. Jogging
Input le: Output le: Time limit: Memory limit: jogging.in jogging.out 2 seconds 64 megabytes

Its Sunday, November 1, 2390, and Eddy has just been elected to the World Council. Of course, it is a very interesting and responsible job, and Eddy is eager to work in the Council, but there is a problem: Eddy is keen on sport. In particular, he likes jogging and he has been jogging at least thirty minutes each day of his life since he was a little boy. Now Eddy will have even less free time than ever before, when he was just a teacher. How will he nd spare time for jogging? Eddy decided that he would jog on his way to his work. Of course, the Council building is rather far away from his house, so he wants to combine jogging with travelling by public transport. Now it has to be told that the moving pathways are the only means of public transport in the Capital. Each line of pathways consists of a pair of straight pathways running in opposite directions with the same speed v1 > 0. These pathways are very long and relatively narrow, so for the purpose of this task they can be considered sharing an innite straight line on the plane. Besides, for each pathway two numbers are given, Ti+ and Ti the time necessary for boarding and leaving this pathway. Note that crossing a pathway doesnt take any additional time, but changing from the i-th pathway to the j -th in their intersection point (which in fact is not an intersection since there are special bridges built at these points) takes exactly Ti + Tj+ seconds. Of course, Eddy wants to jog also on the pathways, so hell move along the pathways with the ground speed v1 + v2 where v2 > 0 is the speed of Eddy while jogging on still ground. Now he wants you to nd a route from his house to the Council building that would require as little time as possible. This route should consist of several segments, some of which can lie on one of the existing pathways.

Input
The rst line of input consists of a single integer N , 0 N 50 the number of pairs of moving pathways in the city. The second line contains six real numbers x1 , y1 , x2 , y2 , v1 , v2 , separated by spaces the coordinates of Eddys house and of the Council building, and the speed of pathways and of Eddy respectively. Each of the next N lines contains a description of a pathway consisting of six real numbers xi1 , yi1 , xi2 , yi2 , Ti+ and Ti , where (xi1 , yi1 ) and (xi2 , yi2 ) are two dierent points on the pathway and 0 Ti+ , Ti 10 are the boarding and leaving times. All coordinates do not exceed 10000 by their absolute values, and v1 and v2 are real numbers ranging from 1 to 100. All pathways lie on dierent straight lines. Neither (x1 , y1 ) nor (x2 , y2 ) lie on any of the pathways.

Output
First line of output must contain one real number T the minimal travel time required. The second line must contain one integer 0 < M 300 the number of segments from which the optimal path is composed. Each of the next M lines should consist of one integer 0 kj N , the number of the pathway taken (0 means no pathway used), and of the coordinates Xj , Yj of the end of j -th segment of this path. All real numbers are to be output with at least six digits after decimal point.

Example
jogging.in 2 -100 -100 200 100 2.92893219 7.07106781 0 0 1 0 0 0 2000 0 2000 1 0 0 jogging.out 50.000000 3 0 0.000000 0.000000 1 100.000000 0.000000 0 200.000000 100.000000

Page 11 of 12

ACM ICPC 20032004, Northeastern European Region, Northern Subregional Contest St Petersburg, November 1, 2003

Problem K. Key Insertion


Input le: Output le: Time limit: Memory limit: key.in key.out 2 seconds 64 megabytes

As an employee of the Macrohard Company, you have been asked to implement the new data structure that would be used to store some integer keys. The keys must be stored in a special ordered collection that can be considered as an array A, which has an innite number of locations, numbered starting from 1. Initially all locations are empty. The following operation must be supported by the collection: Insert(L, K ), where L is the location in the array and K is some positive integer value. The operation must be processed as follows: If A[L] is empty, set A[L] K . If A[L] is not empty, perform Insert(L + 1, A[L]) and after that set A[L] K . Given N integer numbers L1 , L2 , . . . , LN you have to output the contents of the array after a sequence of the following operations: Insert(L1 , 1) Insert(L2 , 2) ... Insert(LN , N )

Input
The rst line of the input le contains N the number of Insert operations and M the maximal position that can be used in the Insert operation (1 N 131 072, 1 M 131 072). Next line contains N integer numbers Li that describe Insert operations to be performed (1 Li M ).

Output
Output the contents of the array after a given sequence of Insert operations. On the rst line print W the number of the greatest location that is not empty. After that output W integer numbers A[1], A[2], . . . , A[W ]. Output zeroes for empty locations.

Example
key.in 5 4 3 3 4 1 3 6 4 0 5 2 3 1 key.out

Page 12 of 12

You might also like