题目链接:HDU6216
手推一下
a^3-b^3=(a-b)(a^2+ab+b^2);
经过推导
只有当a=b+1的时候 立方差可能会出现质数,除此之外,不可能存在质数。(如有错误请指正)
所以打个表,然后用二分查找 lower_bound 去找,如果找到则返回YES 否则返回N0
扩展一下:
ACcode:
/*
2017年9月17日19:30:04
HDU6216 找规律
AC
*/
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const ll maxn=1e6;
vector<ll> vc;
ll i;
void tb(){
ll tmp;
/*如果不写这个会RE*/
tmp=0;
i=1;
for(ll j=1;tmp<=1e12;j++){
tmp=3*j*j+3*j+1;
vc.push_back(tmp);
}
}
int main(){
int t;
scanf("%d",&t);
tb();
while(t--){
ll n;
scanf("%lld",&n);
ll pos=lower_bound(vc.begin(),vc.end(),n)-vc.begin();
if(vc[pos]==n) printf("YES\n");
else printf("NO\n");
}
return 0;
}