特殊质数构造
題目: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; }
?
?
總結