• 沒有找到結果。

InstructionsandAnnouncements ada-ta@csie.ntu.edu.tw DueTime:2017/10/19(Thu.)14:20ContactTAs: Homework#1

N/A
N/A
Protected

Academic year: 2022

Share "InstructionsandAnnouncements ada-ta@csie.ntu.edu.tw DueTime:2017/10/19(Thu.)14:20ContactTAs: Homework#1"

Copied!
10
0
0

加載中.... (立即查看全文)

全文

(1)

Homework #1

Due Time: 2017/10/19 (Thu.) 14:20 Contact TAs: ada-ta@csie.ntu.edu.tw

Instructions and Announcements

• There are four programming problems and two hand-written problems.

• Programming. The judge system is located at https://ada-judge.csie.org. Please login and submit your code for the programming problems (i.e., those containing “Programming” in the problem title) by the deadline. NO LATE SUBMISSION IS ALLOWED.

• Hand-written. For other problems (also known as the “hand-written problems”), please submit your answers to the instructor at the beginning of the class. NO LATE SUBMISSION IS ALLOWED.

• Collaboration policy. Discussions with others are strongly encouraged. However, you should write down your solutions in your own words. In addition, for each and every problem you have to specify the references (e.g., the Internet URL you consulted with or the people you discussed with) on the first page of your solution to that problem. You may get zero point due to the lack of references.

• Top-graded solutions/codes may be published as references for your fellow classmates.

1

(2)

Problem 1 - Coin (Programming) (10 points)

Problem Description

In the ADA country, there are n types of the coin each with value ai.

Eddy has to pay p dollars with some coins. Could you tell him the total number of ways to pay exactly p dollars?

Since Eddy is really rich, you can assume that Eddy has infinity coins of each type.

Input

The first line contains two integers n, p, indicating the number of types and the total amount Eddy should pay respectively. The second line contains n distinct integers a1, . . . , an, indicating the value of each coin type.

• 1 ≤ n ≤ 100

• 1 ≤ p ≤ 10000

• 1 ≤ ai≤ 10000

Output

Output an integer in one line indicating the answer. Because the number may be too large, please output the number mod 1000000007(= 109+ 7).

Sample Input 4 10

1 2 3 5

Sample Output 20

(3)

Problem 2 - Fibonacci (Programming) (10 points)

Problem Description

Hanhan loves the Fibonacci sequence! He can recite the Fibonacci sequence up to first 912 elements.

The Fibonacci sequence looks like 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .. In the definition, the first and second terms in Fibonacci sequence are 1 and each subsequent term is the sum of the previous two.

• F1 = F2 = 1

• Fn= Fn−1+ Fn−2

Hanhan thinks that the Fibonacci sequence is so beautiful, so he wants to create a similar sequence using his name – Hanhan sequence. The Hanhan sequence is defined as follows:

• H1= a

• H2= b

• Hn= Hn−1+ Hn−2

However, the Hanhan sequence is not as easy as the Fibonacci sequence to remember. Please help Hanhan to find n-th term of the Hanhan sequence.

Input

The first line contains an integer T indicating that there are T scenarios. Each of the following T lines contains three space-saparated integer a, b, n indicating that the first term of the Hanhan sequence is a, the second term of the Hanhan sequence is b, and Hanhan wants to know the n-th term.

• 1 ≤ T ≤ 104

• 0 ≤ a, b ≤ 109

• 1 ≤ n ≤ 109

Subtask 1 (40 %)

• n ≤ 103

Subtask 2 (60 %)

• No other constraint Output

For each scenario, output one line containing one integer indicating the n-th term of the Hanhan sequence. Because the number may be too large, please output the number mod 1000000007 (= 109+7).

3

(4)

Sample Input 3

1 1 1 1 1 10 10 21 514

Sample Output 1

55

859861000

(5)

Problem 3 - Tower of Hanoi (Programming) (15 points)

Problem Description

