9.18数论

一。唯一分解定理

vector<int> factor(int n){
        vector<int> f;
        
        for(int i = 2 ; i * i <= n ; i++){
            while(n % i == 0){
                f.push_back(i);
                n /= i;
            }
        }
        
        if(n > 1)
          f.push_back(n);
          
        return f;
} 

二。调和级数(用来进行质数的筛选)

欧拉筛(现在我知道哪个更优了233333,参见7月份的数论质数筛)

//const int N = 505;
bool vis[N + 1];
vector<int> p;

void seive(){
    for(int i = 2 ; i <= N ; i++){
        if(!vis[N + 1])
          p.push_back(i);
          
        for(int j = 0 ; i * p[j] <= N ; j++){
            vis[i * p[j]] = 1;
            
            if(i % p[j] == 0)  break;
        }
    }
}

三。积性函数

若f(1) = 1, 设f(n)为定义在正整数上的函数,则对于任意正整数a,b,

若a,b互质

则有f(ab) = f(a) * f(b)

则称f(n)为积性函数

若不要求a,b互质   则称f(n)为完全积性函数

线性筛计算该函数:(我懒得写了!!!!其实是我还没看懂。。。挖个坑吧到时候再补。。。)

https://blog.csdn.net/WuBaizhe/article/details/76711158

这部分打不出来。。。还是写在笔记本上吧Orz。。。

四。扩展欧几里得算法

int gcd(int a,int b,int x, int y){
    if(b){
        int d = gcd(b, a%b, y, x);
        y -= a/b*x;
        return d;
    }
    
    x = 1;
    y = 0;
    
    return a;
}

 

 五。预处理逆元

//f是阶乘数组
//g是阶乘的逆元数组
//h是逆元数组
f[0]=1;
for(int i=1;i<=n;++i)
    f[i]=(ll)f[i-1]*i%p;
g[n]=pow(f[n],p-2);
for(int i=n-1;i>=0;--i)
    g[i]=(ll)g[i+1]*(i+1)%p;
for(int i=1;i<=n;++i)
    h[i]=(ll)g[i]*f[i-1]%p

欧拉定理

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>

using namespace std;
typedef long long ll;

int a,b,m,temp,phi,ans = 1;
bool flag;

int main(){
    
    char c;
    scanf("%d %d",&a,&m);
    
    temp = phi = m;
    
    for(int i = 2 ; i <= sqrt(m); i++){      //计算phi 
        if(temp % i == 0){
            phi = phi - phi/i;
            
            while(temp % i == 0){
                temp /= i;
            }
        }
    }     
    
    if(temp > 1){
        phi = phi - phi/temp;
    }
    
    while(!isdigit(c = getchar()));
    
    for( ; isdigit(c); c = getchar()){
        b = b*10 + c - '0';
        
        if(b >= phi){
            flag = true;
            b %= phi;
        }
    }
    
    if(flag)   b += phi;
    
    for(int i = 20 ; i >= 0; i--){    //快速幂 
        ans = 1ll*ans*ans%m;
        
        if(b & (1 << i)){
            ans = 1ll*ans*a%m;
        }
    }
    
    printf("%d
",ans);
    
    
    return 0;
}

知识点:

约数函数

欧拉函数

欧拉定理

费马小定理

线性筛求莫比乌斯函数。欧拉函数

降幂公式

中国剩余定理,扩展CRT

原文地址:https://www.cnblogs.com/zyddd915/p/11540091.html