跳到正文

算法·理论

数学

sto_ycy_orz约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年3月7日
收录
2026年3月16日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
1

版本

  1. v2当前
  2. v1

埃氏筛法

C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
int a[MAXN];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);//加速输入
	int n;
	cin>>n;
	a[0]=0;
	a[1]=0;//将0和1标记为非质数
	for(int i=2;i<=n;i++)
	{
		a[i]=1;//剩余数标记为质数
	} 
	for(int i=2;i<=sqrt(n);i++)
	{
		if(a[i])//如果i是质数
		{
			for(int j=2*i;j<=n;j+=i)
			{
				a[j]=0;//将所有的i的倍数标记为非质数
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		cout<<i<<" "<<a[i]<<endl;
	}
	return 0;
}

埃氏筛法的时间复杂度是 O(nloglogn)


欧拉筛


C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e6+10;
int prime[MAXN];
bool b[MAXN];
int cnt=0;
int n;
void prime_ycy()
{
	b[1]=1;//将1标记为非质数
	for(int i=2;i<=n;i++)
	{
		if(!b[i])
		{
			prime[++cnt]=i;//如果i是质数,放进prime数组
		}
		for(int j=1;j<=cnt&&i*prime[j]<=n;j++)
		{
			b[i*prime[j]]=1;//将i的倍数标记为非质数
			if(i%prime[j]==0)
			{
				break;
			}
		}
	}
	return;
}
int main()
{
	cin>>n;
	prime_ycy();
	for(int i=1;i<=cnt;i++)
	{
		cout<<prime[i]<<" ";//输出质数
	}
	return 0;
} 

欧拉筛(线性筛)的时间复杂度是严格的O(n)


约数

约数个数

例如12有质因数:2,3.

有因数:1,2,3,4,6,12(共六个)

可以将12看作2* 2 * 3,因数个数则为(指数+1(0的情况))* (指数+1(0的情况))

C++
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin>>n;
	unordered_map<int,int>primes;//定义map存储底数及指数
	while(n--)
	{
		int x;
		cin>>x;
		for(int i=2;i<=x/i;i++)
		{
			while(x%i==0)
			{
				x/=i;
				primes[i]++;//以i为底数,prime[i]为指数
			}
		}
		if(x>1)primes[x]++;//如果x还有剩余,则是一个大质数
	}
	LL res=1;
	for(auto prime :primes)res=res*(prime.second+1)%mod;
	cout<<res<<endl;
	return 0;
}

约数之和

因数之和=( 1 + 𝑝 1 + 𝑝 1 2 + ⋯ + 𝑝 1 𝑒 1 ) ( 1 + 𝑝 2 + 𝑝 2 2 + ⋯ + 𝑝 2 𝑒 2 ) ⋯ ( 1 + 𝑝 𝑘 + 𝑝 𝑘 2 + ⋯ + 𝑝 𝑘 𝑒 𝑘 )

展开及证明.

C++
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
int main()
{
	int n;
	cin>>n;
	unordered_map<int,int>primes;
	while(n--)
	{
		int x;
		cin>>x;
		for(int i=2;i<=x/i;i++)
		{
			while(x%i==0)
			{
				x/=i;
				primes[i]++;
			}
		}
		if(x>1)primes[x]++;
	}
	LL res=1;
	for(auto prime :primes)
	{
		int p=prime.first;
		int a=prime.second;
		LL t=1;
		while(a--)
		{
			t=(t*p+1)%mod;
		}
		res=res*t%mod;
	}
	cout<<res<<endl;
	return 0;
}

欧几里得算法

辗转相除法证明:

if d|a && d|b--->d|ax+by;

gcd(a,b)==gcd(b,a mod b)—>a mod b=a-c*b;

C++
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
	if(a<b)
	{
		swap(a,b);
	}
	if(a%b==0)
	{
		return b;
	}
	return gcd(a,a%b);
}
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		cout<<gcd(a,b)<<endl;
	}
	return 0;
}

欧拉函数

欧拉函数 表示:小于等于n且与n互质的正整数的个数

若n的质因数分解为:a1k1*a2k2…*ankn

则ϕ(n)=n*(1-1/a1)(1-1/a2)..(1-1/an)

证明:容斥原理.

先删去每个质因数的倍数的个数,再加上每两个质因数的乘积的倍数,循环往复.

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int a;
		cin>>a;
		int res=a;
		for(int i=2;i<=a/i;i++)
		{
			if(a%i==0)
			{
				res=res/i*(i-1);
				while(a%i==0)
				{
					a/=i;
				}
			}
			if(a>1)res=res/a*(a-1); 
		}
		cout<<res<<endl;
	}
}
原文