Problem Description
x^3-y^3
=(x^3-x^2*y)+x^2*y-(y^3-x*y^2)-x*y^2
=x^2(x-y)-y^2(y-x)+xy(x-y)
=(x-y)(x^2+xy+y^2)=p(p是质数)——> x-y=1以及x^2+xy+y^2=p
代入消元:p=3x^2+3x+1
【代码】:
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<map> #include<set> #include<string> using namespace std; int main() { int t,p,flag; scanf("%d",&t); while(t--) { flag=0; scanf("%d",&p); for(int i=1;i<=1e6+10;i++) { if(3*i*i+3*i+1==p) { flag=1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> #include <string> #define ll long long using namespace std; const int MAXN = 1e6 + 5; ll tab[MAXN]; void init() { for (ll i = 0;i < MAXN;++i) tab[i] = 3 * i*i + 3 * i + 1; } int main() { int T; scanf("%d", &T); init(); for (int i = 1;i <= T;++i) { bool flag = false; ll v; scanf("%I64d", &v); int left = 0, right = MAXN-1; int mid = (left + right) >> 1; while (left <= right) { if (v == tab[mid]) { flag = true; break; } else if (v > tab[mid]) left = mid + 1; else right = mid - 1; mid = (left + right) >> 1; } if (flag) printf("YES\n"); else printf("NO\n"); } //system("pause"); return 0; }
HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】
原文:http://www.cnblogs.com/Roni-i/p/7538553.html