总结数论几个基本模板

文章介绍了如何使用Java编写代码来执行试除法判断质数,分解因数,以及计算约数的相关操作。
摘要由CSDN通过智能技术生成

试除法判定质数

输入样例

2
2
6

输出样例

Yes
No

AC代码

import java.util.*;

public class Main{
    private static boolean is_prime(int x){
        if(x < 2) return false;
        for(int i = 2 ; i <= x / i ; i ++)
            if(x % i == 0) return false;
        return true;
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            if(is_prime(s.nextInt())) System.out.println("Yes");
            else System.out.println("No");
        }
    }
}

 

分解质因数

输入样例

2
6
8

输出样例

2 1
3 1

2 3

AC代码

import java.util.*;

public class Main{
    private static void divide(int x){
        for(int i = 2 ; i <= x / i ; i ++){
            if(x % i == 0){
                int cnt = 0;
                while(x % i == 0) {
                    cnt ++;
                    x /= i;
                }
                System.out.println(i + " " + cnt);
            }
        }
        if(x > 1) System.out.println(x + " 1");
        System.out.println();
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            divide(s.nextInt());
        }
    }
}

 

筛质数

 输入样例

8

输出样例

4

AC代码

//朴素筛法 时间复杂度O(nlogn)
import java.util.*;

public class Main{
    static final int N = 1000010;
    static boolean[] st = new boolean[N];

    private static int prime(int x){
        int cnt = 0;
        for(int i = 2 ; i <= x ; i ++){
            if(st[i]) continue;
            cnt ++;
            for(int j = i + i ; j <= x ; j += i) st[j] = true;
        }
        return cnt;
    }

    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println(prime(s.nextInt()));
    }
}
//线性筛法 时间复杂度O(nloglogn)
import java.util.*;

public class Main{
    static final int N = 1000010;
    static int[] prime = new int[N];
    static boolean[] st = new boolean[N];

    private static int primes(int x){
        int cnt = 0;
        for(int i = 2 ; i <= x ; i ++){
            if(!st[i]) prime[cnt ++] = i;
            for(int j = 0 ; prime[j] <= x / i ; j ++){
                st[prime[j] * i] = true;
                if(i % prime[j] == 0) break;
            }
        }
        return cnt;
    }

    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println(primes(s.nextInt()));
    }
}

试除法求约数

输入样例

2
6
8

 输出样例

1 2 3 6 
1 2 4 8 

AC代码

import java.util.*;

public class Main{
    static final int N = 2000;
    static int[] g = new int[N];
    static int cnt = 0;
    
    private static void divide(int x){
        cnt = 0;
        for(int i = 1 ; i <= x / i ; i ++){
            if(x % i == 0){
                g[cnt ++] = i;
                if(i != x / i) g[cnt ++] = x / i;
            }
        }
        
        Arrays.sort(g , 0 , cnt);
        for(int i = 0 ; i < cnt ; i ++) System.out.print(g[i] + " ");
        System.out.println();
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            divide(s.nextInt());
        }
    }
}

约数个数

输入样例

3
2
6
8

输出样例

12

AC代码

import java.util.*;

public class Main{
    static Map<Integer , Integer> prime = new HashMap<>();
    static final long P = (long)1e9 + 7;
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            int x = s.nextInt();
            for(int i = 2 ; i <= x / i ; i ++){
                while(x % i == 0){
                    x /= i;
                    prime.put(i , prime.getOrDefault(i , 0) + 1);
                }
            }
            if(x > 1) prime.put(x , prime.getOrDefault(x , 0) + 1);
        }
        long res = 1;
        for(int i : prime.values()) res = res * (i + 1) % P;
        System.out.println(res % P);
    }
}

约数之和

输入样例 

3
2
6
8

 输出样例

252

AC代码

import java.util.*;

public class Main{
    static final long P = (long)1e9 + 7;
    static Map<Integer , Integer> prime = new HashMap<>();
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            int x = s.nextInt();
            for(int i = 2 ; i <= x / i ; i ++){
                while(x % i == 0){
                    x /= i;
                    prime.put(i , prime.getOrDefault(i , 0) + 1);
                }
            }
            if(x > 1) prime.put(x , prime.getOrDefault(x , 0) + 1);
        }
        
        long res = 1;
        for(int p : prime.keySet()){
            int a = prime.get(p);
            long t = 1;
            while(a -- > 0) t = (t * p + 1) % P;
            res = res * t % P;
        }
        System.out.println(res);
    }
}

 

最大公约数

输入样例

2
3 6
4 6

 输出样例

3
2

AC代码

import java.util.*;

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

 

欧拉函数

输入样例

3
3
6
8

 输出样例

2
2
4

AC代码

import java.util.*;

public class Main{
    private static long phi(int x){
        long res = x;
        for(int i = 2 ; i <= x / i ; i ++){
            if(x % i == 0){
                res = res / i * (i - 1);
                while(x % i == 0) x /= i;
            }
        }
        if(x > 1) res = res / x * (x - 1);
        return res;
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            System.out.println(phi(s.nextInt()));
        }
    }
}

 

筛法求欧拉函数

输入样例

6

 输出样例

12

AC代码

import java.util.*;

public class Main{
    static final int N = 1000010;
    static boolean[] st = new boolean[N];
    static int[] prime = new int[N];
    static int[] phi = new int[N];
    static int cnt = 0;
    
    private static void getphi(int x){
        phi[1] = 1;
        for(int i = 2 ; i <= x ; i ++){
            if(!st[i]){
                prime[cnt ++] = i;
                phi[i] = i - 1;
            }
            for(int j = 0 ; prime[j] <= x / i ; j ++){
                st[prime[j] * i] = true;
                if(i % prime[j] == 0){
                    phi[prime[j] * i] = prime[j] * phi[i];
                    break;
                }
                phi[prime[j] * i] = (prime[j] - 1) * phi[i];
            }
        }
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        getphi(s.nextInt());
        long res = 0;
        for(int i : phi) res += i;
        System.out.println(res);
    }
}

 

快速幂

输入样例

2
3 2 5
4 3 9

 输出样例

4
1

AC代码

import java.util.*;

public class Main{
    private static long qmi(int a , int b , int p){
        long res = 1 % p;
        while(b > 0){
            if((b & 1) > 0) res = res * a % p;
            a = (int)((long)a * a % p);
            b >>= 1;
        }
        return res;
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
        int a = s.nextInt() , b = s.nextInt() , p = s.nextInt();
        System.out.println(qmi(a , b , p));
        }
    }
}

 

快速幂求逆元

 输入样例

3
4 3
8 5
6 3

输出样例

1
2
impossible

AC代码

import java.util.*;

public class Main{
    private static long qmi(int a , int b , int p){
        long res = 1 % p;
        while(b > 0){
            if((b & 1) > 0) res = res * a % p;
            a = (int)((long)a * a % p);
            b >>= 1;
        }
        return res;
    }
    
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n -- > 0){
            int a = s.nextInt() , b = s.nextInt();
            if(a % b == 0) System.out.println("impossible");
            else System.out.println(qmi(a , b - 2 , b));
        }
    }
}

题目源自ACWING

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值