Special Prime HDU - 2866(数论)

本文介绍了一种特殊的素数——特殊素数,这类素数可以通过特定的数学表达式找到,并给出了解决方案。通过分析和编程实现,我们能够找出在一定范围内所有符合条件的特殊素数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值