spoj --- ABCDEF
ABCDEF - ABCDEF
You are given a set S of integers between -30000 and 30000 (inclusive).
Find the total number of sextuples $(a,b,c,d,e,f):a,b,c,d,e,f \in S ,d!=0 $ that satisfy: \[\frac {a*b+c}{d} - e = f\]
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
思路:折半枚举+二分;
很容易想到是折半枚举,一开始用map记录的,超时了,后来自己手写二分.
复杂度(\(n^3*log(n^3)\));
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<string.h>
#include<map>
typedef long long LL;
using namespace std;
map<int,int>my1;
map<int,int>my2;
int ans[105];
int abc[1000005];
int low(int l,int r,int ask)
{ int id = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(ask <= abc[mid])
{
id = mid;
r = mid - 1;
}
else l = mid+1;
}
return id;
}
int high(int l,int r,int ask)
{
int id = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(ask >= abc[mid])
{
id = mid;
l = mid+1;
}
else
{
r = mid - 1;
}
}
return id;
}
int main(void)
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%d",&ans[i]);
int cn = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
for(int s = 0; s < n; s++)
{
int num = ans[i]*ans[j] + ans[s];
abc[cn++] = num;
}
}
}
sort(abc,abc+cn);
LL sum = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
for(int s = 0; s < n; s++)
{
if(ans[i] != 0)
{
int num = ans[i]*(ans[j] + ans[s]);
int x = high(0,cn-1,num) ;
int y =low(0,cn-1,num);
if(x >= y&&x!=-1&&y!=-1)
sum += x-y+1;
}
}
}
}
printf("%lld\n",sum);
return 0;
}