[二分] PKU 1840 Eqs

二分:3+2;

# include <stdio.h>

# include <stdlib.h>



# define trq(x) ((x)*(x)*(x))



int a[5];

long long ans = 0;

int h[105];

int f[305*105*105], g[305*105];



/************************************************************/

int cmp(const void *x, const void *y)

{

    return *(int*)x - *(int*)y;

}



int bins(int *v, int key, int n)

{

    int low = 0, high = n-1, mid, x, y;

    if (key<v[0] || key>v[high])

        return 0;

    while (low+1 < high)

    {

        mid = low + ((high-low)>>1);

        if (v[mid] >= key) high = mid;

        else low = mid;

    }

    x = (v[low]==key ? low-1:low);

    low = 0, high = n-1;

    while (low+1 < high)

    {

        mid = low + ((high-low)>>1);

        if (v[mid] <= key) low = mid;

        else high = mid;

    }

    y = (v[high]==key ? high:high-1);

    return y-x;

}



/************************************************************/



int main()

{

    int i, j, k, n = 0, m = 0;

    

    for (i = 0; i < 5; ++i) scanf("%d", &a[i]);

    for (i = -50; i <= 50; ++i) if (i != 0)

    for (j = -50; j <= 50; ++j)    if (j != 0)

    {

        g[n++] = trq(i)*a[3]+trq(j)*a[4];

        for (k = -50; k <= 50; ++k) if (k != 0)

            f[m++] = trq(i)*a[0]+trq(j)*a[1]+trq(k)*a[2];

    }

    qsort(g, n, sizeof(g[0]), cmp);

    qsort(f, m, sizeof(f[0]), cmp);

    

    for (i = 0; i < n; ++i)

        ans += bins(f, -g[i], m);

    printf("%lld\n", ans);



    return 0;

}

 

你可能感兴趣的:(pku)