【算法基础:数学知识】4.2 约数

约数介绍

约数,又称因数。整数a除以整数b(b≠0) 除得的商正好是整数而没有余数,我们就说a能被b整除,或b能整除a。a称为b的倍数,b称为a的约数。

例题列表

AcWing 869. 试除法求约数 (求一个数的所有约数)

https://www.acwing.com/activity/content/problem/content/938/

在这里插入图片描述

跟判断质数的过程差不多,使用试除法。

import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while (n-- != 0) {
            for (int v: getDivisors(sc.nextInt())) System.out.print(v + " ");
            System.out.println();
        }
    }

    static List<Integer> getDivisors(int x) {
        List<Integer> res = new ArrayList<>();
        for (int i = 1; i <= x / i; ++i) {
            if (x % i == 0) {
                res.add(i);
                if (i != x / i) res.add(x / i);
            }
        }
        Collections.sort(res);
        return res;
    }
}

AcWing 870. 约数个数(求一些数相乘之后的结果有几个约数,答案可能很大)

https://www.acwing.com/activity/content/problem/content/939/

在这里插入图片描述

约数个数定理⭐

在这里插入图片描述

小知识:
int 范围内的数字,约数最多的那个数字,大概有 1500 个约数。

如果 N = p1^c1 * p2^c2 * ... *pk^ck
约数个数: (c1 + 1) * (c2 + 1) * ... * (ck + 1)
约数之和: (p1^0 + p1^1 + ... + p1^c1) * ... * (pk^0 + pk^1 + ... + pk^ck)

解法代码

对每个 a 都求一下质因数分解。

import java.util.*;

public class Main {
    final static int MOD = (int)1e9 + 7;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Map<Integer, Integer> primes = new HashMap<>();     // 用来存储质因数和它的次数
        while (n-- != 0) {
            int x = sc.nextInt();

            // 求出 x 的所有质因数
            for (int i = 2; i <= x / i; ++i) {
                if (x % i == 0) {
                    int s = 0;
                    while (x % i == 0) {
                        s++;
                        x /= i;
                    }
                    primes.merge(i, s, Integer::sum);
                }
            }
            if (x > 1) primes.merge(x, 1, Integer::sum);
        }

        long res = 1;
        for (int cnt: primes.values()) {
            res = res * (cnt + 1) % MOD;
        }
        System.out.println(res);
    }
}

AcWing 871. 约数之和

https://www.acwing.com/activity/content/problem/content/940/
在这里插入图片描述

解法公式⭐

如果 N = p1^c1 * p2^c2 * ... *pk^ck
约数个数: (c1 + 1) * (c2 + 1) * ... * (ck + 1)
约数之和: (p1^0 + p1^1 + ... + p1^c1) * ... * (pk^0 + pk^1 + ... + pk^ck)

按照上面的公式,完成质因数分解之后模拟就好了。

import java.util.*;

public class Main {
    final static int MOD = (int)1e9 + 7;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Map<Integer, Integer> primes = new HashMap<>();     // 用来存储质因数和它的次数
        while (n-- != 0) {
            int x = sc.nextInt();

            // 求出 x 的所有质因数
            for (int i = 2; i <= x / i; ++i) {
                if (x % i == 0) {
                    int s = 0;
                    while (x % i == 0) {
                        s++;
                        x /= i;
                    }
                    primes.merge(i, s, Integer::sum);
                }
            }
            if (x > 1) primes.merge(x, 1, Integer::sum);
        }

        long res = 1;
        for (Map.Entry<Integer, Integer> entry: primes.entrySet()) {
            long x = entry.getKey(), s = entry.getValue(), sum = 0, t = 1;
            for (int i = 0; i <= s; ++i) {
                sum = (sum + t) % MOD;
                t = (t * x) % MOD;
            }
            res = res * sum % MOD;
        }
        System.out.println(res);
    }
}

因为处理 int 溢出很麻烦,所以直接使用 long 好了。

AcWing 872. 最大公约数 (欧几里得算法 gcd)

https://www.acwing.com/activity/content/problem/content/941/
在这里插入图片描述

小知识:
如果 d 能整除 a,且 d 能整除 b,那么 d 就可以整除 a + b。
gcd(a, b) = gcd(b, a % b)

gcd(a, b) = gcd(b, a % b) 的证明:
在这里插入图片描述

https://oi-wiki.org/math/number-theory/gcd/#%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97%E7%AE%97%E6%B3%95

import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while (n-- != 0) {
            int a = sc.nextInt(), b = sc.nextInt();
            System.out.println(gcd(a, b));
        }
    }
    
    static int gcd(int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
}

相关链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wei *

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值