zoukankan      html  css  js  c++  java
  • 【BZOJ】【2982】Combination

    排列组合

      Lucas定理模板题……

      感觉我做题顺序有点问题啊……应该是BZOJ 2982-->HDOJ 3037-->BZOJ 1272

      好吧这个现在来看就有些水了……

      预处理一下fact和inv即可

     1 /**************************************************************
     2     Problem: 2982
     3     User: Tunix
     4     Language: C++
     5     Result: Accepted
     6     Time:4 ms
     7     Memory:1352 kb
     8 ****************************************************************/
     9  
    10 //BZOJ 2982
    11 #include<cstdio>
    12 #include<cstdlib>
    13 #include<cstring>
    14 #include<iostream>
    15 #include<algorithm>
    16 #define rep(i,n) for(int i=0;i<n;++i)
    17 #define F(i,j,n) for(int i=j;i<=n;++i)
    18 #define D(i,j,n) for(int i=j;i>=n;--i)
    19 using namespace std;
    20  
    21 int getint(){
    22     int v=0,sign=1; char ch=getchar();
    23     while(ch<'0'||ch>'9') {if (ch=='-') sign=-1; ch=getchar();}
    24     while(ch>='0'&&ch<='9') {v=v*10+ch-'0'; ch=getchar();}
    25     return v*=sign;
    26 }
    27 /*******************tamplate********************/
    28 const int N=10086,P=10007;
    29 int fac[N],inv[N],n,m;
    30 int pow(int a,int b){
    31     int r=1;
    32     for(;b;b>>=1,a=a*a%P)
    33         if (b&1) r=r*a%P;
    34     return r;
    35 }
    36 void calc(){
    37     fac[0]=1;
    38     F(i,1,P-1) fac[i]=fac[i-1]*i%P;
    39     inv[P-1]=pow(fac[P-1],P-2); inv[0]=1;
    40     D(i,P-2,1) inv[i]=inv[i+1]*(i+1)%P;
    41 }
    42 inline int c(int n,int m){
    43     if (n<m) return 0;
    44     if (n<P && m<P) return fac[n]*inv[m]%P*inv[n-m]%P;
    45     return c(n%P,m%P)*c(n/P,m/P)%P;
    46 }
    47 int main(){
    48     int T=getint();
    49     calc();
    50     while(T--){
    51         n=getint(); m=getint();
    52         printf("%d
    ",c(n,m));
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    C++的预处理(Preprocess)
    【转】深入分析Sleep(0)与Sleep(1)的区别
    【转】两个算法题,感觉挺有意思
    【转】求一个类的sizeof应考虑的问题
    const关键字总结
    C++11各编译器支持情况对比
    【转】C# 创建window服务
    关于C++中char型数组、指针及strcpy函数的细节观察
    用过的shell命令——持续更新
    初试rails 3.1
  • 原文地址:https://www.cnblogs.com/Tunix/p/4297194.html
Copyright © 2011-2022 走看看