首页 > 其他 > 详细

特殊质数构造

时间:2014-12-12 13:19:15      阅读:181      评论:0      收藏:0      [点我收藏+]

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

 

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

 

     bubuko.com,布布扣

 

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

 

 

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

 

     bubuko.com,布布扣

 

     设bubuko.com,布布扣bubuko.com,布布扣,且有bubuko.com,布布扣bubuko.com,布布扣,那么继续得到

 

     bubuko.com,布布扣

 

     很明显bubuko.com,布布扣那么得到

 

     bubuko.com,布布扣

 

     继续化简得到

 

     bubuko.com,布布扣

 

     由于bubuko.com,布布扣是素数,那么bubuko.com,布布扣,且bubuko.com,布布扣,最终得到

 

     bubuko.com,布布扣

  

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

 

 

代码:

#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;
}


 

 

特殊质数构造

原文:http://blog.csdn.net/acdreamers/article/details/41864569

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!