acwing基础算法(四)质数

1.质数

1.质数的判定——试除法

1)大于1的

2)约束只有1和本身

3)时间复杂度sqrt(n)

#include <iostream>
#include <algorithm>

using namespace std;

bool is_prime(int x)
{
    if (x < 2) return false;//质数要严格大于1
    for (int i = 2; i <= x / i; i ++ )//这里用到了约数都是成对出现的性质,实践复杂度为根号n
        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;
}

2.分解质因数——试除法

  时间复杂度sqrt(n)

#include <iostream>
#include <algorithm>

using namespace std;

void divide(int x)
{
    for (int i = 2; i <= x / i; i ++ )//只包含一个大于根号n的质因子,所以枚举到根号n
        if (x % i == 0)//i 一定是质数
        {
            int s = 0;
            while (x % i == 0) x /= i, s ++ ;
            cout << i << ' ' << s << endl;
        }
    if (x > 1) cout << x << ' ' << 1 << endl;//把大于根号n的质因子单独打印出来
    cout << endl;
}

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

    return 0;
}

3.质数的判定——筛子法

//朴素筛法(埃式)
//朴素筛法筛掉p之前的所有数的倍数
//埃式筛掉p之前所有质数的倍数
#include <iostream>
#include <algorithm>

using namespace std;

const int N= 1000010;

int primes[N], cnt;
bool st[N];//true代表不是质数,false代表是质数

void get_primes(int n)
{
    for (int i = 2; i <= n; i ++ )
    {
        if (st[i]) continue;//如果为真,则跳过
        primes[cnt ++ ] = i;//如果是质数,则加入到primes数组里
        for (int j = i; j <= n; j += i)//把所有i的倍数标记为true,这行代码只有i为质数时才会执行,所以就达到了埃式算法的目的
            st[j] = true;
    }
}

int main()
{
    int n;
    cin >> n;

    get_primes(n);

    cout << cnt << endl;

    return 0;
}


//线性筛法
//n只会被其最小质因子筛掉,例如4被2筛掉,36被2筛掉。。。。。
#include <iostream>
#include <algorithm>

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 ++ )//枚举n个数
    {
        if (!st[i]) primes[cnt ++ ] = i;//如果是质数,加到表里
        for (int j = 0; primes[j] <= n / i; j ++ )//从小到大枚举所有的质数
        {
            st[primes[j] * i] = true;//把当前质数和i的乘积筛掉
            if (i % primes[j] == 0) break;//primes[j]一定是i的最小质因子
        }
    }
}

int main()
{
    int n;
    cin >> n;

    get_primes(n);

    cout << cnt << endl;

    return 0;
}

 线性筛法参考连接:https://www.jianshu.com/p/f16d318efe9b

 j=2j=3j=4j=5
i=22X26810
i=32X33X31215
i=42X4121620
i=52X53X5125X5
i=62X6182430
i=72X73X7285X7

如表所示,所有的合数只会被其最小质因数筛掉, 

if (i % primes[j] == 0) break;

这句代码的意思就是:当i能被primes[j]整除时,说明i不能再作为乘积中的一项了,乘积中的另一项一定会比i小了,当前的合数将交给乘积中的另一项(primes[j])去筛除。

j是在枚举最小质因子,i是在枚举最小质因子要乘以的另一项

2.约数

1.试除法求所有约束

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

using namespace std;

vector<int> get_divisors(int x)
{
    vector<int> res;
    for (int i = 1; i <= x / i; i ++ )//只枚举小于sqrt(n)的一半,另一个可以算出来
        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 << x << ' ';
        cout << endl;
    }

    return 0;
}

2.约数的个数

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

using namespace std;

typedef long long LL;

const int N = 110, mod = 1e9 + 7;

int main()
{
    int n;
    cin >> n;

    unordered_map<int, int> primes;//存质因数及其指数
    //step1:分解质因式
    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] ++ ;
    }
    //step2:利用公式计算约数个数
    LL res = 1;
    for (auto p : primes) res = res * (p.second + 1) % mod;

    cout << res << endl;

    return 0;
}

4.约数求和

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

using namespace std;

typedef long long LL;

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] ++ ;
    }

    LL res = 1;
    for (auto p : primes)
    {
        LL a = p.first, b = p.second;
        LL t = 1;
        while (b -- ) t = (t * a + 1) % mod;//公式
        res = res * t % mod;
    }

    cout << res << endl;

    return 0;
}

5.最大公约数(辗转相除法)

#include <iostream>
#include <algorithm>

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;
        scanf("%d%d", &a, &b);
        printf("%d\n", gcd(a, b));
    }

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值