当前位置 博文首页 > 文章内容

    [from CommonAnts]寻找 LCM

    作者: 栏目:未分类 时间:2020-09-13 16:00:34

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    题目

    传送门

    题解

    这道题在考场上还是卡了我一些时间 导致我 T3 没时间写了

    首先,这道题很显然是让我们求一个式子:

    \[\prod_{i=1}^n {c_i \choose x_i} \pmod p \]

    如果 \(p\) 可以保证是一个素数,显然这道题是很好做的,只需要预处理阶乘及逆元即可.

    对于 \(p\) 不是素数的情况,如果数据较小,我们可以考虑使用杨辉三角递推,求出 \(c_i\le 1000\) 的情况.

    这两种较为特殊的情况,我们都可以在 \(\mathcal O(n)\) 的复杂度下解决,但是新的问题来了——如果 \(p\) 不保证是素数,并且 \(c_i\) 还挺大 (\(c_i\le 10^6\))该怎么做?

    考虑回归其本质,将所有组合写成分数形式,有

    \[Ans=\prod_{i=1}^n\frac{c_i!}{x_i!(c_i-x_i)!} \]

    \(x!\),实际上就是对于区间 \([1,x]\) 中所有的数都多乘一次,即将其次数加一,对于分母,就是减一,这一类区间问题,我们可以考虑使用线段树或者树状数组,但是实际上我们只需要维护差分数组,最后做个前缀和,我们就可以求得每一项的次数应该是多少,这样做是 \(\mathcal O(n)\) 的.

    得到最终每个数的次数,我们还要计算其次方,如果直接暴力硬算而不取模,可能要直接爆掉 long long,但是取模不保证有逆元.

    我们考虑将每个数分解成更小的数,因为最终答案肯定是个整数,所以分解成质因数后每个质因数的次数肯定都是正的,对于分解,我们可以每次将这个数除以它最小的质因子,将其分解成更小的数乘上一个质数,从大到小循环,碰到质数直接乘进答案,否则继续分解即可.

    代码

    #include<cstdio>
    
    #define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
    #define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
    #define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define writc(a,b) fwrit(a),putchar(b)
    #define mp(a,b) make_pair(a,b)
    #define fi first
    #define se second
    typedef long long LL;
    // typedef pair<int,int> pii;
    typedef unsigned long long ull;
    typedef unsigned uint;
    #define Endl putchar('\n')
    // #define int long long
    //#define int unsigned
    // #define int unsigned long long
    
    #define cg (c=getchar())
    template<class T>inline void read(T& x){
        char c;bool f=0;
        while(cg<'0'||'9'<c)f|=(c=='-');
        for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
        if(f)x=-x;
    }
    template<class T>inline T read(const T sample){
        T x=0;char c;bool f=0;
        while(cg<'0'||'9'<c)f|=(c=='-');
        for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
        return f?-x:x;
    }
    template<class T>void fwrit(const T x){//just short,int and long long
        if(x<0)return (void)(putchar('-'),fwrit(-x));
        if(x>9)fwrit(x/10);
        putchar(x%10^48);
    }
    template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
    template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
    template<class T>inline T fab(const T x){return x>0?x:-x;}
    inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
    inline void getInv(int inv[],const int lim,const int MOD){
        inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
    }
    inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
        return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
    }
    
    const int maxn=1e6;
    
    int n,p,maxv;
    int x[maxn+5],c[maxn+5],cnt[maxn+5];
    
    inline int qkpow(int a,int n){
        int ret=1;
        for(;n>0;n>>=1,a=1ll*a*a%p)if(n&1)ret=1ll*ret*a%p;
        return ret;
    }
    
    inline void modify(const int l,const int r,const int x){
        cnt[l]+=x,cnt[r+1]+=-x;
    }
    inline void build(){
        rep(i,1,maxv)cnt[i]+=cnt[i-1];
    }
    
    bool vis[maxn+5];
    int prime[maxn+5],pcnt;
    int min_mod[maxn+5];
    inline void sieve(){
        vis[1]=true;
        rep(i,2,maxn){
            if(!vis[i])prime[++pcnt]=i;
            for(int j=1;j<=pcnt && i*prime[j]<=maxn;++j){
                vis[i*prime[j]]=true;
                min_mod[i*prime[j]]=prime[j];
                if(i%prime[j]==0)break;
            }
        }
    }
    
    int pow[maxn+5];
    
    inline void divide(int x,const int val){
        if(x==1)return;
        if(!vis[x])return pow[x]+=val,void();
        int del;
        for(int i=1;i<=pcnt && prime[i]<=x;++i)if(x%prime[i]==0){
            del=0;
            while(x%prime[i]==0)x/=prime[i],++del;
            pow[prime[i]]+=val*del;
        }
    }
    
    int ans=1;
    
    inline void down(const int x,const int val){
        if(!vis[x]){
            ans=1ll*ans*qkpow(x,val)%p;
            return;
        }
        cnt[x/min_mod[x]]+=val;
        cnt[min_mod[x]]+=val;
    }
    
    signed main(){
        // freopen("speech.in","r",stdin);
        // freopen("speech.out","w",stdout);
        n=read(1),p=read(1);
        rep(i,1,n)x[i]=read(1);
        rep(i,1,n)maxv=Max(c[i]=read(1),maxv);
        rep(i,1,n)if(c[i]!=x[i]){
            modify(x[i]+1,c[i],1);
            modify(1,c[i]-x[i],-1);
        }build();
        sieve();
        fep(i,maxv,2)if(cnt[i])down(i,cnt[i]);
        writc(ans,'\n');
        return 0;
    }