算法笔记第四章(数学知识)

数论

质数:在大于1的整数中,如果约数只包含1和本身,这个数就被称为质数或素数

质数判定

试除法:

public static boolean isPrime(int n){
        if(n < 2){
            return false;
        }
        for(int i = 2;i <= n / i;i ++){
            if(n % i == 0) return false;
        }
        return true;
    }

O(sqrt(n))

分解质因数–试除法:从小到大枚举所有数

public static boolean isPrime(int n){
        for(int i = 2;i <=n / i;i ++){
            if(n % i == 0){
                int s = 0;
                while(n % i == 0){
                    n /= i;
                    s ++;
                }
                System.out.println(i +" " + s);
            }
        }
        if(n > 1){
            System.out.println(n + " " + 1);
        }
        System.out.println();
        return true;
    }

埃氏筛

		int N = 1000010;
        int cnt = 0;
        boolean[] st = new boolean[N];
        int n = r.nextInt();
        for(int i = 2;i <= n;i ++){
            if(!st[i]){
                cnt++;
                for(int j = i + i ;j <= n;j += i) st[j] = true;
            }
        }

O(nlognlogn)

线性筛

n只会被最小质因子筛掉

例如:40被2筛掉,也就是当i= 20 , primes[j] = 2 st[primes[j] * i (40)]= true break;

		int N = 1000010;
        int cnt = 0;
        int[] primes = new int[N];
        boolean[] st = new boolean[N];
        int n = r.nextInt();
        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;
            }
        }

约数

试除法求一个数的所有约数

List<Integer> res = new ArrayList<>();
            int n = r.nextInt();
            for(int i = 1;i <= n / i;i ++){
                if(n % i == 0){
                    res.add(i);
                    if(i != n / i) res.add(n / i); //如果i == n / i 只加一个
                }
            }
            Collections.sort(res);
            for (Integer re : res) {
                System.out.print(re+" ");
            }
            System.out.println();

约数个数

AReader r = new AReader();
        int t = r.nextInt();
        double mod = 1e9 + 7;
        HashMap<Integer , Integer> hash = new HashMap<>();
        while(t -- > 0){
            int x = r.nextInt();
            for(int i =2;i <= x/ i;i ++){
                while(x % i == 0){
                    x /= i;
                    hash.put(i , hash.getOrDefault(i , 0) + 1);
                }
            }
            if(x > 1){
                hash.put(x , hash.getOrDefault(x , 0) + 1);
            }
        }
        long res = 1;
        for (Integer value : hash.values()) {
            res = (int) (res * (value + 1) % mod);
        }
        System.out.println(res);

约数之和

import java.io.*;
import java.util.*;
/**
 * 基本思想:
 *  如果 N=a1^k1 * a2 ^ k2 * ... an ^ kn
 *  约数个数: (k1+1) * (k2+1) * ... (kn+1)
 *  约数之和: (a1^0 + a1^1 + ... + a1^k1) * ... * (an^0 + an^1 + ... + an^kn)
 * 
 */
class Main{
    static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws Exception{
        int t = Integer.valueOf(read.readLine());
        Map<Integer, Integer> map = new HashMap();
        while(t -- > 0){
            Integer a = Integer.valueOf(read.readLine());

            for(int i = 2; i <= a / i; i++){
                if(a % i == 0){
                    int cnt = 0;
                    while(a % i == 0){
                        cnt++;
                        a /= i;
                    }
                    map.put(i, map.getOrDefault(i, 0) + cnt);
                }
            }
            if(a > 1) map.put(a, map.getOrDefault(a, 0) + 1);
        }

        long res = 1;
        int mod = (int)1e9 + 7;
        for(Integer a: map.keySet()){
            long sum = 0;
            int k = map.get(a);

//          long sum = 1;
//          while(k-- > 0) sum = (sum * a + 1) % mod;  //求出a^k + a^(k-1) + ... + a^0

            for(int i = 0; i <= k; i++){
                long pow = 1; int tmp = i;
                while(tmp-- > 0) pow = pow * a % mod;
                sum = (sum + pow) % mod;
            }
            res = res * sum % mod; 
        }
        System.out.println(res);
    }
}

