数论专题hdu2866

  

  本题题意:给出一个公式n^3 + n^2*p = m^3,给定L,求出不大于L的满足该公式的p的个数。

  代码如下:

  

/*

    1    m= i^6    n= (1^2 + 1^3)^3
    2    m= i^6    n= (2^2 + 2^3)^3
    3    m= i^6    n= (3^2 + 3^3)^3
    .
    .
    .
    i    m= i^6    n= (i^2 + i^3)^3

    then p = n/m - i^3 = (i^2)^3*(1+i)^3 / m - i^3 = (1+i)^3 - i^3

    then p1 = 7,p2 = 19,p3 = 37,p4 = 61,p5 = 91,...

    But this is true?

    Of course not,let's see the p5,it's not a prime.

    Oh,we forget that the problem also need the number is a prime.

    then we make a prime table,then the answer is true.

*/

#include <iostream>
using namespace std;
typedef long long ll;
const int Max = 1000000;
int a[Max+1];
ll res[Max+1];
ll prime[Max+1];
ll pow3(ll a){return a * a * a;}
void Prime()
{
    int t = 2;
    while(true)    
    {
        for(int i=t+t;i<=Max;i+=t)
        {
            prime[i] = 1;
        }
        int i;
        for(i=t+1;i<=Max;i++)
        {
            if(prime[i] == 0)
            {
                t = i;
                break;
            }
        }
        if(i == Max+1)break;
    }
}
int Res()
{
    int t = 1;
    int l = 1;
    while(true)
    {
        ll temp = pow3(1+t) - pow3(t);
        if(temp >= Max)break;
        if(prime[temp] == 0)
        {
            res[l] = temp;
            l++;    
        }
        t++;
    }
    return l-1;
}
int main(){
    Prime();
    int l = Res();
    for(int i=0;i<l;i++)
    {
        for(int j=res[i];j<res[i+1];j++)
        {
            a[j] = i;
        }    
    }
    int n;
    while(cin >> n)
    {
        if(n < 7)cout << "No Special Prime!" << endl;
        else if(n < res[l])cout << a[n] << endl;
        else cout << l << endl;
    }
    return 0;
}

这道题就是找规律,然后要注意除了满足该公式外还要满足p是素数,然后就对了。

 

posted @ 2017-09-18 23:28  mtl6906  阅读(114)  评论(0编辑  收藏  举报