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”.

InputThe input consists of several test cases.

Every case has only one integer indicating L.(1<=L<=10^6)

OutputFor 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的p可以使上面的等式成立,而且p得为素数;
解题思路:由于直接看那个等式,首先我们在数学的角度上一个等式有桑未知数,而求其中一个未知数的解的个数,那么我们可以首先把其他两个未知数变成只要p成立那个就一定可以找的到;
1.首先由n ^ 3 + p * n ^ 2 =m ^ 3,变成 n^ 2 * (p+n)=m^ 3;由立方数的特性和所给的样列来看,n是一个数的立方;
2.n=b^ 3,那么p =a^ 3-b^3;因为:a=m/( b ^2 ),所以能得到这个公式,
3.p=(a -b)( a^ 2+ab+b^ 2);由于p是一个素数,所以a-b=1,a=b+1;
4.p=3b^ 2+3b+1;带a=b+1所得;
5.12p-3=(6b+3)^ 2;
总结:所以只要素数乘以12减3能够满足是某个数的平方即可;

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
#define maxn 1000005
bool s[maxn];
int ans[maxn];
int cnt;
void sove()
{
    memset(s,true,sizeof(s));
    s[0]=s[1]=false;
    cnt=0;
    ans[1]=0;
    for(int i=2;i<maxn;i++)
        {
            if(s[i])
                {
                    int p=12*i-3;
                    int d=sqrt(p);
                    if(d*d==p) cnt++;
                    for(int j=i+i;j<maxn;j+=i)
                        s[j]=false;
                }
            ans[i]=cnt;
        }
}
int main()
{
    sove();
    int l;
    while(scanf("%d",&l)!=EOF)
        {
            if(ans[l]==0) cout<<"No Special Prime!"<<endl;
            else cout<<ans[l]<<endl;
        }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值