hdu 6216 规律+二分

来源:互联网 发布:软件项目管理发展前景 编辑:程序博客网 时间:2024/06/11 06:09

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

#include   <stdio.h>#include  <string.h>#include <iostream>#include  <vector>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   <stdio.h>#include  <string.h>#include <iostream>#include  <vector>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)                l=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   <stdio.h>#include  <string.h>#include <iostream>#include  <vector>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;            else                l=mid+1;            /*             if(mid*mid*mid-(mid-1)*(mid-1)*(mid-1)<n)                l=mid+1;            else                r=mid;            */        }        if(r*r*r-(r-1)*(r-1)*(r-1)==n)            puts("YES");        else            puts("NO");    }    return 0;}
原创粉丝点击