Problem 1 Generation of combinations Level: Hard

Given unsigned integers n ≥ k ≥ 0, generate all the k-combinations of the n objects, numbered 1, 2, …, n, using the following algorithm:

Algorithm ( k -combinations in )

Starting with the combination 1, 2, …, k The next combination is found by scanning the current combination from right to left so as to locate the rightmost element that has not yet attained its maximum value. This element is incremented by one, and all positions to its right are reset to the lowest values possible.

Sample test Input: Filename: 1.in Format: There are 10 lines to stand for 10 cases. Each line contains a set of n and k which separated with space.

Output: Filename: 1.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next lines are all of the combinations. Notice that one line for one combination and list the results in ascending order. 2. You don’t have to print any thing if the result is null. 3. The last line of each case is an empty line, even if the result is null.

Example: Input: 0 0 6 3

Output: case 1: case 2: 123 124 125 126 134 135 136 145 146 156 234 235 236 245 246 256 345 346 356 456

All inputs are from stdin and outputs to stdout.

Problem 2 Subset generation Level: Easy

Given a set {1, 2, …, n}, 32 > n ≥ 1, generate all of its subsets.

Sample test Input: Filename: 2.in Format: There are 10 lines to stand for 10 cases. Each line contains an integer number, n.

Output: Filename: 2.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next lines are all of the combinations. Notice that one line for one combination and list the results in following order. (1) Grouping conditions: i. The maximum element in each subset ii. The number of elements (2) All listing orders are ascending order. 2. For each combination, use two symbol, “{“ and “}”, to represent the start and termination respectively. To separate each element, please use the comma symbol(“,”). 3. You don’t have to print any thing if the result is null or overflow. 4. The last line of each case is an empty line, even if the result is null.

Example: Input: 1 3

Output: case 1: {} {1} case 2: {} {1} {2} {1,2} {3} {1,3} {2,3} {1,2,3}

Problem 3 Fibonacci numbers Level: Easy

Given an unsigned integer n, compute the nth Finonacci number Fn according to the equation:

n 0 1 Fn−1 Fn    =   , n ≥ 1 1 1  Fn Fn+1 

Please put the result into a 4 bytes unsigned integer. If Fn overflows, your program shall report so.

Sample test Input: Filename: 3.in Format: There are 10 lines to stand for 10 cases. Each line contains an integer number, n.

Output: Filename: 3.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next line is the answer. 2. Print a word, “overflow”, if the n is too large. 3. The last line of each case is an empty line, even if the result is overflow.

Example: Input: 1 25 48

Output: case 1: 1 case 2: 75025 case 3: overflow

All inputs are from stdin and outputs to stdout.

Problem 4 Greatest common divisor Level: Easy

Given unsigned integers a and b, find gcd(a,b), the greatest common divisor of a and b. ( 改成簡單的只求 gcd,則以下皆可省略。) , according to the definition:

gcd(a, b) = a, if b ≤ 1 (Convention: gcd(0,0) = 0 ) gcd(a, b) = b, if a ≤ 1

More specific definition Case 1: a * b = 0 if a = 0 , gcd (a, b) = b if b = 0, gcd ( a, b) = a Case 2: a * b ≠ 0 if a = 1, gcd ( a, b) = 1 if b = 1, gcd ( a, b) = 1

gcd(a,b) = 2min(a2 ,b2 )3min(a3 ,b3 )5min(a5 ,b5 )7min(a7 ,b7 )11min(a11 ,b11 ) , if a,b ≥ 2 where a = 2a2 3a3 5a5 7a711a11  and b = 2b2 3b3 5b5 7b711b11  are prime factorizations of a and b, respectively.

Sample test Input: Filename: 4.in Format: There are 10 lines to stand for 10 cases. Each line contains a set of a and b which separated with space. Output: Filename: 4.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next line is the answer. 2. The last line of each case is an empty line.

Example: Input: 0 0 0 1 2 0 4 6

Output: case 1: 0 case 2: 1 case 3: 2 case 4: 2

Problem 5

Integer partition Level: Easy

Given an integer n ≥ 1, generate all the ways of summing up to n using integers 1, 2, …, n with repetition.

Sample test Input: Filename: 5.in Format: There are 10 lines to stand for 10 cases. Each line contains an integer number, n.

Output: Filename: 5.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next lines are all of the results. Notice that one line for one result and list the results in descending order. 2. To separate each element, please use a space symbol. 3. For convenience, the termination of each result is space symbol. 4. You don’t have to print any thing if the result is null. 5. The last line of each case is an empty line, even if the result is null.

Example: Input: 1 6

Output: case 1: 1 case 2: 6 5 1 4 2 4 1 1 3 3 3 2 1 3 1 1 1 2 2 2 2 2 1 1 2 1 1 1 1 1 1 1 1 1 1 Problem 6 Parser and evaluator Level: Hard

Write a parser and an evaluator for the arithmetic expressions generated by the following grammar:

E → T + E | T - E | T ⇐ addition/subtraction T → F * T | F / T | F % T | F F → ( E ) | - F | + F | N ⇐ plus/minus sign N → D | D N D → 0 | 1 | 2 | ... | 9

Observe that an expression may contain only integers (generated by N). Hence, the value of an expression is defined to be an integer. In particular, as in C and C++, / and % are integer division for quotient and remainder, respectively. The meanings of other operators are standard. The priority of operator: 1. – (Negative) 2. % 3. *, / 4. +, – (Minus)

