Special Prime
Give you a prime number p, if you could find some natural number (0 is not inclusive) n and m, satisfy the following expression:
We call this p a “Special Prime”.
AekdyCoin want you to tell him the number of the “Special Prime” that no larger than L.
For example:
If L =20
1^3 + 7*1^2 = 2^3
8^3 + 19*8^2 = 12^3
That is to say the prime number 7, 19 are two “Special Primes”.
Input
The input consists of several test cases.
Every case has only one integer indicating L.(1<=L<=10^6)
Output
For each case, you should output a single line indicate the number of “Special Prime” that no larger than L. If you can’t find such “Special Prime”, just output “No Special Prime!”
Sample Input
7
777
Sample Output
1
10
Hint
题意:
给你一个数L,问不超过L的范围内有多少素数可以满足上面是式子,其中mn都是非零自然数
分析:
我们已知式子
n3+p⋅n2=m3n3+p⋅n2=m3
观察所给的例子我们发现,好像n都是立方数
我们进一步给式子变形得到:
n2(p+n)=m3n2(p+n)=m3
我们观察到n都是立方数,那么只需要使得p+np+n 也是立方数就能满足式子
即设n=b3,p+n=a3n=b3,p+n=a3
所以 (b3)2⋅a3=(b2)3⋅a3=(b2a)3=m3(b3)2⋅a3=(b2)3⋅a3=(b2a)3=m3
因此我们可以得到:
p+b3=a3p+b3=a3
↓↓
p=a3−b3=(a−b)(a2+ab+b2)p=a3−b3=(a−b)(a2+ab+b2)
我们得到了素数p的一个乘积的形式
所以a−b=1a−b=1
即a=b+1a=b+1
我们带入原式得到:
p=(b+1)2+(b+1)b+b2=3b2+3b+1p=(b+1)2+(b+1)b+b2=3b2+3b+1
我们把右边变成完全平方的形式
两边先同乘12
12p=36b2+36b+1212p=36b2+36b+12
12p=36b2+36b+9+3=(6b+3)2+312p=36b2+36b+9+3=(6b+3)2+3
所以
12p−3=(6b+3)212p−3=(6b+3)2
因此我们只需判断12p-3是否是完全平方数即可其中p是素数
code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
bool isprime[maxn];
int prime[maxn];
int cnt;
void init(){
memset(isprime,true,sizeof(isprime));
isprime[0] = isprime[1] = false;
cnt = 0;
prime[1] = 0;
for(int i = 2; i < maxn; i++){
if(isprime[i]){
int p = 12 * i - 3;
int tmp = sqrt(p);
if(tmp * tmp == p) cnt++;
for(int j = i + i; j < maxn; j += i){
isprime[j] = false;
}
}
prime[i] = cnt;
}
}
int main(){
init();
int l;
while(~scanf("%d",&l)){
if(prime[l] == 0)
printf("No Special Prime!\n");
else
printf("%d\n",prime[l]);
}
return 0;
}