Description
给个公式,求结果
见HDU 2973
Algorithm
见威尔逊定理
然后就是如果3k+7是质数,就是1,否则就是0
Code
#include <cmath>
#include <iostream>
using namespace std;
const int maxn = 1e6+9;
int ans[maxn] = {0};
bool ok(int x)
{
x = x * 3 + 7;
for (int i = 2; i <= sqrt(x); i++)
if (x % i == 0) return false;
return true;
}
void init()
{
for (int i = 1; i < maxn; i++)
{
ans[i] = ans[i - 1] + ok(i);
}
}
int main()
{
init();
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n;
cin >> n;
cout << ans[n] << endl;
}
}
本文介绍了一个用于判断特定形式质数的算法,并通过一个简单的C++程序实现了从1到n范围内所有符合条件的质数计数。该算法基于威尔逊定理,利用质数的定义来优化质数的检测过程。
8238

被折叠的 条评论
为什么被折叠?



