LIGHT OJ 1370 欧拉函数(在小于自身的正整数中有n个与自身互素的数,这样的数最小的为n后第一个素数)...

本文介绍了一个使用C++实现的素数筛选算法,并通过一个具体的程序示例展示了如何标记合数来找出一定范围内的所有素数。该算法首先初始化一个足够大的整数数组,然后遍历数组,将每个素数的倍数标记为合数,以此来找到素数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 #include <cmath>
 7 using namespace std;
 8 typedef long long ll;
 9 const ll INF = -100000000000ll;
10 const double eps = 1e-8;
11 const int maxn = 1e6 + 10;
12 const double PI = acos(-1);
13 int pre[maxn];
14 void init(){
15     pre[1] = 1;
16     for(int i = 2;i <= 1000005;i++)
17         if(!pre[i]){
18             for(int j = i*2;j <= 1000005;j += i)
19                 pre[j] = 1;
20         }
21 
22 }
23 int main()
24 {
25   //  freopen("in.txt","r",stdin);
26     init();
27     int T,cas = 0;
28     cin>>T;
29     while(T--){
30         int n,a;
31         cas++;
32         scanf("%d",&n);
33         ll ans = 0;
34         for(int i = 1;i <= n;i++){
35             scanf("%d",&a);
36             a++;
37             while(pre[a]) a++;
38            // cout<<i<<" "<<a<<endl;
39             ans += a;
40         }
41 
42         printf("Case %d: %lld Xukha\n",cas,ans);
43     }
44 
45     return 0;
46 }

 

转载于:https://www.cnblogs.com/shimu/p/5927457.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值