Special Prime HDU - 2866(数论)

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
问有在这个L范围内有多少的素数使得这个成立,一开始看感觉很难,最后还是做出来了
来一步一步分析:

n3+n2p=m3

显然 m>n ,并且写成
n2(n+p)=m3

然后我观察了一下hint,发现似乎每个n都是立方数,那么, n+p 也必须为立方数这样 n2(n+p) 就必然是一个立方数
设, n=b3 只要满足 b3+p=a3 ,到这里发现这个题和我之前做过的一道题类似了,则:
p=a3b3=(ab)(a2+ab+b2)

所以 ab=1 ,则:
p=3b2+3b+1
只要判断 12p3 是一个平方数即可

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1000005
using namespace std;
bool p[N];
int ans[N];
bool check(int x)
{
    int k=12*x-3;
    int m=(int)sqrt(k);
    return m*m==k;
}
void init()
{
    p[0]=p[1]=true;
    for(int i=2;i<N;i++)
    {
        if(!p[i])
            for(int j=i+i;j<N;j+=i)
                p[j]=true;
    }
    for(int i=1;i<=1000000;i++)
    {
        if(!p[i]&&check(i))
            ans[i]++;
        ans[i]+=ans[i-1];
    }
}

int main()
{
    init();
    int l;
    while(scanf("%d",&l)==1)
    {
        if(ans[l]==0)
            printf("No Special Prime!\n");
        else
            printf("%d\n",ans[l]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值