Sample test Input: Filename: 6.in Format: There are 10 lines to stand for 10 cases. Each line contains an expression. Output: Filename: 6.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next line is the answer. 2. Print “syntactically incorrect”, if the input is not accepted. 3. The last line of each case is an empty line.

Example: Input: 789-(400+300) 789-400+300 -9*80+72/61%7 72+((38-66) -101**29 123/78%23 45

Output: case 1: 89 case 2: 689 case 3: -706 case 4: syntactically incorrect case 5: syntactically incorrect case 6: syntactically incorrect

All inputs are from stdin and outputs to stdout.

Problem 7 – Binary tree generation

Given an integer n 0, generate all binary trees with n nodes. The order of generation is right subtree first.

Display the binary tree by rotating it 90∘counterclockwise. That is, display it in reverse inorder traversal (right subtree – root – left subtree).

Input: Each line of input contains an integer n 0 specifying the number of nodes in the binary trees to be generated. End of input is indicated by -1.

Output: Each binary tree generated should be in its own separate lines with no trailing spaces and should take minimum number of rows to display. Levels of the tree should be in consecutive columns. Asterisk, ‘*’, should be used to indicate tree nodes. No extra lines should be inserted between binary trees generated. After generating all the binary trees of one input line, print a separator ‘===’

Sample Input: 0 1 2 3 -1

Sample Output from Sample Input: === * === * * * * === * *

* * * * * * * * * * *

* * ===

All inputs are from stdin and outputs to stdout.

Problem 8 – Prime factorization

Given an integer n 2. Factor it into primes and use the prime factorization to determine the number of divisors of n and the sum of divisors of n.

Let , where are primes and . The number of divisors of n can be calculated from .

The sum of divisors of n can be calculated from .

Input: Each line of input contains an integer n 0 specifying the number to be factored. End of input is indicated by -1.

Output: For each test case, three output lines are generated. The first output line is the prime factorization. Each prime factor is represented by an ordered pair . The second and the third output lines are the number of divisors and the sum of divisors, respectively. At the end of each test case, please print a separator ‘===’.

Sample Input: 12 14612 547114163 -1

Sample Output from Sample Input: (2,2)(3,1) 6 28 === (2,2)(13,1)(281,1) 12 27636 === (2333,1)(234511,1) 4 547351008 === All inputs are from stdin and outputs to stdout.

Problem 9 – The next higher integer

Given an integer n 0, find the next higher integer that can be formed by the digits of n. If n is already the maximum integer that can be formed by its digits, the next higher integer is defined to be n itself.

Input: Each line of input contains an integer n 0. End of input is indicated by -1.

Output: Output should contain K lines if there are K integer as input. Each line contains the the next higher integer of input, with no leading or trailing spaces.

Sample Input: 254776631 776654321 -1

Sample Output from Sample Input: 256134677 776654321

All inputs are from stdin and outputs to stdout.

Problem 10 – The inversion vector of a

Let be an array that contains a permutation of the integers 0, 1, 2, …, . Define an array by b[k] = the number of values in that are less than a[k]. The array b is called the inversion vector of the permutation.

For example, let n = 10 and the array a contain the permutation 2, 4, 7, 6, 0, 9, 1, 8, 5, 3 Then, the inversion vector b is 0, 1, 2, 2, 0, 5, 1, 6, 4, 3

Part A – Construct the inversion vector of a permutation Part B – Construct the permutation from its inversion vector

Input: The first line of input contains an integer N specifying the number of test trials that follow. Each test trial contains a letter code followed by a number list. If the letter code is ‘A’, the number list is interpreted as a permutation. If the letter code is ‘B’, the number list is interpreted as an inversion vector. The number list is delimited with spaces and terminated by -1.

Output: Output should contain K lines if there are K input of data. If the letter code is ‘A’, construct the inversion vector from the permutation. If the letter code is ‘B’, construct the permutation from the inversion vector. The numbers are separated with a single space. No trailing spaces follow either the permutation or the inversion vector.

Sample Input: 4 A 2 4 7 6 0 9 1 8 5 3 -1 A 9 8 7 6 5 4 3 2 1 0 -1 B 0 1 2 2 0 5 1 6 4 3 -1 B 0 0 0 0 0 0 0 0 0 0 -1

Sample Output from Sample Input: 0 1 2 2 0 5 1 6 4 3 0 0 0 0 0 0 0 0 0 0 2 4 7 6 0 9 1 8 5 3 9 8 7 6 5 4 3 2 1 0

All inputs are from stdin and outputs to stdout.

Problem 11 – conversion

Given a integer , and a base b, , convert the decimal integer n into an equivalent base-b number.

Use digits 0, 1, …, 9, A(=10), B(=11), …, Z(=35). The base-b number has no leading zeros.

Input: Each line of input contains a single test trial. Each test trial contains 2 integers: the decimal integer to be converted, and the base. The 2 integers is separated by a space. Assume the decimal integer is within the range of a 32-bit unsigned integer. A single -1 indicates the end of trials.

Output: Output should contain K lines if there are K test trials. Each line contains the decimal integer represented in the specified base.

Sample Input: 4294967295 16 4294967295 36 1867590395 2 1867590395 8 -1

Sample Output from Sample Input: FFFFFFFF 1Z141Z3 1101111010100010010101011111011 15724225373

All inputs are from stdin and outputs to stdout.

Problem 12 – number system

Given an integer , compute 1+2+…+n and express the sum in the factorial number system.

In the factorial number system, an unsigned integer m is expressed as

where each di is an integer satisfying and except for m = 0.

For example,

Input: Each line of input contains an integer n 0. End of input is indicated by -1.

Output: Output should contain K lines if there are K integer as input. Each line contains the coefficients of the factorial number system representation. The coefficients are separated by a single space and followed by no trailing spaces. For example, the factorial number representation of the number 2000 would be expressed as 2 4 3 1 1 0 0.

Sample Input: 654321 12345678 -1

Sample Output from Sample Input: 2 6 4 10 9 4 7 7 3 5 3 1 1 1 0 3 10 4 2 3 4 3 1 8 3 4 3 3 1 1 1 0

All inputs are from stdin and outputs to stdout.

Problem 13 – Maximum contiguous subsequence (C) Given a of signed numbers (of any integral or floating type), find a contiguous subsequence whose sum is the maximum sum found in any contiguous subsequence. Also indicate the sum of the subsequence. In case the numbers are all negative, the empty sequence is defined to be the maximum contiguous subsequence, which has sum zero.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset contains a sequence of signed numbers. A sequence begins with a [ symbol and a space, ends with a space and a ] symbol. Every two numbers are separated by a space.

Output For each case, the output should indicate the first index and the last index of the maximum contiguous subsequence and the sum of maximum contiguous subsequence. The detailed form is as the sample output. Notice that there are three dot symbols between the first and the last index.

Sample Input [ 38 -62 47 -33 28 13 -18 -46 8 21 12 -53 25 ] [ 3.1 -4.1 5.9 2.6 -5.3 5.8 9.7 -9.3 -2.3 8.4 ] .

Sample Output for the Sample Input Sum of A[2...5] is max, which sum is 55 Sum of A[2...6] is max, which sum is 18.7

All inputs are from stdin and outputs to stdout.

Problem 14(易)

Prime factorization of n!

The grow very rapidly. Such large numbers can be represented by their prime factorizations. For example, 25!= 15511210043330985984000000

= 222 ⋅ 310 ⋅56 ⋅ 73 ⋅112 ⋅131 ⋅171 ⋅191 ⋅ 231 may be represented by (22 10 6 3 2 1 1 1 1) i.e. the list of exponents of the primes 2, 3, 5, 7, 11, 13, 17, 19, 23, in that order.

Given an unsigned integer n ≥ 2, your problem is to factor n! into primes according to the following theorem:

THEOREM The prime p is a divisor of n! with the multiplicity  n   n   n   n  µ = + + + = ∗    2   3  ∑  k  ( )  p  p   p  k>0  p 

For example, if n = 25 and p = 2, we have 25 25 25 25 µ = + + + = 12 + 6 + 3 + 1 = 22  2   4   8  16 So 25! is divisible by 222 but not by 223.

Although formula (∗) is written as an infinite sum, it is really finite for any particular values of n and p, since all of the terms are eventually zero. Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset consists of a single line which contains an unsigned integer. Each unsigned integer is greater than one and less than or equal to 5000 ( n ≥ 2 and n ≤ 5000).

Output For each input unsigned integer n, print n!= first. Then print the list of exponents of primes in that order and the list is enclosed by (). Any two adjacent exponents in the list are separated with space.

Sample Input 25 500 . Sample Output for the Sample Input 25! = (22 10 6 3 2 1 1 1 1) 500! = (494 247 124 82 49 40 30 27 21 17 16 13 12 11 10 9 8 8 7 7 6 6 6 5 5 4 4 4 4 4 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)

All inputs are from stdin and outputs to stdout.

Problem 15(易)

Fibonacci number system

The sequence of Fibonacci numbers is defined by

fib(0) = 1 fib(1) = 2 fib(n) = fib(n −1) + fib(n − 2), n ≥ 2

So, the first few Fibonacci numbers are: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

Using Fibonacci numbers as bases, the binary string bk b1b0 represents the number k ∑bi ⋅ fib(i) . i=0 For example, the binary string 100010 represents the number 15, since

1⋅ fib(5) + 0 ⋅ fib(4) + 0 ⋅ fib(3) + 0 ⋅ fib(2) +1⋅ fib(1) + 0 ⋅ fib(0) = 13 + 2 = 15 Observe that the binary string 11010 also represents the number 15, since 15 = 8+5+2. To make the representation unique, larger Fibonacci number must always be used whenever possible (i.e. disallow two adjacent 1's). So, the number 15 is represented by 100010, rather than 11010.

Your problem is to find out the binary string that represents a given unsigned integer.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset consists of a single line which contains an unsigned integer.

Output For each input dataset, print the corresponding binary string representation using Fibonacci numbers as bases.

Sample Input 15 2971215072 4294967296 . Sample Output for the Sample Input 100010 101010101010101010101010101010101010101010101 1010001000010101000101000100000001000100100100

All inputs are from stdin and outputs to stdout.

Problem 16(難)

Binary quicksort (Radix-exchange sort)

Sort an array of unsigned integers into non-decreasing order by means of a variant of quicksort, called binary quicksort (or radix-exchange sort), as described below.

Step 1: Partition the array into two subarrays according to the leftmost bits of the integers so that one subarray contains integers beginning with a 0 bit, and the other contains integers beginning with a 1 bit. Step 2: Recursively sort the two subarrays of integers with 1 fewer bit (since the leftmost bits have already been sorted in Step 1).

As an example, the process of sorting the sequence 4,0,3,7,5,1,6,2 of 3-bit unsigned integers is illustrated below. The bits boxed together are the leftmost bits of the shaded integers of the subarray to be sorted.

4 = 100 010 001 000 = 0 0 = 000 000 000 001 = 1 3 = 011 011 011 010 = 2 7 = 111 001 010 011 = 3 5 = 101 101 101 100 = 4 1 = 001 111 100 101 = 5 6 = 110 110 110 110 = 6 2 = 010 100 111 111 = 7

The first stage partitions the array into a subarray with integers having leading 0 bits and a subarray with integers having leading 1 bits. The first subarray is then partitioned into a subarray with integers having leading 00 bits and a subarray with integers having leading 01 bits. The process stops when the bits are exhausted.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset is an array of unsigned integers. The array is enclosed by {} and any two adjacent unsigned integers of the array are separated with space.

Output For each input dataset, print the sorted result. In other words, print the unsigned integers of the input array in non-decreasing order. Any two adjacent unsigned integers are separated with space.

Sample Input {UINT_MAX 777, INT_MAX 7 999999999 INT_MAX+1u 7777777 77 88888888 UINT_MAX-1} . Sample Output for the Sample Input 7 77 777 7777777 88888888 999999999 2147483647 2147483648 4294967294 4294967295 Problem 17(易)

Farey series of order n

The ascending sequence of all reduced fractions between 0 and 1 which have denominators ≤ n is called the Farey series of order n. For example, the Farey series of order 7 is 0 1 1 1 1 2 1 2 3 1 4 3 2 5 3 4 5 6 1 , , , , , , , , , , , , , , , , , , 1 7 6 5 4 7 3 5 7 2 7 5 3 7 4 5 6 7 1 Note that there are 19 terms in this series.

Let xi yi , i = 0,1,2,3,, be the Farey series of order n. It can be shown that

x0 = 0, y0 = 1

x1 = 1, y1 = n

xk+2 = ( yk + n) / yk+1 xk+1 − xk

yk+2 = ( yk + n) / yk+1  yk+1 − yk , k ≥ 0

2 2 The total number of terms in the series is approximately 3n π . Given an integer n ≥ 1, generate and count the number of terms in the Farey series of order n.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset consists of a single line which contains an unsigned integer. Each unsigned integer is greater than zero and less than or equal to 100 ( n ≥1and n ≤100).

Output For each input dataset, print a line which shows the number of generated terms. Next, print the all terms in the Farey series of order n and any two adjacent terms are separated with space in a new line. Note that print a new line whenever the number of printed terms is times of 100.

Sample Input 3 7 9 . Sample Output for the Sample Input 5 terms 0/1 1/3 1/2 2/3 1/1

19 terms 0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1

29 terms 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1

All inputs are from stdin and outputs to stdout.

Problem 18(易)

64-bit unsigned integers

Compute the value of a 64-bit unsigned integer represented by two 32-bit unsigned integers r and s:

63 … 32 31 … 0 r s

32 In other words, given r and s, compute r × 2 + s = r × 4294967296 + s

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset consists of a single line which contains two unsigned integer separated with space. Each unsigned integer is less than or equal to 4294967295.

Output For each input dataset, print the computed 64-bit unsigned integer.

Sample Input 0 2003 1000 0 23283 276447232 4294967295 4294967295 . Sample Output for the Sample Input 2003 4294967296000 100000000000000 18446744073709551615

All inputs are from stdin and outputs to stdout.

Problem 19(難)

Set partition

Given a set A = {a1, a2 ,, an } of positive integers, count the number of A′ ⊂ A such that = ∑ ∈ ′ ai ∑ ∈ − ′ ai ai A ai A A Also generate all such A′.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset is an array of unsigned integers. The array is enclosed by {} and any two adjacent unsigned integers of the array is separated with space.

Output For each input dataset, print “No such subset” if the input set contains no subsets which meet the criterion. If the input set can be divided into desired subsets, print the number of such subsets first and then print each of those subsets. Each subset is enclosed by {} and printed in one line and any two adjacent positive integers in the subset are separated with space.

Sample Input {1 2 3 4 5 6 7} {1 3 5 7 12} . Sample Output for the Sample Input 8 subsets. {1 6 7} {2 5 7} {3 4 7} {1 2 4 7} {3 5 6} {1 2 5 6} {1 3 4 6} {2 3 4 5}

No such subset

All inputs are from stdin and outputs to stdout.

Problem 20 - A Vertical numbers

Given two unsigned integers -- x and y, whose data type is short or long, and two parameters -- b and align_up. Write a function to display the integer x vertically in the first column, and the integer y vertically in the second column. Display both integers in base-b notation, where b = 2 or 16. If align_up is true, the two integers should be aligned at the top; otherwise, they should be aligned at the bottom. For example, when the inputs are long x and short y, the following diagram shows what you should output.

Column 1 2 1 2

y x x

y

align_up = false align_up = true

Input The first line of input file contains an integer number M specifying the number of test cases. Each test case consists of a single line which contains data type of x, value of x in representation, data type of y, value of y in hexadecimal representation, value of b, and a string K. There will be a space interleaved between each parameter. When K equals to “true”, align_up should be set to true. When K equals to “false”, align_up should be set to false. The value of x and y is at most 4294967295.

Output The output data file contains M blocks. Each block contains a vertical number. The i-th block displays a vertical number which is the result of the i-th test case. For the i-th case, you should output Case i: and start to show the vertical number from the next line.

Sample Input 2 long 123456 short abcd 16 true short abcd long 123456 16 false

Sample Output for the Sample Input Case 1: 0a 0b 1c 2d 3 4 5 6 1

Case 2: 0 0 1 2 a3 b4 c5 d6

All inputs are from stdin and outputs to stdout.

2

Problem 21-C Number sequence

Let Sk denote the sequence of integers form 1 to k, written one after another. For

example, S20 is the sequence 1234567891011121314151617181920. Next, let X denote the sequence of S1S2S3…. Here are the first 56 digits (form S1 to S10) of the sequence X

11212312341234512345612345671234567812345678912345678910… .

Let X(n) = the nth digit in the sequence X. For examples, X(1) = 1, X(2) = 1, X(3) = 2, X(4) = 1, X(5) = 2, X(6) = 3. Given an integer 15000000 ≥n ≥ 1, compute X(n)

Input The first line of input file contains an integer number M specifying the number of test cases. Each test case consists of a single line which contains a natural number n.

Output The output data file contains M lines, each of which is the value of X(n) for the corresponding input case. For the i-th case, you should output Case i: followed by a space and the value of X(n) in line i.

Sample Input 3 155 32767 123456789

Sample Output for the Sample Input Case 1: 1 Case 2: 8 Case 3: 3

All inputs are from stdin and outputs to stdout.

Problem 22 - A Number substitution

Given three unsigned integers n, a, and b, compute sub(n,a,b) = the number obtained from n by substituting b for the leftmost occurrence of a in n = n, if there is no occurrence of a in n

Input The first line of input file contains an integer number M specifying the number of test cases. Each test case consists of a single line which contains three decimal numbers indicate (n, a, b).

Output The output data file contains M lines, each of which is the value of sub(n, a, b) for the corresponding input case. For the i-th case, you should output Case i: followed by a space and the value of sub(n, a, b) in line i.

Sample Input 3 123232789 232 56 1250000 12 40000 2222233333 55 66

Sample Output for the Sample Input Case 1: 15632789 Case 2: 4000050000 Case 3: 2222233333

All inputs are from stdin and outputs to stdout.

Problem 23 - B Divisible group sums

Given an array of n integers, an integer k, , and an integer d, . For all groups of k integers chosen from the array, determine how many of these chosen groups have a sum, which is divisible by d.

Input The first line of input file contains an integer number M specifying the number of test cases. Each test case consists of a single line. In the beginning of test case are 3 decimal numbers indicating (n, k, d). Following the 3 decimal numbers is the array of n integers. All numbers in the same line are interleaved by a space.

Output The output data file contains M lines, each of which is the number of groups for the corresponding input case. For the i-th case, you should output Case i: followed by a space and the number of groups in line i.

Sample Input 3 7 2 5 1 3 5 6 7 8 9 7 5 3 1 3 5 6 7 8 9 7 6 7 1 3 5 6 7 8 9

Sample Output for the Sample Input Case 1: 4 Case 2: 7 Case 3: 0

All inputs are from stdin and outputs to stdout.

Problem 24 - C Integral-2-floating conversion Recall that IEEE 754 single precision floating-point numbers are of the format depicted below.

R 1-bit sign s 8-bit exponent e 23-bit fraction f

, where .

R = 0, if e = 0 and f = 0 You are asked to convert a given signed integer n to an IEEE 754 single precision floating-point number subject to the following requirements: 1 The 32-bit floating-point number shall be stored in a Boolean array, say, bool a[32]; by treating each Boolean value as a binary digit. 2 The result of the conversion is exact, if the corresponding real number n.0 is representable with IEEE 754 single precision format. Otherwise, the result of the conversion shall be rounded down (truncated). For example, there is a 4-bit space to save a number. Now there is a number 11101. The last 1 must be rounded down (truncated), so 1110 will be saved to that space. 3 The final conversion result contained in the array may be viewed as a 32-bit . Print it out as an equivalent 11-digit number.

Input The first line of input file contains an integer number M specifying the number of test cases. Each test case consists of a single line which contains a signed integer n. The given signed integer is between -2147483647 and 2147483647.

Output The output data file contains M lines. Each line consists of an 11-digital octal number which is equivalent to the given number n in IEEE 754 single precision floating-point format. For the i-th case, you should output Case i: followed by a space and the 11-digital octal number in line i.

Sample Input 4 23 123 -123 -23 Sample Output for the Sample Input Case 1: 10156000000 Case 2: 10275400000 Case 3: 30275400000 Case 4: 30156000000

All inputs are from stdin and outputs to stdout.

Problem 25 - B

Given an unsigned integer n, 1 ≤ n ≤ 7, generate all the permutations of n objects, numbered 1, 2, 3, … , n.

Input The first line of input file contains an integer number M specifying the number of test cases. Each test case consists of a single line which contains a natural number n, where n = 1~7.

Output The output data file contains M blocks, each of which is the total permutations of n objects for the corresponding input case. For the i-th case, you should output Case i: and start from next line to output all the permutations of n objects line by line. You must use ordered lexicographic permutation to show your answer. When you show a permutation of n objects, interleave a space between each object.

Sample Input 3 2 3 2

Sample Output for the Sample Input Case 1: 1 2 2 1 Case 2: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Case 3: 1 2 2 1

All inputs are from stdin and outputs to stdout.

Problem 26 Binary-coded decimal

Binary-coded decimal (BCD) is a format for representing decimal integers in which each digit is represented by its own binary encoding. In BCD, each digit occupied 4 bits (a nibble). Thus, the 4-byte BCD representation of the decimal integer 61259 is

00000000 00000110 00010010 01011001 0 0 0 6 1 2 5 9

You are asked to perform the addition, subtraction, and multiplication of two decimal integers, say a + b, a – b, a * b, where a and b are decimal integers, in the following way:

Step 1 Convert a and b to equivalent BCDs Step 2 Add, subtract, or multiply the two BCDs to obtain a result in BCD format Step 3 Convert the resulting BCD back to an equivalent decimal integer

Note that the input decimal numbers are less than or equal to 8 digits.

Sample input Fist line in input file indicates the number of input patterns, and flowing are tuples of operation and two numbers. The delimiter in tuple is a space. 3 + 1234567 8765433 - 22334455 4456 * 219625 4237

Sample Output 10000000 22329999 930551125

All inputs are from stdin and outputs to stdout.

Problem 27 Reverse and add

Given an unsigned integer, repeatedly apply the “reverse-and-add” procedure − reverse its digits and add it to the original number − until a palindrome (which is a number that reads the same backwards as forwards) is produced or an overflow occurs

You shall output the sequence of numbers produced by the reverse-and-add procedure until a palindrome or an overflow is detected. In case an overflow occurs, display an overflow message.

Input The first line starts with an integer indicates numbers of test patterns which is the followings. Each of test pattern lines specifies an integer number for output of the “reverse-and-add” results. 4 6 99 46075 187

Output Each line in output should be the procedure result of input line. List all temporary results of reverse-and-add procedure and delimit these by space. 6 12 33 99 198 1089 10890 20691 40293 79497 46075 103139 1034440 1478741 187 968 1837 9218 17347 91718 173437 907808 1716517 8872688 17735476 85189247 159487405 664272356 1317544822 3602001953 overflow

All inputs are from stdin and outputs to stdout.

Problem 28 Longest monotonically increasing subsequence in

Find the longest monotonically increasing subsequence (LMIS) of a sequence of n numbers. Also indicate its length. For example, the sequence 2, 5, 3, 1, 6, 4 has three LMIS’s of length 3, namely, (2, 5, 6), (2, 3, 6), (2, 3, 4).

Every two numbers are separated by a space. Sample input Fist line in input file indicates the number of input patterns, and each of flowing two lines which first one notes the number of sequence elements and next notes the sequence represents individual test pattern. Every two numbers are separated by a space. 4 6 2 5 3 1 6 4 9 2 6 1 9 7 3 5 4 8 7 1 2 3 4 5 6 7 7 7 6 5 4 3 2 1

Sample Output In each output, you must point out the number of LMIS in test pattern, and then output the possible LMIS below. Every two numbers are separated by a space. 3 2 5 6 2 3 6 2 3 4 5 2 6 7 8 2 3 5 8 2 3 4 8 1 3 5 8 1 3 4 8 1 1 2 3 4 5 6 7 7 7 6 5 4 3 2 1

All inputs are from stdin and outputs to stdout.

Problem 29 Permutation

Given a positive integer n, generate a permutation of 1, 2, …, n

Sample input Fist line in input file indicates the number of input patterns, and flowing indicates the value of n. 2 2 3

Sample Output For every test pattern, you must first print out the number of all permutations, and print the permutation in the following lines. The delimiter of permutation is a space. 2 1 2 2 1 6 1 2 3 1 3 2 2 1 3 3 1 2 2 3 1 3 2 1

All inputs are from stdin and outputs to stdout.

Problem 30 Generalized Towers of Hanoi

Let G be a directed graph that describes the allowable moves between the pegs A, B, and C of a Hanoi Towers problem.

G: A

C B

Given a graph G, solve the associated Hanoi Towers problem of moving n disks from peg A to B.

Sample input Fist line in input file indicates the number of input patterns, and flowing indicates the value of n. 1 3

Sample Output For every test pattern, you must first print out the number of moving, and print the moving step bye step in following lines. Use “->” represents the moving. 7 A->B A->C B->C A->B C->A C->B A->B

All inputs are from stdin and outputs to stdout.

W.P.Hwang CC-CCS-NCTU Problem 31 m-way merge

Given m sorted sequences, each having n elements which are all unsigned integer, merge the m sequences into a single sorted sequence. The input data are given by an m × n matrix. After merging the input sequences, the same matrix shall hold the resulting single sorted sequence.

For example, let int a[4][5] = {{3,4,7,10,20},{2,5,8,11,19},{6,9,12,16,17},{1,13,14,15,18}}; Then, after merging the 4 sorted sequences, the contents of the array a becomes 1,2,3,4,5, 6,7,8,9,10, 11,12,13,14,15, 16,17,18,19,20

Sample input Fist line in input file indicates the number of input patterns, and following which shows “m n” means we will have m sorted sequences, and each sequence has n elements. The delimiter in sequence is a space. 2 4 5 3 4 7 10 20 2 5 8 11 19 6 9 12 16 17 1 13 14 15 18 5 4 4 7 10 20 2 5 11 19 6 9 16 17 1 13 15 18 3 8 12 14

Sample Output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

All inputs are from stdin and outputs to stdout.

1

Problem 32 Single-source all-destination shortest paths

Given a weighed digraph, find the shortest paths from a source vertex to goal vertices.

Sample test Test your program on the following digraph in which the weights of red, green, and blue edges are 6, 1, and 3, respectively.

  

  

  

Let vertex 0 be the source vertex. Print out the length of a shortest path from vertex 0 to vertex 8 = 7.

Sample test Input The input consists of multiple lines, followed by a line which contains only a single ‘.’(period). The fist line of input file contains an integer number M specifying the number of vertices of a graph. There will be M lines below and each line contains M decimal numbers, which are separated by a space. Each number A in line B represents the weight of traveling from vertex B to vertex with index of A’s position in this line, and if there is no path from B to A, the weight should be -1. Notice that there could be more than one graph for testing, and the input of next graph begins from the next line after the last input of former graph.

Sample Input 9 -1 3 -1 3 6 -1 -1 -1 -1 -1 -1 1 -1 3 6 -1 -1 -1 -1 -1 -1 -1 1 3 -1 -1 -1 -1 -1 -1 -1 3 -1 1 6 -1 -1 -1 -1 -1 -1 1 -1 1 6 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 -1 5 3 -1 -1 -1 -1 -1 4 9 -1 2 -1 -1 -1 -1 -1 -1 -1 5 -1 -1 -1 -1 -1 .

Output Output should contain K lines if there are K graphs as input. Each line contains the answer.

Sample Output 7 14

All inputs are from stdin and outputs to stdout.

Problem 33 UNIX-like command: recho

Echo the command-line arguments, possibly in reverse order as demanded by switches.

recho [-a] [-c] arg1 ... argn

-a Echo the arguments in reverse order -c Echo the characters of each argument in reverse order

The switches and arguments may be mixed up. The switches themselves are not echoed. The same switch may have multiple occurrences, but the net effect is the same as a single occurrence, i.e. –a –a –a is the same as –a. Report error on illegal switches.

Sample test Input The input consists of multiple lines, followed by a line which contains only a single ‘.’(period). Each line contains a command.

Sample Input recho I like C and C++. Incredible! Crazy! recho -a I like C and C++. Incredible! Crazy! recho I like C and C++. Incredible! Crazy! -c recho I like -c C and C++. -a Incredible! Crazy! recho I like C and C++. Incredible! Crazy! –k I like C and C++. Incredible! Crazy! .

Output Output should contain K lines if there are K lines of input data. Each line contains the answer. If the switch is illegal, print “Illegal switches on "-k"” , where –k is the illegal switch. If the command is illegal, print “Command error !!”.

Sample Output I like C and C++. Incredible! Crazy! Crazy! Incredible! C++. and C like I I ekil C dna .++C !elbidercnI !yzarC !yzarC !elbidercnI .++C dna C ekil I Illegal switches on "-k" Command error !!

All inputs are from stdin and outputs to stdout.

Problem 34 Hamming sequence

The Hamming sequence S is a sequence of distinct integers in ascending order defined as follows: 1 1 ∈ S 2 If x ∈ S, then 2x ∈ S, 3x ∈ S, and 5x ∈ S 3 Nothing else belongs to S

Given a positive integer n, generate the first n elements of the Hamming sequence.

Sample test Input The input consists of multiple lines, followed by a line which contains only a single ‘.’(period). Each line contains a positive integer . Sample Input 20 50 .

Output Output should contain 2K lines if there are K lines of input data. Odd line contains “For n = k, the first k elements of the Hamming sequence are”,where k is the input. Even lines contains the answer.

Sample Output For n = 20, the first 20 elements of the Hamming sequence are 1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36 For n = 50, the first 50 elements of the Hamming sequence are 1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36,40,45,48,50,54,60,64,72,75,80,81 ,90,96,100,108,120,125,128,135,144,150,160,162,180,192,200,216,225,240,243

All inputs are from stdin and outputs to stdout.

Problem 35 Substring generation

A substring of a string s contains a (possibly empty) sequence of characters of s. The characters may not be consecutive in s, but the order of the characters in s is preserved in the substring. For examples, the string s = "snoopy" contains the following substrings: "sopy" "npy" "sy" "snoopy"

Given a string, generate all of its substrings.

Sample test Input The input consists of multiple lines, followed by a line which contains only a single ‘.’(period). Each line contains a string.

Sample Input cat cafe .

Output Output should contain 2K lines if there are K lines of input data. Odd line contains “For s=k, the substrings are”,where k is the input string. Even lines contains the answer. Each substring is quoted by " " and separated by a space.

Sample Output For s=cat, the substrings are "" "t" "a" "at" "c" "ct" "ca" "cat" For s=cafe, the substrings are "" "e" "f" "fe" "a" "ae" "af" "afe" "c" "ce" "cf" "cfe" "ca" "cae" "caf" "cafe"

All inputs are from stdin and outputs to stdout.

Problem 36-1 Well-balanced parentheses

The language of well-balanced parentheses is defined by the following grammar:

S → ε | ( S ) S

Given a string of parentheses, determine if it is well-balanced

Sample test Input The input consists of multiple lines, followed by a line which contains only a single ‘.’(period). Each line contains a string of parentheses.

Sample Input (()())((()())()) (()())(())) .

Output Output should contain K lines if there are K lines of input data. Each line contains “This string of parentheses is well-balanced !” if the string of parentheses is well-balanced, or “This string of parentheses is not well-balanced !”, otherwise.

Sample Output This string of parentheses is well-balanced ! This string of parentheses is not well-balanced !

All inputs are from stdin and outputs to stdout.

Problem 36-2 Well-balanced parentheses

The language of well-balanced parentheses is defined by the following grammar:

S → ε | ( S ) S

Given an unsigned integer n, generate all of the n-pair balanced parenthses

Sample test Input The input consists of multiple lines, followed by a line which contains only a single ‘.’(period). Each line contains an unsigned integer.

Sample Input 0 1 3 .

Output Output consists of multiple lines, each line contains a result of input.

Sample Output

() ()()() ()(()) (())() (()()) ((()))

All inputs are from stdin and outputs to stdout.

Problem 37 – Prefix expression evaluation (B) Evaluate a given prefix arithmetic expression that consists of postive integer constants and binary operators +, -, *, /, and %. The meanings of the operators are standard. In particular, / and % are integer division for quotient and remainder, respectively. In case the expression is an illegal prefix expression, report so. You may assume that the expression contains only digits, spaces, and the five operators mentioned above.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset represents a test data. Each dataset contains a prefix expression. Each symbol or number is separated by a space.

Output For each case, the output should indicate the calculation result of the input prefix expression. If the input expression is illegal, the output is the string “illegal”.

Sample Input - * + 23 % 45 10 6 / 77 12 + * 234 56 .

Sample Output for the Sample Input 162 illegal

All inputs are from stdin and outputs to stdout.

Problem 38 – Postfix expression evaluation (B) Evaluate a given postfix arithmetic expression that consists of positive integer constants and binary operators +, -, *, /, and %. The meanings of the operators are standard. In particular, / and % are integer division for quotient and remainder, respectively. In case the expression is an illegal postfix expression, report so. You may assume that the expression contains only digits, spaces, and the five operators mentioned above.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset represents a test data. Each dataset contains a prefix expression. Each symbol or number is separated by a space.

Output For each case, the output should indicate the calculation result of the input postfix expression. If the input expression is illegal, the output is the string “illegal”.

Sample Input 23 45 10 % + 6 * 77 12 / - 234 56 * + .

Sample Output for the Sample Input 162 illegal

All inputs are from stdin and outputs to stdout.

Problem 39 – Level-order traversal (B) Let the nodes of a binary tree be labeled with small letters a, b, c, etc.

a

b c

d e f

g h

Representing the null pointer by the dot (.) character, the preorder traversal of this binary tree is written as abd..eg…c.fh…

Given such a preorder traversal, show the level-order traversal of the corresponding binary tree. The level-order traversal shall not present the dot characters.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset represents the preorder traversal of a binary tree. The null pointer is represented by a dot character.

Output For each case, the output should indicate the corresponding level-order traversal.

Sample Input abd..eg…c.fh… .

Sample Output for the Sample Input abcdefgh

All inputs are from stdin and outputs to stdout.

Problem 40 – Lazy list (A) Let the circular linked list fib contain an initial sequence the Fibonacci numbers generated so far. Let the function take(fib,n) take out the first n Fibonacci numbers from fib as follows: Case 1: If the list fib already contains (more than) n elements, take(fib,n) simply takes the first n Fibonacci numbers out. Case 2: If the list fib contains fewer than n elements, it is extended to include the first n Fibonacci numbers so that take(fib,n) has enough elements to take out.

The list fib initially contains the first 2 Fibonacci numbers and is initialized to:

fib 0 1

The call take(fib,5) forces the list fib to be extended to

fib 0 1 1 2 3 and produces the list [0,1,1,2,3]. Observe that the circular pointer makes the computation of the next Fibonacci number easy. Next, the call take(fib,3) simply yields the list [0,1,1]; but the call take(fib,6) extends the list again:

fib 0 1 1 2 3 5

Each time the function take is invoked, it reports the number of newly created nodes.

Input The input consists of multiple datasets, followed by a line which contains only a single ‘.’ (period). Each dataset contains a function. Each is represented by the form take(fib,n), where n indicates the first Fibonacci numbers should be taken out. Each call may extend the previous resulting list. The initial list contains the first 2 Fibonacci numbers.

Output For each case, the output should indicate the number of newly created nodes and the output Fibonacci list.

Sample Input take(fib,12) take(fib,10) .

Sample Output for the Sample Input [0,1,1,2,3,5,8,13,21,34,55,89] create 10 nodes [0,1,1,2,3,5,8,13,21,34] create 0 nodes

All inputs are from stdin and outputs to stdout.