6216 A Cubic number and A Cubic Number 立方差為素數 兩數相差一定為1 相差為1的立方差不定為素數
A cubic number is the result of using a whole number in a multiplication three times. For example, 3×3×3=273×3×3=27 so 2727 is a cubic number. The first few cubic numbers are 1,8,27,641,8,27,64 and 125125. Given an prime number pp. Check that if pp is a difference of two cubic numbers.
Input
The first of input contains an integer T (1≤T≤100)T (1≤T≤100) which is the total number of test cases. For each test case, a line contains a prime number p (2≤p≤1012)p (2≤p≤1012).
Output
For each test case, output 'YES' if given pp is a difference of two cubic numbers, or 'NO' if not.
Sample Input
10 2 3 5 7 11 13 17 19 23 29
Sample Output
NO NO NO YES NO NO NO YES NO NO
題解: a^3 - b^3 = p p為素數 a^3 - b^3 = (a - b)(a^2 + a*b + b^2) 所以 (a-b)為1
所以 a=b+1 帶入即可 為3*b^2+3*b+1 暴力可過, 儲存後二分找也可
結論:立方差為素數 兩數相差一定為1 相差為1的立方差不定為素數(8^3-7^3=512-343=169)
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; const int N = 1e5+10; ll n; int main(){ int T; int nn = 1; scanf("%d", &T); while(T--){ scanf("%lld",&n); int flag = 0; for(ll i = 2; i <= 1000000; i++){ if(3 * i * i + 3 * i + 1 == n){ flag = 1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }