September 15, 2019 - The number of monographic Course summary (day2)

Cancer \ (xgzc, QwQ \) ~~~~~~

\(PartA:gcd\)

The amount of the specific ideas and proven not to say, do not know this, then retireThe whereabouts of Euclid mathematicsIt ......

inline int gcd(int a,int b)
{
    return !b?a:gcd(b,a%b);
}

\(extend:exgcd\)

Forehead, which do not permit it, that does not \ (yy \) what came out of it ~ ~
solvability necessary and sufficient conditions:

\ [\ Text {In} ~ ~ ~ ax + by = c ~ ~ ~ \ text {has} ~ ~ ~ gcd (a, b) | c \]

May assume \ (AX + by = GCD (A, B) \) , there are:
\ [AX + by = GCD (A, B) = BX ^ { '} + (A ~ MOD ~ B) Y ^ {' } = gcd (b, a ~
mod ~ b) \] me remove item will engage recursively:

int x,y;
inline void exgcd(int a,int b)
{
    if(!b)
    {
        x=1,y=0;
        return;
    }
    exgcd(b,a%b);
    int tmp=x;
    x=y;
    y=tmp-a/b*y;
}

Template: frog date :

#include<bits/stdc++.h>
using namespace std;
#define in(a) a=read()
#define ll long long
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
ll jie[2];
inline ll gca(ll s,ll t)
{
    if(t==0) return s;
    return gca(t,s%t);
}
inline void getit(ll s,ll t)
{
    if(t==0)
    {
        jie[0]=1;
        jie[1]=0;
        return;
    }
    getit(t,s%t);
    ll u=jie[1],v=jie[0]-s/t*jie[1];
    jie[0]=u;
    jie[1]=v;
}
int main()
{
    ll n,x,y,a,b;
    in(x);in(y);in(a);in(b);in(n);
    if(a<b) swap(a,b),swap(x,y);
    ll delta=y-x;
    ll q=gca(a-b,n);
    if(delta%q!=0)
    {
        printf("Impossible\n");
        return 0;
    }
    getit(a-b,n);
    ll ans=jie[0]*delta/q;
    ll r=n/q;
    ans=(ans%r+r)%r;
    printf("%lld\n",ans);
    return 0;
}

\ (PartB: \) Fermat's little theorem and Euler's theorem

Fermat's little theorem:
\ [A-P. 1 ^ {} \ equiv1 \ ~~~ PMOD {P} (== D_P. 1) \]

Euler's theorem:
\ [A ^ B \ equiv A ^ B {\% \ Phi (n-)} \ {n-PMOD} \]

\ (Part: BSGS \)

The main function is used to solve the equations similar to the following:
\ [A ^ X \ equiv B \ PMOD {C} \]
PCA course \ (X \) a \ (QwQ \) ~~~

Forehead, face persimmon outset ignorant force,Still now……

Okay, now I'm still just konjac practice, hoping to someday be able to thoroughly confused a bit:

Order \ (m = \ lceil \ sqrt {p} \ rceil, x = my + z (y, z \ text { } are variable) \) , then when \ (a ^ x \ equiv b \ pmod {c} \) there is the establishment \ (A ^ A ^ {My} * Z \ equiv B \ PMOD {C} \) , there shift key click \ (a ^ {my} \ equiv \ frac {b} {a ^ z } \ {C} PMOD \) , pretreatment of the \ (\ forall z \ in \ left [1, m \ right] \) of \ (\ frac {b} { a ^ z} \) and the presence of \ (Map \) inside, and then enumerate \ (y \) judge whether the solution can be.

Time complexity \ (O (\ n-sqrt {}) \) :

inline int sol(int a,int b,int c)
{
    if(!(a%c)) return -1;
    int ans=b%c,m=ceil(sqrt(c));
    mp[ans]=0;
    fur(i,1,m)
    {
        ans=ans*a%c;
        mp[ans]=i;
    }
    int tmp=power(a,m,c);ans=1;
    fur(i,1,m)
    {
        ans=ans*tmp%c;
        if(mp[ans]) return (i*m-mp[ans])%c+c)%c;
    }
    return -1;
}

Template: \ (Discrete ~ Logging \) :

