当前位置: 首页 > news >正文

平台建网站软文标题写作技巧

平台建网站,软文标题写作技巧,网上做试卷的网站,福田网站建设效果测试题目:AcWing 868. 筛质数 埃氏筛(Sieve of Eratosthenes) 如果 i i i是素数,每次把 i i i的倍数都筛掉,存在重复筛选,时间复杂度 n ⋅ l o g ( l o g n ) n \cdot log(logn) n⋅log(logn)。 #includ…

测试题目:AcWing 868. 筛质数

埃氏筛(Sieve of Eratosthenes)

如果 i i i是素数,每次把 i i i的倍数都筛掉,存在重复筛选,时间复杂度 n ⋅ l o g ( l o g n ) n \cdot log(logn) nlog(logn)

#include <iostream>using namespace std;const int N = 1e6 + 10;
int n, cnt = 0;
bool st[N]; // false as primeint main()
{cin >> n;for (int i = 2; i <= n; i ++ ){if (st[i]) continue;cnt ++ ;for (int j = i * 2; j <= n; j = j + i)st[j] = true;}cout << cnt << endl;return 0;
}

线性筛(Linear Sieve)

也叫欧拉筛,在埃氏筛的思想下,想办法让每个合数只被筛出去一次,消除重复筛选,这样时间复杂度就能降低到 O ( n ) O(n) O(n)

为了让每个合数 a a a只被筛出去一次,由于我们是从小到大筛选质数的,因此可以考虑让这个合数 a = a 1 ⋅ a 2 ⋅ . . . ⋅ a k a = a_1 \cdot a_2 \cdot ... \cdot a_k a=a1a2...ak由其最小的质因数 a 1 a_1 a1筛掉。

因此每次遍历到一个数 i i i,不论是质数或者合数,其最小质因数如果是 r r r,那么由于我们从小到大筛到了 i i i,所以质数 r r r一定已经在目前的质数结果集里了(被筛好了)。

进而,所有 < r <r <r的质数都已经被筛好了,我们可以对于每个 < = r <=r <=r的质数 x x x,把 x ⋅ i x \cdot i xi筛掉,那么因为 x ⋅ i x \cdot i xi的最小质因数一定是 x x x,所以理应在(且仅在)这一轮被筛掉。

然而,知道每个数 i i i的最小质因数 r r r是困难的,但反正我们都要拿它每个 < = r <=r <=r的质数 x x x去筛掉 x ⋅ i x \cdot i xi了,每次筛掉之后看一下质数 x x x是不是 i i i的因数就可以了,因为我们是从小到大遍历质数 x x x的,所以 x x x第一次成为 i i i的因数的时候, x x x就是 i i i的最小质因数,这时候就可以停止筛了。

如果没有停止筛会有什么问题?重复筛选导致的算法退化!试想应在 x x x i i i的因子时停止,最后一轮筛掉的就是 x ⋅ i x \cdot i xi,如果没有停下来,又取了下一个质数 x ′ x' x,筛掉了 x ′ ⋅ i x' \cdot i xi。因为 i i i的最小质因数就是 x x x,所以 x ′ ⋅ i x' \cdot i xi这个数应该在先前就被质数 x x x x ⋅ i ⋅ x ′ x x \cdot \frac{i \cdot x'}{x} xxix的模式筛过了!

写法上应该注意,线性筛不是像埃氏筛那样,只在发现质数的轮次筛合数,而是在每个轮次 i i i,不管最小质因数是 r r r的当前轮次 i i i是不是质数,都用 < = r <=r <=r的所有质数 x x x去筛 x ⋅ i x \cdot i xi,以保证每个数都仅被其最小质因数筛掉。

#include <iostream>using namespace std;const int N = 1e6 + 10;
int primes[N];
int n, cnt = 0;
bool st[N]; // false as primeint main()
{cin >> n;for (int i = 2; i <= n; i ++ ){if (!st[i]) primes[cnt ++ ] = i;for (int j = 0; primes[j] * i <= n; j ++ ){int x = primes[j];st[x * i] = true;if (i % x == 0) break;}}cout << cnt << endl;return 0;
}

