首页 > 编程语言 >C++算法之旅、08 基础篇 | 质数、约数

C++算法之旅、08 基础篇 | 质数、约数

时间:2023-10-05 22:46:08浏览次数:46  
标签:约数 cout int 质数 C++ primes include

质数

在>1的整数中,如果只包含1和本身这两个约数,就被称为质数(素数)


866 试除法判定

866. 试除法判定质数 - AcWing题库

\(O(n)\)

bool isprime(int x) {
    if (x < 2) return false;
    for (int i = 2; i < x; i++)
        if (x % i == 0) return false;
    return true;
}

约数 d 与 n / d 成对出现,可以枚举较小的那一个 \(O(\sqrt{n})\)

\(d <= n/d \\ d^2 <= n \\ d <= \sqrt{n}\)

难点

  • 循环判断条件不要用 sqrt,每次循环都会执行一遍sqrt函数,比较慢
  • 循环判断条件不要用 i * i,存在溢出风险(变成负数)
  • 一定不会溢出的写法是 i <= n / i
#include <iostream>

using namespace std;

bool isprime(int n) {
    if (n < 2) return false;
    for (int i = 2; i <= n / i; i++)
        if (n % i == 0) return false;
    return true;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        if (isprime(x))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}

867⭐分解质因数

867. 分解质因数 - AcWing题库

质因数指能整除给定正整数的质数。把一个合数分解成若干个质因数的乘积的形式,即求质因数的过程叫做分解质因数。

相关理论证明可看 数论——质数:分解质因数 - 知乎 (zhihu.com)

从2到\(\sqrt{n}\)枚举n的所有质因数,求其指数并输出。还要考虑最多有一个质因素大于\(\sqrt{n}\)的情况,单独判断输出。 最坏 \(O(\sqrt{n})\),最好 \(O(logn)\) (考虑\(2^k\)情况)

#include <iostream>

using namespace std;

void divide(int n) {
    for (int i = 2; i <= n / i; i++) {
        if (n % i == 0) {
            int cnt = 0;
            while (n % i == 0) {
                cnt++;
                n /= i;
            }
            cout << i << " " << cnt << endl;
        }
    }
    if (n > 1) cout << n << " " << 1 << endl;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        divide(x);
        cout << endl;
    }
}

868⭐筛质数

868. 筛质数 - AcWing题库

朴素算法是从前往后删倍数(2~p-1都不是n的约数,所以n是质数)

调和级数\(1/2+1/3+1/4+1/5+...+1/∞\) 极限等于 \(lnn+C\)。

\(lnn < log_2n\),因此朴素算法复杂度为 \(O(nlogn)\)


埃式筛法:只删除2~p-1中质数的倍数,原理跟867类似(算数基本定理:每个正整数都可以唯一表示成素数的乘积)

粗略估计:1~n当中,有\(n/lnn\)个质数,时间复杂度变为 \(O(n)\),真实复杂度 \(O(nloglogn)\),两者差不多一个级别

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1e6 + 10;
int primes[N], cnt;
bool st[N];

void get_primes(int n) {
    for (int i = 2; i <= n; i++) {
        if (!st[i]) {
            primes[cnt++] = n;
            for (int j = i + i; j <= n; j += i) st[j] = true;
        }
    }
}

int main() {
    int n;
    cin >> n;
    get_primes(n);
    cout << cnt << endl;

    return 0;
}

线性筛法,\(O(n)\),基本思路一样(基于每个质数的倍数为非质数),当 n 很大时,速度比埃式筛法快一倍。

每个数只会被其最小质因子筛掉

  • i % pj == 0,pj 一定是 i 的最小质因子,pj 一定是 pj * i 的最小质因子
  • i % pj != 0,pj 一定小于 i 的所有质因子,pj 一定是 pj * i 的最小质因子
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1e6 + 10;
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] * i <= n; j++) {
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;  // primes[j] 一定是 i 的最小质因子
        }
    }
}

int main() {
    int n;
    cin >> n;
    get_primes(n);
    cout << cnt << endl;

    return 0;
}

约数

869 试除法求约数

869. 试除法求约数 - AcWing题库

与866优化原理类似 \(O(\sqrt{n})\)

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<int> get_divisors(int n) {
    vector<int> res;
    for (int i = 1; i <= n / i; i++) {
        if (n % i == 0) {
            res.push_back(i);
            if (i != n / i) res.push_back(n / 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 t : res) cout << t << ' ';
        cout << endl;
    }
}

870⭐约数个数

image-20230906120959871

利用算术基本定理,每个质因数有(1+n)种选择。m个选择组合得出m个约数

具体原理可看 第四章 数学知识(一)——质数与约数 - 知乎 (zhihu.com)

INT_MAX 约数个数约1500


870. 约数个数 - AcWing题库

求 n 个数的乘积的约数个数,可以求每个数的每个质因子指数之和,然后套用公式。

#include <algorithm>
#include <iostream>
#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) res = res * (prime.second + 1) % mod;
    cout << res << endl;
    return 0;
}

