Sie sind auf Seite 1von 6

PROBLEM-SET ( 21 SEP 2011 )

Problem A : Pay the Autowala


After successfully(?) finishing your mid-sem 1 exams, you went to watch the movie 'Bodyguard' ( and probably thought exams are better :p ). You took an autorikshaw back to college and need to pay him R rupees. The autowala doesn't have any change with him and you have exactly N notes of currency with you, each of value A[i] for i = 0 to N-1. You may not make exactly R rupees with the money you have. For eg. if A = {1,3,7}, you can't make an exact sum of 2 or 5 or 6 with what you have. If you can't make R rupees, you decided to pay him more than R rupees, but as minimum as possible. Given R, N and the array A, calculate the money you will finally pay him. If you can't make at least R rupees, then output -1 instead.

Constraints: 1 <= R <= 108 1 <= N <= 20 1 <= A[i] <= 108 Time Limit : 2s Input: First line contains R and N. Second line contains the array A. Output: Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ) Sample Case 1: Input: 20 5 5 10 3 25 9 Output: 22\n Explanation: You can pay him 10 + 3 + 9 = 22, any other combination is more than 22 or less than 20. Sample Case 2: Input: 10 3 513 Output: -1\n Explanation: You don't have enough money :(

Problem B : Number of ways to Select


Given N ( 1 <= N <= 10000 ), R ( 0 <= R <= N ) and M ( 2 <= M <= 10 9 ), find the number of ways to select R items from a collection of N items. The answer may become very large, so just output the answer modulo M.

Input: Only one line containing N R M. Output: Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ) Time Limit: 2s Sample Case 1: Input: 538 Output: 2\n Explanation: Out of N = 5 items, we can select R = 3 items in 10 ways and ( 10 % 8 ) = 2 Sample Case 2: Input: 1000 112 1003 Output: 666\n Explanation: Take care of modulo operator while dealing with such large values. You may have to take modulo of intermediate values, to avoid overflows.

Problem C : Kth smallest number


Long ago, there lived Ram and Raavan. Ram loves APS and Raavan loves Discrete Maths. They both were invited to Mithibowli to participate in Sita swayamvar. To be fair to both, Janaka set a really good question for them to solve. A lot of range of integers were given to them, and they were asked to find the Kth smallest integer in the union of the sets. For eg: given K = 7 and 3 ranges [1..3], [2..5] and [3..6], their union results in the list {1,2,3, 2,3,4,5, 3,4,5,6} and looking at a sorted list {1,2,2,3,3,3,4,4,5,5,6}, the K = 7th smallest number is 4. Note that K is 1-based. Though Ram is good at APS, he has some trouble dealing with sets. Please help him !

Constraints: N integer ranges are given. 1 <= N <= 1000 Each range [A..B] is such that 1 <= A <= B <= 109 1 <= K <= total number of integers in the union of the ranges Note that you need to use long long for K Time Limit : 1s Input: First line contains N and K. N lines follow, each contains A B, which is the interval [A..B] Output: Print the Kth smallest integer in the union of the sets, in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ) Sample Case 1: Input: 3 11 15 38 24 Output: 5\n Explanation: Union = {1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7, 8} and the 11th smallest integer is 5

Problem D : Substring made easy


Given a string of length L(1 <= L <= 200, Hint: L is small), find the number of distinct sub-strings in the string. Constraint : Use string and set in STL. Time Limit : 1 sec Input : a string (without extra spaces) Output: number of distinct sub-strings. Sample Case: Input: abad Output: 9\n a, b, d, ab, ba, ad, aba, bad, abad

Problem E : Solve Linear Recurrence


Here is a very easy linear recurrence for you to solve, F(k) = 2.F(k-1) + 5.F(k-2) + 3 Initial Cases: F(0) = 1, F(1) = 2. Input: Given n and M Constraints: 0 <= n <= 10^9 , 2 <= M <= 10^9 Output: find the value of F(n) % M Sample Case: 1) Input: 4 15 Output: 2\n 2) Input: 3 40 Output: 37\n

Problem F : Turn On Lights!!!


Given a grid of lights(R X C), our goal is to turn on all the lights. 1 = on, 0 = off.Flipping a blub means, changing its state ( from on to off, or off to on ). If we flip a bulb(i,j), all the bulbs in a matrix consisting of rows [0..i] and columns [0..j] also get flipped. eg: 0110 00*1 0101 lets say bulb (1,2), marked as * is actually 0 and if you flip it the grid becomes 1000 1111 0101 (i.e., all (0,0) to (i,j) flipped ).

Objective : You have to turn on all lights using minimum flips. Constraints: 1 <= R,C <= 100 Time limit : 1s Input : First line contains R(1<=R<=100) and C(1<=C<=100) following line contains the matrix(R X C) input Output : Print the answer in a single line, followed by a '\n' ( print new line, no need to display '\n' )

Examples : Input : 23 111 101 Output : 4\n Input : 33 111 000 000 Output: 2\n

Problem G : Hangout
Dhookudu movie ki vellakunda lab ki enduku vacham raa devudaaa ! haha.. thats telugu and I'm sure most of you didn't understand it. How can you understand if you don't hangout with friends from different states. To make things better, your batchmates decided to form small groups, each group having exactly K students and no two of them in a group from same state. There are N states in India and you are given the number of students from each of them. How many maximum groups can you form ? Constraints: 1 <= N <= 100 2 <= K <= 100 1 <= A[i] <= 109 Time Limit : 1s Input: First line contains N and K. Second line contains the array A. A[i] is the number of students from ith state. Output: Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ). Note that you may need to use long long for the answer. Sample Case 1: Input: 43 2436 Output: 4\n Explanation: Let the 4 states be A, B, C, D and we have 2 from A, 4 from B, 3 from C and 6 from D. Each group should contain 3 students from different states. We can form the following groups {A,B,C}, {A,B,D}, {B,C,D}, {B,C,D}. Note that we can't form more than 4 groups, though there are 15 students in total. Sample Case 2: Input: 64 10 11 12 13 14 15 Output: 18\n Explanation: (A,B,D,E) 3 times (A,C,D,E) 3 times (A,C,D,F) 4 times (B,C,E,F) 5 times (B,D,E,F) 3 times