AcWing数学知识

Posted 捕若审若判若

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AcWing数学知识相关的知识,希望对你有一定的参考价值。

数学知识

质数

试除法 O ( n ) O(\\sqrtn) O(n )

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

分解质因数 O ( n ) O(\\sqrtn) O(n )

public static 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 ++;
            
            System.out.println(i + " " + s);
        
    
    if (x > 1) System.out.println(x + " 1");
    System.out.println();

筛质数

// 埃氏筛法 $O(nloglogn)$
public static int getPrimes(int x) 
    for (int i = 2; i <= x; i ++) 
        if (!st[i]) 
            prime[++ cnt] = i;
            for (int j = i + i; j <= x; j += i) 
                st[j] = true;
            
        
    
    return cnt;


// 线性筛法 $O(n)$, n = 1e7时比埃氏筛法快一倍
// 算法核心:x仅会被其最小质因子筛去
import java.util.*;

class Main 
    static final int N = 1000010;
    static int[] primes = new int[N];
    static boolean[] st = new boolean[N];
    static int cnt = 0;
    
    public static int getPrimes(int x) 
        for (int i = 2; i <= x; i ++)  // 循环遍历所有数
            if (!st[i]) 
                primes[cnt ++] = i;
            
            for (int j = 0; primes[j] <= x / i; j ++) 
                // 对于任意一个合数x,假设pj为最小质因子,当i<x/pj时,pj*i一定会被筛掉
                st[primes[j] * i] = true;
                if (i % primes[j] == 0) break;
            	/*
            	1. i%pj == 0,pj一定为i的最小质因子,pj也一定为i*pj的最小质因子
            	2. i%pj != 0, pj一定小于i的所有质因子,所以pj也为pj*i的最小质因子
            	*/
            
        
        return cnt;
    
    
    public static void main(String[] args) 
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int res = getPrimes(n);
        System.out.println(res);
    

约数

试除法求约数

class Main 
    
    public static void main(String[] args) 
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> temp;
        while (n -- > 0) 
            int x = sc.nextInt();
            temp = getDivisors(x);
            for(int i : temp) 
                System.out.print(i + " ");
            
            System.out.println();
        
    
    
    public static List<Integer> getDivisors(int n) 
        ArrayList<Integer> res = new ArrayList<Integer>();
        
        for (int i = 1; i <= n / i; i ++) 
            if (n % i == 0) 
                res.add(i);
                if (i != n / i) 
                    res.add(n / i);
                
            
        
        
        Collections.sort(res);
        return res;
    

约数个数

KaTeX parse error: No such environment: align at position 8: \\begin̲a̲l̲i̲g̲n̲̲ N = &p_1^\\al…

import java.util.*;

class Main 
    public static void main(String[] args) 
        Scanner sc = new Scanner(System.in);    
        int n = sc.nextInt();
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        long ans = 1;
        long mod = 1000000007;
        while (n -- > 0) 
            int x = sc.nextInt();
            for (int i = 2; i <= x / i; i ++) 
                while(x % i == 0) 
                    x /= i;
                    map.put(i, map.getOrDefault(i, 0) + 1);
                
                
            
            if (x > 1) 
                map.put(x, map.getOrDefault(x, 0) + 1);
            
        
        
        for (int i : map.values()) 
            ans = ans * (i + 1) % mod;    
        
        System.out.println(ans);
    

约数之和

N = p 1 c 1 × p 2 c 2 × . . . × p k c k 约 数 个 数 : ( c 1 + 1 ) × ( c 2 + 1 ) × . . . × ( c k + 1 ) 约 数 之 和 : ( p 1 0 + p 1 1 + . . . p 1 c 1 ) × . . . × ( p k 0 + p k 1 + . . . p k c k ) N = p_1^c_1 \\times p_2^c_2 \\times ... \\times p_k^c_k \\\\ 约数个数:(c_1 + 1) \\times (c_2 + 1) \\times ... \\times (c_k + 1) \\\\ 约数之和:(p_1^0 + p_1^1 + ... p_1^c_1) \\times ...\\times (p_k^0 + p_k^1 + ... p_k^c_k) N=p1c1×p2c2×...×pkck(c1+1)×(c2+1)×...×(ck+1)(p10+p11+...p1c1)×...×(pk0+pk1+...pkck)

while (pow -- > 0) 
    temp = (temp * key + 1) % mod

t = t ∗ p + 1 t = t * p + 1 t=tp+1
t = 1 t = 1 t=1
t = p + 1 t = p + 1 t=p+1
t = p 2 + p + 1 t = p^2 + p + 1 t=p2+p+1
t = p 3 + p 2 + p + 1 t = p^3 + p^2 + p + 1 t=p3+p2+p+1
. . . ... ...以上是关于AcWing数学知识的主要内容,如果未能解决你的问题,请参考以下文章

AcWing数学知识

AcWing数学知识

AcWing 213. 古代猪文 数学知识

LaTeX一些数学公式中常用符号

AcWing 605 简单乘积

AcWing 605 简单乘积