#243. 特定条件分组求和
特定条件分组求和
Grouping Sum with Specific Conditions
Background
Congcong has recently been studying how to cleverly group a set of numbers. He wants to know if it's possible to divide these numbers into two groups such that the sum of the two groups is the same, while satisfying specific conditions.
Problem Description
Given an array of integers, determine if it's possible to divide these integers into two groups such that the sum of the two groups is the same. Additionally, the following constraints must be met: all values that are multiples of must be in one group, and all values that are multiples of (and not multiples of ) must be in the other. (No loops needed.)
Input Format
The input is given from standard input in the following format.
The input consists of a single line containing space-separated integers, representing the elements of an array.
Output Format
Output to standard output in the following format.
Output
trueif it's possible to divide the integers into two groups with equal sums according to the requirements; otherwise, outputfalse.
Sample
1 1
true
1 1 1
false
2 4 2
true
Sample Explanation
Sample 1: Input 1 1.
- There are no multiples of or in the array.
- The total sum is , and the target sum for each group is .
- We can put the first into group 1 and the second into group 2. Both groups have a sum of , which are equal. So, the output is
true.
Sample 2: Input 1 1 1.
- There are no multiples of or in the array.
- The total sum is . Since the total sum is odd, it cannot be divided into two groups with equal sums. So, the output is
false.
Sample 3: Input 2 4 2.
- There are no multiples of or in the array.
- The total sum is , and the target sum for each group is .
- We can put the first and the third into group 1, making its sum ; and put into group 2, making its sum . The sums of both groups are equal. So, the output is
true.
Constraints
Time limit: second, Memory limit: KiB for each test case.