A - Matrix (二分套二分)

#include
#include
#include
#include
#include
#define maxn 1e12
#define LL long long 
using namespace std;

LL n,m,le,ri,mid;

LL res(LL i , LL j)
{
    return i*i + j*j + 100000*i - 100000*j + i*j;
}
LL check(LL x)
{
    LL cnt = 0,mid,le,ri;
    for(int i=1;i<=n;i++)
    {
        le = 1;
        ri = n+1;
        mid = le + (ri-le)/2;
        while(le < ri)
        {
            if(res(mid, i) < x)
            {
                le = mid+1;
            }
            else
            {
                ri = mid;
            }
            mid = le + (ri- le)/2;
        }
        cnt += (mid - 1);
    }
    return cnt;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        
        scanf("%lld%lld",&n,&m);
        
        ri = maxn,le = -maxn;
        mid = le + (ri - le)/2;
        while(le < ri)
        {
            if(check(mid) < m)
            {
                le = mid + 1;
            }
            else
            {
                ri = mid;
            }
            mid = le + (ri - le)/2;
            //printf("mid = %d\n",mid);
        }
        printf("%lld\n",mid-1);
    }
}

你可能感兴趣的:(A - Matrix (二分套二分))