题目
The math department has been having problems lately. Due to immense amount of unsolicited automated programs which were crawling across their pages, they decided to put Yet-Another-Public-Turing-Test-to-Tell-Computers-and-Humans-Apart on their webpages. In short, to get access to their scientific papers, one have to prove yourself eligible and worthy, i.e. solve a mathematic riddle.
However, the test turned out difficult for some math PhD students and even for some professors. Therefore, the math department wants to write a helper program which solves this task (it is not irrational, as they are going to make money on selling the program).
Input
The first line contains the number of queries t (t ≤ 10^6). Each query consist of one natural number n (1 ≤n ≤10^6).
Output
For each n given in the input output the value of Sn.
Sample Input
13
1
2
3
4
5
6
7
8
9
10
100
1000
10000
Sample Output
0
1
1
2
2
2
2
3
3
4
28
207
1609
然而,对于一些数学博士生甚至一些教授来说,测试结果很难。 因此,数学系想要编写一个帮助程序来解决这个任务(这不是不合理的,因为他们会在出售程序时赚钱)。
代码如下:
#include
#include
#include
#include
using namespace std; const int mx=3e6+10; int s[mx]; int isprime[mx]; void init()//这里初始化,求出每个i对应的数字是否为素数,然后求和 {
memset(isprime,0,sizeof(isprime)); for(int i=2;i<mx;i++) {
if(!isprime[i])//最普通的埃氏筛法 {
for(int j=2*i;j<mx;j+=i) isprime[j]=1; } } memset(s,0,sizeof(s)); s[0]=0,s[1]=0; for(int i=2;i<=1e6;i++)//求和 {
int re=3*i+7; s[i]=s[i-1]+(1-isprime[re]); } } int main() {
int t,n; init(); scanf("%d",&t); while(t--) {
scanf("%d",&n); printf("%d\n",s[n]);//直接输出 } return 0; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215630.html原文链接:https://javaforall.net
