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