## Thursday, August 11, 2016

### [SPOJ] ABCDEF – ABCDEF | 书脊

[SPOJ] ABCDEF – ABCDEF | 书脊
http://www.spoj.com/problems/ABCDEF/
You are given a set S of integers between -30000 and 30000 (inclusive).
Find the total number of sextuples  that satisfy:

### Input

The first line contains integer N (1 ≤ N ≤ 100), the size of a set S.
Elements of S are given in the next N lines, one integer per line. Given numbers will be distinct.

### Output

Output the total number of plausible sextuples.

### Examples

 ```Input: 1 1 Output: 1``` ```Input: 2 2 3 Output: 4``` ```Input: 2 -1 1 Output: 24``` ```Input: 3 5 7 10 Output: 10```

1. 这里是每个元素, 包括重复的.
2. SPOJ真是卡空间, 用map直接TLE, 好好自己写counter吧.
3. d不能是0
http://neverendingbits.blogspot.com/2011/12/spoj-4580-abcdef.html
The Proper Approach:
N<=100 => N^3<=10^6 numbers which can easily be stored in an array.
Rearrange the equation as (a*b+c)=d*(e+f)
• Find the values of (a*b+c) for all possible sets (a,b,c) and store them in an array arr1 --> O(N^3) time.
• Sort arr1 --> O(N^3 log(N^3)) = O(N^3 logN) time.
• Find the values of (d*(e+f)) for all possible sets (d,e,f) and store them in an array arr2 --> O(N^3) time.
• Sort arr2 --> O(N^3 log(N^3)) = O(N^3 logN) time.
• Now perform an intersection operation on the two arrays, arr1 and arr2, realizing that two same values in an array come from two distinct subsets of either (a,b,c) or (d,e,f) and thus contribute to two different sets (a,b,c,d,e,f).
This all takes O(N^3 logN) time (about 10^7 operations) manageable within a second :)

public void solve(int testNumber, InputReader in, OutputWriter out) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
}
Arrays.sort(nums);
int[] left = new int[n*n*n];
int p = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
for (int k = 0; k < nums.length; k++) {
left[p++] = nums[i] * nums[j] + nums[k];
}
}
}
Arrays.sort(left, 0, n*n*n);
int[][] range = new int[2][left.length];
p = 0;
long count = 0;
int tmp = Integer.MAX_VALUE;
for (int i = 0; i < left.length; i++) {
if (tmp != left[i]){
range[0][p] = left[i];
range[1][p]++;
p++;
}
else {
range[1][p-1]++;
}
tmp = left[i];
}
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
for (int k = 0; k < nums.length; k++) {
if (nums[i] == 0)
continue;
else {
int right = nums[i]*(nums[j]+nums[k]);
int b = Arrays.binarySearch(range[0], 0, p, right);
if (b >= 0)
count+= range[1][b];
}
}
}
}
out.print(count);
}

https://github.com/srikk595/Spoj-Problems/blob/master/ABCDEF-6726594-src.cpp
int main()
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
for (int k=0;k<n;k++)
{
s1.push_back(x[i]*x[j]+x[k]);
}
}
}
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
for (int k=0;k<n;k++)
{
if (x[k]==0) continue;
s2.push_back((x[i]+x[j])*x[k]);
}
}
}
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
for (int i=0;i<s1.size();i++)
{
lo=lower_bound(s2.begin(),s2.end(),s1[i])-s2.begin();
hi=upper_bound(s2.begin(),s2.end(),s1[i])-s2.begin();
res+=(hi-lo);
}
printf("%lld\n",res);
return 0;
}