871⭐约数之和

AcWing 871. 约数之和 - AcWing

#include <algorithm>
#include <iostream>
#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, a = prime.second;
        LL t = 1;
        while (a--) {
            t = (t * p + 1) % mod;
        }
        res = res * t % mod;
    }
    cout << res << endl;
    return 0;
}

872⭐最大公约数

872. 最大公约数 - AcWing题库

欧几里得算法(辗转相除法)

image-20230906122950118
#include <algorithm>
#include <iostream>
#include <unordered_map>

using namespace std;

// a 和 0 的最大公约数是 a
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 << gcd(a, b) << endl;
    }
    return 0;
}

标签:约数,cout,int,质数,C++,primes,include
From: https://www.cnblogs.com/linxiaoxu/p/17744061.html

相关文章

  • 14_C++对c的扩展
    c++对c的扩展::作用域运算符::使用全局变量usingnamespacestd;inta=10;voidtest01(){inta=20;cout<<a<<endl;//20cout<<::a<<endl;//10}命名空间namespacenamespace和c语言中static只在本源文件中有效差不多命名空间使用语法创建一......
  • C++ 数据结构插入效率学习
    转自:https://blog.csdn.net/breaksoftware/article/details/829478381.总结在头部插入。元素数量>15k时,效率unordered_set>set,unordered_map>map。元素数量<1024时,效率unordered_set>set,map> unordered_map。元素数量<256时,效率unordered_set>set,map> unorder......
  • C++ namespace User_Unauthorized version 1.0.0 is officially released
    CodenamespaceUser_Unauthorized{/***@briefThisisaheaderfileforcompetitiveprogramming.*@authorUser-Unauthorized*@version1.0.0*@date2023-10-5*/typedeflonglongvalueType;typedefstd::vector<......
  • C++ Profiler Introduction [CPU Time Only]
    C++ProfilerIntroduction[CPUTimeOnly]author:LastWhisperdate:2023/10/05ThereareseveralprofilersforC++.Basedonmyresearch,I'vefoundthattracyisthemostpowerful.However,it'schallengingtoconfigure.Toquicklybenchmark......
  • C/C++学习 -- HMAC算法
    1.HMAC算法概述HMAC,全称为HMAC-MD5、HMAC-SHA1、HMAC-SHA256等,是一种在数据传输中验证完整性和认证来源的方法。它结合了哈希函数和密钥,通过在数据上应用哈希函数,生成一个带密钥的散列值,用于验证数据的完整性。HMAC算法广泛应用于网络协议、数字签名、认证和访问控制等领域。2.HM......
  • 用C++写的一个万用哈希函数模板
    用C++写的一个万用哈希函数模板template<typenameT>inlinevoidhash_combine(size_t&seed,constT&val){seed^=hash<T>()(val)+0x9e3779b9+(seed<<6)+(seed<<2);}template<typenameT,typename......
  • windows上的C++编译环境
    Windows上的C++编程环境比Linux上的繁杂很多,有许多工具已经很老了,但是很多教材也还在用,很多学校的教学也还在用。另一方面,有更现代的选择,但是需要一些必要的配置和对工具链组成的理解,本文将必要的环境都介绍一遍,让新手能有一个相对完整的理解,然后迅速抛弃老旧的工具链,使用更现代......
  • VC++编写ActiveX控件方法
    暑假在做一个项目的时候,本来是用C#.NET来写的一个港口进出闸的流程控制程序,里面涉及一个响应用PLC的采集信息的问题(PLC用串口和工控机相连接),然后思考如何用C#写串口通讯程序,结果师兄在一旁直接用VC++写了一个“*.ocx控件”,并在自己的电脑上进行了测试,完工后就把生成的“*.ocx”控......
  • C++中的inline用法
    1.引入inline关键字的原因在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数。栈空间就是指放置程序的局部数据(也就是函数内数据)的内存空间。在系统下,栈空间是有限的,假如频繁大量的使用就会造成因栈空间不足而导致程......
  • C++模板template应用总结
    引言模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计。C++的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IOStream。函数模板在c++入门中,很多人会接触swap(int&,int&)这样的函数类似代码如下:voidswap(int&a,int&b){......