1462 素数和

1462 素数和

 

时间限制: 1 s
空间限制: 64000 KB
题目等级 : 青铜 Bronze
 
 
 
题目描述 Description

给定2个整数a,b 求出它们之间(不含a,b)所有质数的和。

输入描述 Input Description

一行,a b(0<=a,b<=65536)

输出描述 Output Description

一行,a,b之间(不含a,b)所有素数的和。

样例输入 Sample Input

39 1224

样例输出 Sample Output

111390

数据范围及提示 Data Size & Hint

注意没有要求a<b

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 int vis[65537];
 6 int main()
 7 {
 8     int a,b;
 9     cin>>a>>b;
10     if(a>b)swap(a,b);
11     for(int i=2;i<=sqrt(b);i++)
12      {
13          if(vis[i]==0)
14           {
15               for(int j=i*2;j<=b;j+=i)
16                {
17                    vis[j]=1;
18                }
19           }
20      }
21      vis[1]=1;
22      vis[0]=1;
23      long long tot=0;
24      for(int i=a;i<=b;i++)
25       {
26           if(vis[i]==0)
27            {
28                tot+=i;
29            }
30       }
31       cout<<tot;
32 }
原文地址:https://www.cnblogs.com/lyqlyq/p/6745125.html