#include<bits/stdc++.h>
using namespace std;
#define in read()
#define re register
#define fur(i,a,b) for(re int i=a;i<=b;++i)
#define fdr(i,a,b) for(re int i=a;i>=b;--i)
#define cl(a,b) memset(a,b,sizeof(a))
#define jinitaimei signed
#define int long long
inline int read()
{
    int x=0;
    char ch=getchar();
    for(;!isalnum(ch);ch=getchar());
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x;
}
map <int,int> mp;
inline int power(int x,int k,int mod)
{
    int res=1;
    for(;k;k>>=1,x=x*x%mod) if(k&1) res=res*x%mod;
    return res;
}
inline void sol(int a,int b,int c)
{
    if(!(a%c))
    {
        puts("no solution");
        return;
    }
    int ans=b%c,m=ceil(sqrt(c));
    bool flag=false;
    mp[ans]=0;
    fur(i,1,m)
    {
        ans=ans*a%c;
        mp[ans]=i;
    }
    int tmp=power(a,m,c);ans=1;
    fur(i,1,m)
    {
        ans=ans*tmp%c;
        if(mp[ans])
        {
            printf("%lld\n",((i*m-mp[ans])%c+c)%c);
            flag=true;
            break;
        }
    }
    if(!flag) puts("no solution");
}
jinitaimei main()
{
    int a,b,c;
    while(scanf("%lld%lld%lld",&c,&a,&b)!=EOF)
    {
        mp.clear();
        sol(a,b,c);
    }
    return 0;
}

