美文网首页
A - Matrix (二分套二分)

A - Matrix (二分套二分)

作者: 陌路晨曦 | 来源:发表于2017-07-15 10:42 被阅读0次
    #include<stdio.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    #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 (二分套二分)

          本文链接:https://www.haomeiwen.com/subject/aspahxtx.html