hdu 6216 规律+二分

思路: 一个素数是否是立方的差, 只要判断是否是两个连续立方数的差。
在线规律查找](http://oeis.org/)
主要区分一下二分的几种写法

#include   
#include  <string.h>
#include 
#include  
using namespace std;
const int MAXN = 100005;
typedef long long LL;
int main()
{
    LL n;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld", &n);
        LL l=0, r=600000;
        LL mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)>n)
                r=mid-1;
            else
                l=mid+1;
            /*
            if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)<=n)
                l=mid+1;
            else
                r=mid-1;
            */
        }
        if(r*r*r-(r-1)*(r-1)*(r-1)==n)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}
#include   
#include  <string.h>
#include 
#include  
using namespace std;
const int MAXN = 100005;
typedef long long LL;
int main()
{
    LL n;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld", &n);
        LL l=0, r=600000;
        LL mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)mid+1;
            else
                r=mid-1;
            /*
            if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)>=n)
                r=mid-1;
            else
                l=mid+1;
             */
        }
        if(l*l*l-(l-1)*(l-1)*(l-1)==n)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}
#include   
#include  <string.h>
#include 
#include  
using namespace std;
const int MAXN = 100005;
typedef long long LL;
int main()
{
    LL n;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld", &n);
        LL l=0, r=600000;
        LL mid;
        while(lmid=(l+r)>>1;
            if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)>=n)
                r=mid;
            else
                l=mid+1;
            /*
             if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)mid+1;
            else
                r=mid;
            */
        }
        if(r*r*r-(r-1)*(r-1)*(r-1)==n)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}

你可能感兴趣的:(二分查找,规律)