欧拉函数

欧拉函数表示1~N中与N互质的数的个数

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. 从1~N中去掉p1,p2…,pk的所有倍数
  2. 加上所有pi*pj的倍数
int a = r.nextInt();
            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);

            System.out.println(res);

欧拉筛

    static int N = 1000010;
    static boolean[] st = new boolean[N];
    static int[] primes = new int[N];
    static int[] phi = new int[N];
    static int cnt = 0;

	public static long get(int n){
        phi[1] = 1;
        for(int i =2;i <= n;i ++){
            if(!st[i]) {
                primes[cnt++] = i;
                phi[i] = i - 1;
            }
            for(int j = 0;primes[j] <= n / i;j ++){
                st[primes[j] * i] = true;
                if(i % primes[j] == 0) {
                    phi[primes[j] * i] = primes[j] * phi[i];
                    break;
                }
                phi[primes[j] * i] = phi[i] * (primes[j] - 1);
            }
        }

        long res = 0;
        for(int i = 1;i <= n;i ++) res += phi[i];
        return res;
    }

快速幂

    public static long kuaisumi(long a , int k , int p){
        long res = 1;
        while(k != 0){
            if((k & 1) == 1) res = res * a % p;
            k >>= 1;
            a =  a*a%p;
        }
        return res;
    }

快速幂求逆元

public static void main(String[] args) throws IOException {
        AReader r = new AReader();
        int n = r.nextInt();
        while(n -- > 0){
            long a = r.nextInt();
            long p = r.nextInt();
            long res = kuaisumi(a , p - 2, p);
            if(a % p != 0) System.out.println(res);
            else System.out.println("impossible");
        }
    }
    public static long kuaisumi(long a , long k , long p){
        long res = 1;
        while(k != 0){
            if((k & 1) == 1) res = res * a % p;
            k >>= 1;
            a =  a*a%p;
        }
        return res;
    }

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

裴蜀定理

有一对正整数a ,b ,那么一定存在非零整数x ,y,使得ax + by = gcd(a , b)

import java.util.*;
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();
            int b = sc.nextInt();
            int d = exgcd(a, b);
            System.out.println(x + " " + y);
        }
    }
    private static int x, y;
    private static int exgcd(int a, int b){
        if(b == 0){
            x = 1;
            y = 0;
            return a;
        }
        int d = exgcd(b, a % b);
        int tmp = x;
        x = y;
        y = tmp - a / b * y;
        return d;
    }
}

求组合数

		AReader r = new AReader();
        int N = 2010;
        int mod = (int) (1e9 + 7);
        int[][] c = new int[N][N];
        for(int i = 0;i < N;i ++){
            for(int j = 0;j <= i;j ++){
                if(i == 0) c[i][j] = 1;
                else {
                    if(j != 0){
                        c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod;
                    }else{
                        c[i][j] = 1;
                    }
                }
            }
        }
        int n = r.nextInt();
        while(n -- > 0){
            int a = r.nextInt();
            int b = r.nextInt();
            System.out.println(c[a][b]);
        }

进阶求组合数

	public static void main(String[] args) throws IOException {
        AReader r = new AReader();
        int N = 100010;
        int mod = (int) (1e9 + 7);
        long[] fact = new long[N];
        long[] infact = new long[N];
        fact[0] = infact[0] = 1;
        for(int i = 1;i < N;i ++){
            fact[i] = (fact[i-1] * i % mod);
            infact[i] = infact[i-1] * qmi(i , mod - 2 , mod) % mod;
        }
        int n = r.nextInt();
        while(n -- > 0){
            int a = r.nextInt();
            int b = r.nextInt();
            System.out.println(fact[a] * infact[b] % mod * infact[a - b] % mod);
        }
    }
    public static long qmi(long a , long k , long p){
        long res = 1;
        while(k != 0){
            if((k & 1) == 1) res =  res * a % p;
            a =  a * a % p;
            k >>= 1;
        }
        return res;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值