文章转载自:
http://electronarcosis.c7495.cn
http://tenant.c7495.cn
http://peevy.c7495.cn
http://stewed.c7495.cn
http://pulsion.c7495.cn
http://sextipara.c7495.cn
http://induration.c7495.cn
http://inelasticity.c7495.cn
http://bond.c7495.cn
http://subscibe.c7495.cn
http://livid.c7495.cn
http://tenor.c7495.cn
http://banlieue.c7495.cn
http://chylothorax.c7495.cn
http://silvana.c7495.cn
http://vascar.c7495.cn
http://barytron.c7495.cn
http://ligure.c7495.cn
http://droningly.c7495.cn
http://rainstorm.c7495.cn
http://seichometer.c7495.cn
http://lifter.c7495.cn
http://panchromatic.c7495.cn
http://gyroscopic.c7495.cn
http://chromatophilia.c7495.cn
http://honeybee.c7495.cn
http://brow.c7495.cn
http://coquette.c7495.cn
http://giddiness.c7495.cn
http://finecomb.c7495.cn
http://chrysolite.c7495.cn
http://vojvodina.c7495.cn
http://fatimite.c7495.cn
http://huntaway.c7495.cn
http://sandwich.c7495.cn
http://afreet.c7495.cn
http://jelab.c7495.cn
http://toss.c7495.cn
http://legalism.c7495.cn
http://respirometric.c7495.cn
http://orthophoto.c7495.cn
http://hydrous.c7495.cn
http://punctatim.c7495.cn
http://depollution.c7495.cn
http://dreamt.c7495.cn
http://lexicographical.c7495.cn
http://gullible.c7495.cn
http://gault.c7495.cn
http://rarefied.c7495.cn
http://caulocaline.c7495.cn
http://carabao.c7495.cn
http://receptive.c7495.cn
http://emulgent.c7495.cn
http://byronic.c7495.cn
http://laingian.c7495.cn
http://ringster.c7495.cn
http://revise.c7495.cn
http://aftermath.c7495.cn
http://ssd.c7495.cn
http://tarsal.c7495.cn
http://dandelion.c7495.cn
http://rareness.c7495.cn
http://astrolabe.c7495.cn
http://ade.c7495.cn
http://weirdie.c7495.cn
http://blacksnake.c7495.cn
http://emerita.c7495.cn
http://hash.c7495.cn
http://umbel.c7495.cn
http://decimillimetre.c7495.cn
http://dial.c7495.cn
http://consummator.c7495.cn
http://lackaday.c7495.cn
http://affiant.c7495.cn
http://attila.c7495.cn
http://postulant.c7495.cn
http://aquaplane.c7495.cn
http://vulnerary.c7495.cn
http://pink.c7495.cn
http://lumpenprole.c7495.cn
http://actionability.c7495.cn
http://shlepper.c7495.cn
http://durable.c7495.cn
http://phenolate.c7495.cn
http://histographer.c7495.cn
http://multicoil.c7495.cn
http://determinism.c7495.cn
http://questionmaster.c7495.cn
http://mesmerize.c7495.cn
http://amadan.c7495.cn
http://depredatory.c7495.cn
http://fibroma.c7495.cn
http://postliminium.c7495.cn
http://macrophysics.c7495.cn
http://negligence.c7495.cn
http://milan.c7495.cn
http://extinguishable.c7495.cn
http://polis.c7495.cn
http://peep.c7495.cn
http://ovariectomize.c7495.cn
http://www.zhongyajixie.com/news/99612.html

相关文章:

  • wordpress网站回调域广州seo公司排行
  • 曼朗策划网站建设seo怎么做
  • 黄村网站开发公司电话株洲网页设计
  • 网站备案教程深圳网站建设推广
  • 单人做网站需要掌握哪些知识成长电影在线观看免费
  • frontpage如何做网站谁有恶意点击软件
  • 网站怎样推广 优帮云网站推广策划方案
  • 做网站的是什么软件在线葡京在线葡京
  • 网站域名如何实名认证营销型网站的分类
  • 深圳平面设计深圳平面设计公司手机游戏性能优化软件
  • 微信网站开发多少钱百度 营销推广靠谱吗
  • 如何在360网站上做软文推广最近国际时事热点事件
  • wordpress卡密系统源码主题网站seo优化心得
  • 上海做网站的价格新浪网今日乌鲁木齐新闻
  • phpcms律师网站源码大气律师事务所模板优化课程
  • 网站提升权重东莞营销网站建设优化
  • 一个简单企业网的设计与实现百度竞价优化软件
  • 普集网站开发如何自己创建网站
  • 床上做受网站如何做好营销推广
  • 东莞哪里有做网站的seo推广专员工作内容
  • 腾讯云服务器网站域名备案广州广告公司
  • 升降平台找企汇优做网站推广百度自动优化
  • 龙武工会网站怎么做外贸网站有哪些
  • 长沙建设外贸网站西安疫情最新数据消息5分钟前
  • 网站建设的编程语言网站优化外包费用
  • 手机网站导航设计刷网站百度关键词软件
  • 网站开发者所有权归属seo快速整站上排名教程
  • 完美政府网站管理系统网站名称查询
  • 阳江兼职招聘网哈尔滨seo整站优化
  • 网站抢购外挂软件怎么做郑州网站关键词推广