Review of Mathematical noip (3) - Number Theory

1. The basic concepts and common code

(1) prime number (质数)

int prime[maxn],tot=0;
bool vis[maxn];
void init(int n)
{
    vis[1]=1;
    for(int i=2;i<=n;++i)
    {
        if(!vis[i]) prime[++tot]=i;
        for(int j=1;j<=tot&&prime[j]*i<=n;++j)
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;//每个数都只会被它最小的素数因子筛掉
        }
    }
}//线性筛素数

(2) Euclidean algorithm (Euclidean)

#define ll long long
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

Extended Euclidean Algorithm

int exgcd(int a,int b,int &d,int &x,int &y)
{
    if(!b)
    {
        x=1,y=0;
        return d=a;
    }
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}//可用于求ax+by=d的一组解,当且仅当d=gcd(a,b)时有解(若要通过此特解推出其他解,每次x减小b/d,y增加a/d即可)

Extended Euclid can inverse yuan (inverse perhaps be understood as meaning the countdown in the mold)

If required a inverse element in the mold b sense, first under a die b significance has the inverse element with the proviso that gcd (a, b) = 1, i.e., a and b are relatively prime, we assume that at a die b Significance the inverse is x, then the ax in the mold b congruent to 1, you can get ax + by = 1 (should be ax = by + 1, after transposition becomes the ax-by = 1, we let b = -b, the ax + by = 1)

(3) rapid power

#define LL long long
LL pow_mod(LL a,LL b,int mod)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;
        b>>=1;
    }
}

(4) the Euler function (not more than the counted number with their own prime)

\[ \varphi(n)=n(1-\frac{1}{p_1})(1-\frac{1}{p_2})\cdots(1-\frac{1}{p_k}) \]

prove:

Positive integer n is given a unique factorization
\ [n = p_1 ^ {a_1
} p_2 ^ {a_2} p_3 ^ {a_3} \ cdots p_k ^ {a_k} \] with inclusion and exclusion, is first subtracted from the total n, p1 number multiple of p2, p3 ... pk of (pi is a prime number, it shall not multiples its prime), i.e. nn / p1-n / p2 ...- n / pk ( pi largest provided n is less than or equal to meet multiple of pi * t, t is determined so that n / pi), and then add "is also the number of multiples of two, and subtracting a multiple of the number is three while the number of .. ., that is, the resulting formula:
\ [\ varphi (n-) = \ sum_ {S \ subseteq P_1, P_2, ... P_K} (-. 1) ^ {| S |} \ {n-FRAC} {\ prod_ {p_i \ epsilon S}} p_i \]

