现在的位置: 首页 > 综合 > 正文

特殊质数构造

2019年09月01日 ⁄ 综合 ⁄ 共 879字 ⁄ 字号 评论关闭

题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1226

 

题目:https://projecteuler.net/problem=291

 

题意:如果质数可以由如下公式构造出来,那么称质数可造的

 

    

 

     给定一个区间,求在这个区间内有多少个可造的质数,其中

 

 

分析:把原式进行化简得到

 

    

 

     设,且有,那么继续得到

 

    

 

     很明显那么得到

 

    

 

     继续化简得到

 

    

 

     由于是素数,那么,且,最终得到

 

    

  

     接下来,可以利用Brahmagupta–Fibonacci identity原理进行筛选。

 

     最后附论文一篇:http://ami.ektf.hu/uploads/papers/finalpdf/AAPASM_25_from39to53.pdf

 

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long LL;

LL Solve(LL a, LL b)
{
    vector<LL> v;
    for(LL i = 0; ;i++)
    {
        LL t = 2 * i * i + 2 * i + 1;
        if(t > b) break;
        v.push_back(t);
    }

    int cnt = 0;
    for(LL i = 1; i < v.size(); i++)
    {
        LL t = 2 * i * i + 2 * i + 1;
        LL x = v[i];
        if(t == x && t >= a)
            cnt++;
        if(x > 1)
        {
            for(int j = i; j < v.size(); j += x)
                while(v[j] % x == 0)
                    v[j] /= x;
            for(int j = x - i - 1; j < v.size(); j += x)
                while(v[j] % x == 0)
                    v[j] /= x;
        }
    }
    return cnt;
}

int main()
{
    LL a, b;
    while(cin >> a >> b)
        cout << Solve(a, b) << endl;
    return 0;
}

 

 

【上篇】
【下篇】

抱歉!评论已关闭.