spoj --- ABCDEF

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;
}

转载于:https://www.cnblogs.com/zzuli2sjy/p/6647210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值