YOU CAN DRINK ALL YOU LIKE, BUT IN THE MORNING YOU GET HEADACHE WITH THE SAME PROBLEMS.
质数、约数、简单的欧几里得算法
题目原文在文末的原文链接中,在ACWING这个平台的题库中输入对应的题号即可
基础知识
- 所有小于等于1的数既不是质数也不是合数,在大于1的整数中,如果只包含1和本身这两个约数则为质数(素数)
- 质数的判定:试除法,时间复杂度一定是
O(N^1/2)
866.试除法判定质数
#include
using namespace std;
bool is_prime(int x) {
if (x 2) return false;
for (int i = 2; i <= x / i; i++)
if (x % i == 0) return false;
return true;
}
int main() {
int n;
cin >> n;
while (n--) {
int x;
cin >> x;
if (is_prime(x)) puts("Yes");
else puts("No");
}
return 0;
}
- 分解质因数:试除法(从小到大尝试N的所有因数,N中最多只包含一个大于
N^1/2
的质因子)时间复杂度最坏是O(N^1/2)
,最好是O(logN)
867.分解质因数
#include
using namespace std;
void divide(int x) {
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) {
int s = 0;
while (x % i == 0) x /= i, s++;
cout <" " <endl;
}
}
if (x > 1) cout <" " <1 <endl;
cout <endl;
}
int main() {
int n;
cin >> n;
while (n--) {
int x;
cin >> x;
divide(x);
}
return 0;
}
- 筛质数:
- 埃氏筛法:时间复杂度
O(NloglogN)
- 线性筛法:每个数N只会被它的最小质因子筛掉(当
i%primes[j]==0
时,primes[j]
一定是i
的最小质因子,所以primes[j]
一定是i*primes[j]
的最小质因子;当i%primes[j]!=0
时,所以primes[j]
一定小于i
的最小质因子,primes[j]
一定是i*primes[j]
的最小质因子。任何一个合数X一定存在一个最小质因子J,当i枚举到X/J时,此合数一定会被筛,因此每个合数只会被筛一次,所以时间复杂度为线性的。)时间复杂度O(N)
- 埃氏筛法:时间复杂度
868.筛质数
// 埃氏筛法
#include
using namespace std;
const int N = 1000010;
int primes[N], cnt;
bool st[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (st[i]) continue;
primes[cnt++] = i;
for (int j = i + i; j <= n; j += i)
st[j] = true;
}
}
int main() {
int n;
cin >> n;
get_primes(n);
cout <endl;
return 0;
}
// 线性筛法
#include
using namespace std;
const int N = 1000010;
int primes[N], cnt;
bool st[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
int main() {
int n;
cin >> n;
get_primes(n);
cout <endl;
return 0;
}
- 试除法求约数:时间复杂度一定是
O(N^1/2)
869.试除法求约数
#include
using namespace std;
vector<int> get_divisors(int x) {
vector<int> res;
for (int i = 1; i <= x / i; i++)
if (x % i == 0) {
res.push_back(i);
if (i != x / i) res.push_back(x / i);
}
sort(res.begin(), res.end());
return res;
}
int main() {
int n;
cin >> n;
while (n--) {
int x;
cin >> x;
auto res = get_divisors(x);
for (auto x : res) cout <" ";
puts("");
}
return 0;
}
- 约数个数:
N=P1^A1+P2^A2+P3^A3+...+PK^AK
,约数个数为(A1+1)(A2+1)(A3+1)...(AK+1)
,每一个P都有对应的A+1种选法,每个不同的选法,都能凑成一个不同的约数
870.约数个数
#include
using namespace std;
const int N = 110, 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]++;
}
long long res = 1;
for (auto p : primes) res = res * (p.second + 1) % mod;
cout <endl;
return 0;
}
- 约数之和:
N=P1^A1+P2^A2+P3^A3+...+PK^AK
,约数之和为(P1^0+P1^1+...+P1^A1)(P2^0+P2^1+...+P2^A1)(P3^0+P3^1+...+P3^A1)...(PK^0+PK^1+...+PK^A1)
,每个项选一个,凑成一个约数,累加
871. 约数之和
#include
using namespace std;
const int N = 110, 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]++;
}
long long res = 1;
for (auto p : primes) {
long long a = p.first, b = p.second;
long long t = 1;
while (b--) t = (t * a + 1) % mod;
res = res * t % mod;
}
cout <endl;
return 0;
}
- 欧几里得算法(辗转相除法):这只是最基本的用法,欧几里得的扩展定理明天会专门写一篇文章来讲解
872.最大公约数
#include
using namespace std;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int main() {
int n;
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
cout <endl;
}
return 0;
}