The Tower of Hanoi (also called the Tower of Brahma or Lucas’ Tower and sometimes pluralized) is a mathematical game or puzzle. It consists of three rods and a number of disks of different sizes, which can slide onto any rod.

The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

1. Only one disk can be moved at a time.

2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack.

3. No disk may be placed on top of a smaller disk.

Eddy is simply good at it. He finds that the solution with minimal number of moves required to solve a Tower of Hanoi puzzle is unique. Now he requests you to figure out the moves from step l to step r.

Input

The first line contains an integer T indicating the total number of test cases. Each test case contains three integers n, l, r in one line, indicating the number of disks and the range of moves to figure out.

• 1 ≤ T ≤ 1000

• 1 ≤ n ≤ 100

• 1 ≤ l ≤ r ≤ 264− 1

• r − l ≤ 1000

• It is guaranteed that r is not greater than the minimal number of moves.

Subtask 1 (40%)

• 1 ≤ n ≤ 10

Subtask 2 (60%)

• No other constraint.

Output

For each test case, output one line containing Case #x:, where x is the test case number (starting from 1). Then, for each step of move, output one line Step x: #y -> #z denoting moving the disk from y to z in step x.

5

(6)

Sample Input

2 3 4 5 2 1 3

Sample Output Case #1:

Step 4: #1 -> #3 Step 5: #2 -> #1 Case #2:

Step 1: #1 -> #2 Step 2: #1 -> #3 Step 3: #2 -> #3

(7)

Problem 4 - Group up (Programming) (25 points)

Problem Description

Today is another happy ADA day. (Will tonight be another happy ADA night?)

As you may know, ADA is one of the most interesting courses in NTU. As usual, there’s a fun activity during the ADA class. Students need to team up and compete with each other with the power of algorithm. Sounds excellent and exciting, right?

However, grouping students into several teams is challenging. There are N students sitting in a row in the ADA class. Let’s number the students from left to right as 1 to N and also number the seats from left to right as 1 to N . That is, student numbered 2 is seated on the right of student numbered 1 while student numbered 3 is seated on the right of student numbered 2, and so on.

The professors of the ADA class require students to be divided into exactly K teams and each of the teams should have at least one student (but can have different number of team members). Each team should gather in front of one of the seats. No two teams can gather in the same position.

Hanhan is the class leader of the ADA class. He comes up with an idea to team up students orderly and methodically. That is, let students numbered l1, l1+1, . . . , r1 gather at seat numbered r1, students numbered l2, l2+1, . . . , r2gather at seat numbered r2, . . ., students numbered lK, lK+1, . . . , rKgather at seat numbered rK, where 1 = l1 ≤ r1 < l2 ≤ r2 < . . . < lK ≤ rK = n and r1 + 1 = l2, r2+ 1 = l3, . . . , rK−1+ 1 = lK. Since Hanhan is quite talented, you can find that following this method, students can be teamed up into exactly K teams and each team will gather at a different position.

However, moving is quite tiring. Students don’t like to move. If a student weighted w moves from seat numbered i to seat numbered j, he/she will consume w × |i − j| units of energy.

Hanhan wants to find a way to choose li, ri such that the total energy consumed by students is minimized. Hanhan has found one way to choose each li, ri. However, he doesn’t know whether the total consumed energy is minimum. Please help Hanhan find out the minimum possible total consumed energy.

Input

The first line contains two integer N, K indicating that there are N students needed to be divided into K groups. The second line contains N space-saparated integers wi indicating the weight of student numbered i.

• 1 ≤ N ≤ 2 × 106

• 1 ≤ K ≤ min(20, N )

• 1 ≤ wi≤ 104

Subtask 1 (10 %)

• N ≤ 13

Subtask 2 (30 %)

• 1 ≤ N ≤ 100

7

(8)

Subtask 3 (60 %)

• 1 ≤ N ≤ 2000

Subtask 4 (bonus 15 %)

• 1 ≤ N ≤ 105

Subtask 5 (bonus 15 %)

• No other constraint

Output