int phi[maxn];
void init(int n)
{
    phi[1]=1;
    for(int i=2;i<=n;++i)
    {
        phi[i]=i;
    }
    for(int i=2;i<=n;++i)
    {
        if(phi[i]==i)
        {
            for(int j=i;j<=n;j+=i)
            {
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}//筛表法求欧拉函数

(5) the remaining lines, the inverse modulo multiplication

Popular that lines the remainder modulo n is entirely {0,1,2, ..., n-1}, to simplify the remaining lines (also referred to as shrink-based) system that is completely the remaining number prime with n.

The most common system remaining completely written modulus n is Z / nZ, may be written as:
\ [the Z / n or Z_n \]
, referred to as a shrink-based
\ [Z_n ^ * \]

#define LL long long
LL inv(LL a,LL mod)
{
    LL d,x,y;
    return exgcd(a,mod,d,x,y)==1?(x+mod)%mod:-1;
}//模乘法的逆

Another method is to use the inverse Euler's theorem

Given an arbitrary integer n> 1, for any shrinking lines n in an element a,
\ [a ^ {\ varphi (n)} \ equiv1 (MOD \ Quad n) \]
Thus a inverse element is
\ [ a ^ {\ varphi (n)
-1} mod \ quad n \] if n is prime, then
\ [\ varphi (n) =
n-1 \] Therefore, a reverse is pow_mod (a, n-2, n )

2. mode equation

(1) linear model equations

\[ ax\equiv b(mod\quad n) \]

Put it into ax-ny = b, no solution when the number of about d = gcd (a, n) is not b, or both sides while divided by d, to give a'x-n'y = b ', a ' = a / d, n '= n / d, b' = b / d, namely
\ [a'x \ equiv b '(
mod \ quad n') \] At this time, a 'and n' have coprime, so then left by a 'mold n' inverse element in the sense, the solution is:
\ [X \ equiv (a ') ^ {-. 1} B' (MOD \ Quad n ') \]
this solution is the modulus n' the remaining elements of a system, we must put the remaining modulo n represented as element lines. Order (A ') ^ -. 1 B' = P , corresponding to the above solution x = p, x = p + n ', x p + 2n =', x p + 3n '=, .... For mold n , if p + in 'and p + jn' congruence, the (p + in ') - ( p + jn') = (ij) n ' is a multiple of n , and therefore, (ij of) a d multiples (gcd (a, n)) of. In other words, the remaining lines modulo n,
\ [AX \ equiv B (MOD \ Quad n) \]
exactly d a solution of p, p + n ', p + 2n', p + 3n ',. .., p + (d-1 ) n '.

If you have multiple equations, variables, or only one, how should we do it?

Then use the following theorem

(2) Chinese remainder theorem (Chinese Remainder Theorem)

Consider equations
\ [x \ equiv a_i (mod
\ quad m_i) \] and all pairwise relatively prime mi. Let M be the product of all of mi, wi = M / mi, the gcd (wi, mi) = 1 .

Extended Euclidean Algorithm with pi and qi can be found such that wi * pi + mi * qi = 1. Then allowed ei = wi * pi, the equations equivalent to a single equation
\ [X \ equiv e_1a_1 + e_2a_2 + \ cdots + e_na_n (MOD \ Quad M) \]
, i.e. in modulo M remaining lines, primary equation with a unique solution.

Proof: The equation Wi PI mi + Qi = 1 on both sides of the mold can be obtained immediately mi
\ [e_i \ equiv 1 (MOD \ Quad m_i) \]
, whereas for all i not equal to j, wi mj is a multiple of, and therefore
\ [e_i \ equiv 0 (MOD \ Quad M_j) \]
, so that, when x0 mi of modulo addition eiai which a remainder is other than 1 * ai = ai, the remainder remaining terms are zero.

#define LL long long
LL crt(int n,int* a,int* m)
{
    LL M=1,d,x,y,x=0;
    for(int i=1;i<=n;++i) M*=m[i];
    for(int i=1;i<=n;++i)
    {
        LL w=M/m[i];
        exgcd(m[i],w,d,d,y);
        x=(x+y*w*a[i])%M;
    }
    return (x+M)%M;
}

(3) discrete logarithm

For simplicity, we only consider the simplest case, i.e., when n is a prime number, MODULUS equation
\ [a ^ x \ equiv b
(mod \ quad n) \] Since n is a prime number, as long as a is not 0 , there must be an inverse a ^ -1. Euler's theorem, simply by checking x = 0,1,2, ..., n- 1 is not a solution to. Since
\ [A ^ {n-1} \ equiv. 1 (MOD \ n-Quad) \]
, n-1 when x exceeds a ^ x when the cycle starts.

We check the front m (m is we make n ^ (1/2)) item, i.e., a ^ 0, a ^ 1, ..., a ^ value modulo n (m-1) if is b, and the a ^ imodn stored in the ei, and calculates the inverse a ^ m a ^ (- m).

Consider a ^ m, a ^ (m + 1), ..., a ^ (2m-1). The eleven do not check, because if one of them has a solution, that corresponds to the presence of i
\ [e_i * A ^ m \ equiv B (MOD \ n-Quad) \]
, with both sides by a ^ (- m) to give
\ [e_i \ equiv b '(mod
\ quad n) \] wherein
\ [b' = a ^ {
- m} b (mod \ quad n) \] Thus simply test whether such ei is equal to b 'to .

If not, then consider a ^ 2m, a ^ (2m + 1), ..., a ^ (3m-1), so that b '' = (a ^ (- 2m) * b) mod n

Therefore we have to enumerate a method according to a ^ (m * m-1)

int pow_mod(int a,int b,int mod)
{
    int ans=1;
    while(b)
    {
        if(b&1) ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans;
}
int log_mod(int a,int b,int n)
{
    int m,v,e=1;
    m=(int)sqrt(n+0.5);
    v=pow_mod(pow_mod(a,m,n),n-2,n);
    map<int,int> x;
    x[1]=0;
    for(int i=1;i<m;++i)
    {
        e=(e*a)%n;
        if(!x.count(e)) x[e]=i;
    }
    for(int i=0;i<m;++i)//考虑a^(im),a^(im+1),...,a^(im+m-1)
    {
        if(x.count(b)) return i*m+x[b];
        b=(b*v)%n;
    }
    return -1;
}//这就是可用于解决离散对数的大步小步算法(Baby_Step_Giant_Step Algorithm),复杂
//度O(n^(1/2)logn)

Guess you like

Origin www.cnblogs.com/iwillenter-top1/p/11616178.html