Fundamentals of Number Theory 1

Euler sieve


int cnt, n;
int pri[N];
bool st[N];

void init(int n)
{
    
    
    st[0] = st[1] = true;
    for(int i = 2; i <= n; i ++)
    {
    
    
        if(!st[i]) pri[cnt ++] = i;
        for(int j = 0; j < cnt; j ++)
        {
    
    
            if(pri[j] * i > N) break;
            st[pri[j] * i] = true;
            if(i % pri[j] == 0) break;
        }
    }
}



Judgment of prime numbers by trial division


bool fun(int x)
{
    
    
    if(x < 2) return false;
    for(int i = 2; i <= x/i; i ++)
    {
    
    
        if(x % i == 0) return false;
    }
    return true;
}

prime factor


 		map<int, int> cnt;
        for(int i = 2; i <= x/i; i ++)
        {
    
    
            int num = 0;
            while(x % i == 0)
            {
    
    
                num ++;
                x /= i;
            }
            if(num >= 1)
            cnt[i] = num;
        }
        if(x > 1) cnt[x] = 1;

fast exponentiation element

There is (a/b) mod m, if b and m are relatively prime, then division can be transformed into multiplication, which is
equivalent to a*x, x = b to the power of m-2, and x is the inverse of b under the condition of mod m


Trial division to find all divisors


		vector<int> res;
        for(int i = 1; i <= x/i; i ++)
        {
    
    
            if(x % i == 0)
            {
    
    
                res.push_back(i);
                if(x / i != i)
                {
    
    
                    res.push_back(x / i);
                }
            }
        }

Trial division method to find approximation number


void fun(int x)
{
    
    
    for(int i = 2; i <= x/i; i ++)
    {
    
    

        while(x % i == 0)
        {
    
    
            cnt[i] ++;
            x /= i;
        }
    }
    if(x > 1)
    cnt[x] ++;
}

Trial division to find the sum of divisors (summation by Qin Jiushao algorithm)


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int p = 1e9 + 7;

int n;
map<int, int> cnt;

void fun(int x)
{
    
    
    for(int i = 2; i <= x/i; i ++)
    {
    
    
        while(x % i == 0)
        {
    
    
            cnt[i] ++;
            x /= i;
        }
    }
    if(x > 1)
    cnt[x] ++;
}

int main()
{
    
    
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
    
    
        int x;
        cin >> x;
        fun(x);
    }
    ll res = 1;
    for(auto [a, b]: cnt)
    {
    
    
        ll tem = 0;
        //秦九韶算法
        for(int i = 0; i <= b; i ++)
        {
    
    
            tem = (tem * a + 1) % p;
        }
        res = (res * tem) % p;
    }
    cout << res << endl;
    return 0;
}

Calculate the number of combinations by formula


void init()
{
    
    
    for(int i = 0; i < N; i ++)
    {
    
    
        for(int j  = 0; j <= i; j ++)
        {
    
    
            if(!j) f[i][j] = 1;
            else
            f[i][j] = (f[i-1][j-1] + f[i-1][j]) % p;
        }
    }
}

おすすめ

転載: blog.csdn.net/qq_63092029/article/details/129888255