Output one line containing one integer indicating the minimum total consumed energy when choosing optimal li, ri.

Sample Input 1 5 5

1 2 3 4 5

Sample Output 1 0

Sample Input 2 3 2

1021 10 21

Sample Output 2 10

(9)

Problem 5 - (Hand-Written) (20 points)

(1) Given each pair of expressions, A and B, in the following table, please find the relation between A and B (A is O, o, Ω, ω, or Θ of B). Assume that k ≥ 1,  > 0 and c > 1 are constants.

Your answer should be in the form like below:

Given A = x2+ 2 and B = x2 Ans: A is O, Ω, Θ of B

A B

(a) (10%) log2nk n

(b) (10%) √

n nsin n

(c) (10%) 2n 2n/2

(d) (10%) nlog2c clog n (e) (10%) log n! lognn

(2) There is a mysterious saying in the CSIE department: If you want to chase a girl/boy, you need to approach her/him first, and use Messenger (or any kind of chatting apps) to chat with her/him actively for a period of time. The most important thing is to stop chatting with her/him when she/he has the most positive mood changing, comparing to her/his mood at the first time you approach her/him. Therefore, if she/he takes the initiative to contact you and share her/his joy, she/he may have a crush on you.

Now we suppose you are single. Your best friend, who is also her/his best friend, is so close to her/him that he can predict her/his future mood. Your friend will send you her/his mood sequence before you act. In order to approach her/him immediately, you must create an algo- rithm to output the time to approach her/him and the time to disconnect her/him such that you have the highest possibility of getting rid of being single.

Input: [-1, -2, 10, 5, 7, 8, -7] (mood sequence) Output: 1, 2 (highest mood changing score: 12)

(a) (30%) Give the pseudocode for an optimal divide-and-conquer algorithm that outputs the time to approach and the time to disconnect given the mood sequence. Assume that the ups and downs of mood values are stored in an array M [0...n].

(b) (20%) Please show the complexity of your algorithm using Θ and briefly explain how you derive this bound.

9

(10)

Problem 6 - (Hand-Written) (20 points)

(1) Solve the following recurrences. Assume that T (n) = 1 ∀n ≤ 2.

(a) (10%) T (n) = 4T (n2) + n

Prove or disprove that T (n) = O(n2).

(b) (10%) T (n) = 2T (n2) +log nn

Prove or disprove that T (n) = O(n).

(c) (10%) T (n) = 9T (n3) + n3

Prove or disprove that T (n) = O(n3).

(d) (10%) T (n) = T (n5) + T (7n10) + n Prove or disprove that T (n) = O(n).

(e) (10%) T (n) =√ nT (√

n) + n

Prove or disprove that T (n) = O(n log log n).

(2) (50%) In an n×m binary matrix (i.e., every element ∈ {0, 1}), find the largest square sub-matrix which only contains 1s.

Prove or disprove that the time complexity of this problem is O(nm).

參考文獻

相關文件

The first line contains two integers N, M indicating the number of cities in the HH kingdom and the number of roads between them... Problem 5 - Compiler (Handwriting)

Given this additional information, please provide an algorithm that utilizes those relics and finds the shortest route for Piepie to reach QMao’s home alive.. However, troops

Given N magic wands and M fitness values, find a linking method to enhance the maximum power in total. (4) (4pts) The professor who invented Magic Wands Linking realizes that the

Also, because the robbers work as a group, the overall time the group spends on robbing all the people will be the maximum time spent by individual members.. For example, if there are

By the similar reasoning of pumping lemma, if some non-S variable appears at least twice in a path from root, then the looping part between the two occurrences can be repeated as

HMW let the students group themselves into groups with different microculture that optimized the sense of belonging to both class and group and suited each student.. HMW

For periodic sequence (with period n) that has exactly one of each 1 ∼ n in any group, we can find the least upper bound of the number of converged-routes... Elementary number

Write the following problem on the board: “What is the area of the largest rectangle that can be inscribed in a circle of radius 4?” Have one half of the class try to solve this