zoukankan      html  css  js  c++  java
  • 带除法的取模运算

    type1 $frac{x}{y}\%P,其中P是大质数$

    用费马小小定理得:

    $y^{P-1}equiv 1(mod P)$

    故:

    $frac{x}{y}\%P=frac{x*y^{P-1}}{y}\%P=x*y^{P-2}\%P$

    type2 $frac{x}{y}\%P,其中x和y可分解质因数$

    我们还是用一些例子来讲比较好一些。

    求卡特兰数$frac{C_{2n}^{n}}{n+1}\%P$

    $frac{C_{2n}^{n}}{n+1}\%P$

    $=frac{(n+2) imes (n+3) imes ... imes (2n)}{1 imes 2 imes ... imes (n-1) imes n}\%P$

    一个直接的想法是分别将分子和分母分解质因数,但是这样写起来很恶心。

    我们这样想:

    将$n^{i}$分解质因数:

    $n^{i}=(p_{1}^{k_{1}} imes p_{2}^{k_{2}} imes... imes p_{t}^{k_{t}})^i$

    我们任取n的一个质因子,不妨为$p_{1}$。

    $n=(p_{1})^{i} imes (p_{1}^{k_{1}-1} imes p_{2}^{k_{2}} imes... imes p_{t}^{k_{t}})^{i}$

    其实就是$p_{1}$多了i个,$p_{1}^{k_{1}-1} imes p_{2}^{k_{2}} imes... imes p_{t}^{k_{t}}$多了i个。

    我们可以交给$p_{1}$和$p_{1}^{k_{1}-1} imes p_{2}^{k_{2}} imes... imes p_{t}^{k_{t}}$做。

    如果n本来就是质数,直接快速幂。

    找n的质因子可以用线性筛。

    下面是bzoj1485代码,就是求$frac{C_{2n}^{n}}{n+1}\%P$。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<fstream>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<utility>
    #include<set>
    #include<bitset>
    #include<vector>
    #include<functional>
    #include<deque>
    #include<cctype>
    #include<climits>
    #include<complex>
    //#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
     
    using namespace std;
    
    typedef long long LL;
    typedef double DB;
    typedef pair<int,int> PII;
    typedef complex<DB> CP;
    
    #define mmst(a,v) memset(a,v,sizeof(a))
    #define mmcy(a,b) memcpy(a,b,sizeof(a))
    #define fill(a,l,r,v) fill(a+l,a+r+1,v)
    #define re(i,a,b)  for(i=(a);i<=(b);i++)
    #define red(i,a,b) for(i=(a);i>=(b);i--)
    #define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
    #define fi first
    #define se second
    #define m_p(a,b) make_pair(a,b)
    #define SF scanf
    #define PF printf
    #define two(k) (1<<(k))
    
    template<class T>inline T sqr(T x){return x*x;}
    template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
    template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}
    
    const DB EPS=1e-9;
    inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
    const DB Pi=acos(-1.0);
    
    inline int gint()
      {
            int res=0;bool neg=0;char z;
            for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
            if(z==EOF)return 0;
            if(z=='-'){neg=1;z=getchar();}
            for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
            return (neg)?-res:res; 
        }
    inline LL gll()
      {
          LL res=0;bool neg=0;char z;
            for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
            if(z==EOF)return 0;
            if(z=='-'){neg=1;z=getchar();}
            for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
            return (neg)?-res:res; 
      }
    
    const int maxN=2000000;
    
    int N;LL P;
    LL ans;
    
    inline LL power(LL a,LL k){LL x=1,y=a;while(k){if(k&1)x=x*y%P;k>>=1;y=y*y%P;}return x;}
    
    int flag[maxN+100],cnt,prime[maxN+100];
    int a[maxN+100];
    LL b[maxN+100];
    
    int main()
      {
          freopen("bzoj1485.in","r",stdin);
            freopen("bzoj1485.out","w",stdout);
            int i,j;
            cin>>N>>P;
            re(i,2,2*N)
              {
                  if(!flag[i])prime[++cnt]=i;
                  for(j=1;j<=cnt && i*prime[j]<=2*N;j++)
                    {
                        flag[i*prime[j]]=1;
                        a[i*prime[j]]=prime[j];
                        if(i%prime[j]==0)break;
                    }
              }
            re(i,2,N)b[i]=-1;
            re(i,N+2,2*N)b[i]=1;
            ans=1;
            red(i,2*N,2)
              if(!flag[i])
                ans=ans*power(LL(i),b[i])%P;
              else
                b[a[i]]+=b[i],b[i/a[i]]+=b[i];
            cout<<ans<<endl;
            return 0;
        }
    View Code

     type 3

    $frac{a}{b}\%m=frac{a\%bm}{b}$

  • 相关阅读:
    c++字符串
    Ubuntu系统的安装与使用 深度音乐播放器
    Ubuntu14.04安装wineqq国际版
    pythonchallenge 解谜 Level 0
    Ubuntu 14.04 安装pdf阅读器
    Ubuntu换源
    pythonchallenge 解谜
    java实现图像灰度化
    c语言完成宽带拨号
    应用程序权限设计
  • 原文地址:https://www.cnblogs.com/maijing/p/4711854.html
Copyright © 2011-2022 走看看