#239. 带约束的组合求和

    ID: 239 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatRecursion-2gesp6DFS递归

带约束的组合求和

Group Sum with Special Constraints

Background

Congcong has been studying how to select specific combinations from a set of numbers. He encountered an interesting problem that requires your help to solve.

Problem Description

Given an array of integers numsnums and a target sum targettarget, determine if it's possible to choose a group of some of the integers from the array such that their sum equals targettarget, with these additional constraints:

  1. All multiples of 55 in the array must be included in the group.
  2. If the value immediately following a multiple of 55 is 11, it must not be chosen. (No loops needed.)

Input Format

The input consists of a single line. This line contains an integer start_indexstart\_index, a list of integers numsnums enclosed in square brackets, and an integer targettarget. In this problem, start_indexstart\_index will always be 00.

start_index [num1 num2 ... numK] target

Output Format

Output to standard output in the following format.

Output true if such a group exists; otherwise, output false.

Sample

0 [2 5 10 4] 19
true
0 [2 5 10 4] 17
true
0 [2 5 10 4] 12
false

Sample Explanation

Sample 1: For input 0 [2 5 10 4] 19, the multiples of 55 in the array are 55 and 1010, which must be included. Their sum is 1515. The target sum is 1919, so 44 more is needed. Choosing 44 from the remaining elements 2,42, 4 achieves the target. The chosen group is 5,10,45, 10, 4, with a sum of 1919. Thus, true is output.

Sample 2: For input 0 [2 5 10 4] 17, the multiples of 55 in the array are 55 and 1010, which must be included. Their sum is 1515. The target sum is 1717, so 22 more is needed. Choosing 22 from the remaining elements 2,42, 4 achieves the target. The chosen group is 5,10,25, 10, 2, with a sum of 1717. Thus, true is output.

Sample 3: For input 0 [2 5 10 4] 12, the multiples of 55 in the array are 55 and 1010, which must be included. Their sum is 1515. Since 1515 is already greater than the target sum 1212, it's impossible to reach the target by adding non-negative numbers. Thus, false is output.

Constraints

Time limit: 11 second, Memory limit: 10241024 KiB for each test case.