There are n balls in a bag, there is a number on each ball (the ball with the same number is no different). If a bag is lucky and only if the number of all balls is greater than the product of the number of all balls.
For example: If the number of the ball inside the bag is {1, 1, 2, 3}, this bag is lucky, because 1 + 1 + 2 + 3 > 1 * 1 * 2 * 3
You can remove some balls from the bag (you can remove 0, but don't remove it), so you're lucky to have the bag removed. Now you are programmed to calculate how many different lucky bags you can get.
Input Description:
The first line enters a positive integer n (n≤1000) second behavior n number of positive integer XI (xi≤1000)
Output Description:
The number of lucky bags the output can produce
Input Example:
31 1 1
Output Example:
2
The title can be translated into the number of true subsets of the set that meet the criteria. Each time a subset (bag) of elements (small balls) is selected from the full set. The number of set subsets is 2^n, and using DFS must time out. And this problem has duplicate elements, then search pruning. For any two positive integers a, a, or a, if a+b>a*b is satisfied, there must be a number of 1. Can be used to prove that: set A=1+x,b=1+y, then 1+x+1+y> (1+x) * (1+y),---> 1>x*y, then x, y must have a 0, that is, a, b There is one for 1. Extended to any k positive integer, assuming A1,A2,... AK, if the given condition is not satisfied, that is, and sum is less than or equal to the product pi, if at this time select a number B, can make it meet sum+b > pi*b, then B must be 1, and for the necessary non-sufficient conditions. Conversely, if the selected B>1, then Sum+b <=pi*b, that is A1,A2,..., Ak,b does not meet the given conditions. (Important basis for search pruning) Therefore, the ball is sorted in ascending order by label. Each time from small to large selection, when the choice to a1,a2,..., ak-1 to meet the given conditions, and then increase the selection of AK is not satisfied with the condition (AK must be greater than or equal to Max (A1,A2,..., ak-1)), continue to select a larger number, must not be satisfied! Therefore, pruning can be done. If there are more than 1, that is, when k=1, sum (1) >pi (1) is not sufficient, but the next element is still 1, it can satisfy the 1+1>1*1, so to determine whether the current AK equals 1. In addition, for repeating numbers, repeat.
import
java.util.*;
public
class
Main {
public
static
void
main(String[] args) {
Scanner scanner =
new
Scanner(System.in);
while
(scanner.hasNextInt()) {
int
n = scanner.nextInt();
int
[] nums =
new
int
[n];
for
(
int
i=
0
; i<n; i++)
nums[i] = scanner.nextInt();
Arrays.sort(nums);
System.out.println(find(nums,
0
,
0
,
1
));
}
}
private
static
int
find(
int
[] nums,
int
index,
long
sum,
long
multi) {
int
count =
0
;
for
(
int
i=index; i<nums.length; i++) {
sum += nums[i];
multi *= nums[i];
if
(sum > multi)
count +=
1
+ find(nums, i+
1
, sum, multi);
else
if
(nums[i] ==
1
)
count += find(nums, i+
1
, sum, multi);
else
break
;
sum -= nums[i];
multi /= nums[i];
while
(i<nums.length-
1
&& nums[i]==nums[i+
1
])
i++;
}
return
count;
}
}
[Programming questions] Lucky bag