\ (Extend: exbsgs \) (where the number is not a prime)
the \ (XG \) , said bit tumor, it is the direct calculations persimmon:
\ [A ^ X \ equiv B \ PMOD C \ ^ X Rightarrow A = KC + B \]
\ [\ text {so} ~ G = GCD (A, C), \ THEREFORE \ FRAC {B} {G} \ in N ^ {*} \]
\ [\ Rightarrow \ FRAC {A} {G } A ^ {X-. 1} + K \ FRAC {P} {G} \ equiv \ FRAC {B} {G} \ PMOD {\ FRAC {P} {G}} \]
\ [\ Rightarrow A ^ {X } -1 \ equiv \ FRAC {B} {G} (\ FRAC {A} {G}) ^ {-}. 1 \ PMOD {\ FRAC {P} {G}} \]
\ [\ A ^ {X Rightarrow ^ { '}} \ equiv b ^ {'} \ pmod {m ^ { '}} (x ^ {'} = x-1, b ^ { '} = \ frac {b} {g} (\ frac { a} {g}) ^ {
- 1}, m ^ { '} = \ frac {p} {g}) \] analogous \ (exgcd \) thought to recursively

\(PartD:crt\)

This is used to seek congruence equations:
\ [\ Cases the begin {X} \ equiv A_1 \ P_1 PMOD {X} \\ \ equiv A_2 \ {P_2 PMOD ......} \\ \\ X \ equiv A_N \ PMOD {p_n} \ end {cases}
\] where \ (P_i \) are prime

Looks like there is a use of chestnuts Han soldiers? ?

Or that it's normal:

(No template problem, not to code)
\ [\ text {set} M = \ prod \ limits_ { i = 1} ^ n p_i, m_i = \ frac {M} {p_i}, t_i \ equiv \ frac { m_i} {}. 1 \ PMOD {P_i} \]
\ [\ then ask for the text {} ans \ equiv \ sum a_it_im_i \ pmod {M} \]

\ (extend: excrt \ text { ( many words QwQ-EX)} \) :

This is not a prime number a little uncomfortable

Consider twenty-two merger equation, \ (exgcd \) to:

const int xx=1e5+1612;
int t[xx],p[xx];
int k1,k2,x,n,M;
inline int gcd(int a,int b){return !b?a:gcd(b,a%b);}
inline void exgcd(int a,int b)
{
    if(!b)
    {
        k1=1,k2=0;
        return;
    }
    exgcd(b,a%b);
    int tmp=k1;
    k1=k2;k2=tmp-a/b*k2;
}
inline int Fast_mul(int a,int b,int mod)
{
    int res=0;
    for(;b;b>>=1,a=(a+a)%mod) if(b&1) res=(res+a)%mod;
    return res;
}
inline void excrt()
{
    M=p[1];x=t[1];
    fur(i,2,n)
    {
        int a=M,b=p[i];
        int c=(t[i]-x%b+b)%b;
        int tmp=gcd(a,b);
        exgcd(a,b);
        int lcm=b/tmp;
        k1=Fast_mul(k1,c/tmp,lcm);
        x+=k1*M;
        M*=lcm;
        x=(x%M+M)%M;
    }
    printf("%lld\n",x);
}

Template :

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define in read()
#define re register
#define fur(i,a,b) for(re int i=a;i<=b;++i)
#define fdr(i,a,b) for(re int i=a;i>=b;--i)
#define cl(a,b) memset(a,b,sizeof(a))
#define jinitaimei signed
#define int long long
inline int read()
{
    int x=0;
    char ch=getchar();
    for(;!isalnum(ch);ch=getchar());
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x;
}
const int xx=1e5+1612;
int t[xx],p[xx];
int k1,k2,x,n,M;
inline int gcd(int a,int b){return !b?a:gcd(b,a%b);}
inline void exgcd(int a,int b)
{
    if(!b)
    {
        k1=1,k2=0;
        return;
    }
    exgcd(b,a%b);
    int tmp=k1;
    k1=k2;k2=tmp-a/b*k2;
}
inline int Fast_mul(int a,int b,int mod)
{
    int res=0;
    for(;b;b>>=1,a=(a+a)%mod) if(b&1) res=(res+a)%mod;
    return res;
}
inline void sol()
{
    M=p[1];x=t[1];
    fur(i,2,n)
    {
        int a=M,b=p[i];
        int c=(t[i]-x%b+b)%b;
        int tmp=gcd(a,b);
        exgcd(a,b);
        int lcm=b/tmp;
        k1=Fast_mul(k1,c/tmp,lcm);
        x+=k1*M;
        M*=lcm;
        x=(x%M+M)%M;
    }
    printf("%lld\n",(x%M+M)%M);
}
inline void init()
{
    n=in;
    fur(i,1,n) p[i]=in,t[i]=in;
}
jinitaimei main()
{
    init();
    sol();
    return 0;
}

Tulong Yong Shi :

The idea quite interesting, you need to look at the question, two cases (available \ (excrt \) and is not available but do not need to use), then column-wise, simplifying the equation, the last thing you can do with templates friends :

#include<bits/stdc++.h>
using namespace std;
#define re register
#define fur(i,a,b) for(re int i=a;i<=b;++i)
#define fdr(i,a,b) for(re int i=a;i>=b;--i)
#define jinitaimei signed
#define int long long
const int xx=1e5+1612;
int t[xx],p[xx],cnt;
int k1,k2,x,n,M;
int tmp1[xx],tmp2[xx],tmp3[xx],tmp4[xx];
bool flag;
multiset <int> s;
multiset <int>:: iterator it;
inline int gcd(int a,int b){return !b?a:gcd(b,a%b);}
inline void exgcd(int a,int b)
{
    if(!b)
    {
        k1=1,k2=0;
        return;
    }
    exgcd(b,a%b);
    int tmp=k1;
    k1=k2;
    k2=tmp-a/b*k2;
}
inline int Fast_mul(int a,int b,int mod)
{
    int res=0;
    if(b<0) return 1;
    for(;b;b>>=1,a=(a+a)%mod) if(b&1) res=(res+a)%mod;
    return res;
}
inline void sol()
{
    if(flag) return;
    M=p[1];x=t[1];
    fur(i,2,cnt)
    {
        int a=M,b=p[i];
        int c=((t[i]-x%b+b)%b+b)%b;
        int tmp=gcd(a,b);
        exgcd(a,b);
        int lcm=b/tmp;
        if(c%tmp)
        {
            puts("-1");
            return;
        }
        k1=Fast_mul(k1,c/tmp,lcm);
        x+=k1*M;
        M*=lcm;
        x=(x%M+M)%M;
    }
    printf("%lld\n",(x%M+M)%M);
}
inline void init()
{
    s.clear();
    flag=false;cnt=0;
    int tmp=0,m;
    scanf("%lld%lld",&n,&m);
    fur(i,1,n) scanf("%lld",&tmp1[i]);
    fur(i,1,n){scanf("%lld",&tmp2[i]);if(tmp2[i]<tmp1[i]) flag=true;}
    fur(i,1,n) scanf("%lld",&tmp4[i]);
    fur(i,1,m) scanf("%lld",&tmp3[i]);
    fur(i,1,m) s.insert(tmp3[i]);
    fur(i,1,n)
    {
        it=(tmp1[i]<(*s.begin()))?s.begin():(--s.upper_bound(tmp1[i]));
        tmp3[i]=*it;
        s.erase(it);
        s.insert(tmp4[i]);
    }
    if(flag)
    {
        fur(i,1,n) tmp=max(tmp,(int)ceil((double)tmp1[i]/(double)tmp3[i]));
        printf("%lld\n",tmp);
    }
}
inline void handle()
{
    if(flag) return;
    fur(i,1,n)
    {
        if(tmp1[i]==tmp3[i]) continue;
        int tmp=gcd(tmp3[i],tmp2[i]);
        exgcd(tmp3[i],tmp2[i]);
        if(tmp1[i]%tmp)
        {
            puts("-1"),flag=true;
            return;
        }
        k1=Fast_mul(k1,tmp1[i]/tmp,tmp2[i]/tmp);
        t[++cnt]=k1;
        p[cnt]=tmp2[i]/tmp;
    }
}
jinitaimei main()
{
    int T;
    scanf("%lld\n",&T);
    while(T--)
    {
        init();
        handle();
        sol();
    }
    return 0;
}

\ (Part Lucas \)

To a formula like, you surely will,escape……( \ (P \) is a prime number):
\ [C ^ M_n \ equiv C_ {n-\% P} ^ {m \% P} * C_ {n-/ P} ^ {m / P} \ PMOD {P} \ ]

Template :

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;i++)
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
inline ll power(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b>0)
    {
        if(b&1) ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
ll jc[250000];
inline ll lucas(ll m,ll n,ll mod)
{
    if(m>n) return 0;
    if(m==0) return 1;
    if(m<mod&&n<mod) return jc[n]*power(jc[m]*jc[n-m]%mod,mod-2,mod)%mod;
    return lucas(m/mod,n/mod,mod)*lucas(m%mod,n%mod,mod)%mod;
}
int main()
{
    ll t;t=in;
    while(t--)
    {
        ll n,m,p;
        n=in;m=in;p=in;
        jc[0]=1;
        fur(i,1,n+m) jc[i]=i*jc[i-1]%p;
        printf("%lld\n",lucas(m,n+m,p));
    }
    return 0;
}

\ (Extend: exLucas \) :
This needs to bother with \ (CRT \) to satisfy the equation such that the number of parting ordinary conditions of formula

Being too much food, not to the code (I'm not really not)

\ (PartF: \ text {quadratic residue} \)

Time reasons, but also to change the exams, put a big brother blog here first, next time write

Templates & Templates solution to a problem :

#include<bits/stdc++.h>
using namespace std;
#define in read()
#define re register
#define fur(i,a,b) for(re int i=a;i<=b;++i)
#define fdr(i,a,b) for(re int i=a;i>=b;--i)
#define cl(a,b) memset(a,b,sizeof(a))
#define jinitaimei signed
#define int long long
inline int read()
{
    int x=0;
    char ch=getchar();
    for(;!isalnum(ch);ch=getchar());
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x;
}
struct num
{
    int x,y;
};
int w,q;
inline num times(num a,num b,int p)
{
    num ans;
    ans.x=(a.x*b.x%p+a.y*b.y%p*w%p+p)%p;
    ans.y=(a.y*b.x%p+a.x*b.y%p+p)%p;
    return ans;
}
inline int pow1(num a,int b,int p)
{
    num ans={1,0};
    for(;b;b>>=1,a=times(a,a,p)) if(b&1) ans=times(ans,a,p);
    return ans.x%p;
}
inline int pow2(int a,int b,int p)
{
    int ans=1;
    for(;b;b>>=1,a=a*a%p) if(b&1) ans=ans*a%p;
    return ans%p;
}
inline int sol(int n,int p)
{
    n%=p;
    if(pow2(n,(p-1)/2,p)==p-1) return -1;
    while(true)
    {
        q=rand()%p;
        w=(q*q%p-n%p+p)%p;
        if(pow2(w,(p-1)/2,p)==p-1) break;
    }
    return pow1((num){q,1},(p+1)/2,p);
}
jinitaimei main()
{
    int T=in;
    while(T--)
    {
        int n=in,p=in;
        if(!n)
        {
            puts("0");
            continue;
        }
        int ans1=sol(n,p),ans2=p-ans1;
        if(ans1==-1) puts("Hola!");
        else
        {
            if(ans1==ans2) printf("%lld\n",ans1);
            else if(ans1<ans2) printf("%lld %lld\n",ans1,ans2);
            else printf("%lld %lld\n",ans2,ans1);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ALANALLEN21LOVE28/p/11564866.html