zoukankan      html  css  js  c++  java
  • 【HDOJ】【3037】Saving Beans

    排列组合

      啊……这题是要求c(n-1,0)+c(n,1)+c(n+1,2)+......+c(n+m-1,m)

      这个玩意……其实就等于c(n+m,m)

      好吧然后就是模P……Lucas大法好= =

      我SB地去预处理<P的所有fac和inv了……果断TLE

      事实上Lucas时对于<P的部分直接暴力算就好了

     1 //HDOJ 3037
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #define rep(i,n) for(int i=0;i<n;++i)
     8 #define F(i,j,n) for(int i=j;i<=n;++i)
     9 #define D(i,j,n) for(int i=j;i>=n;--i)
    10 using namespace std;
    11 
    12 int getint(){
    13     int v=0,sign=1; char ch=getchar();
    14     while(ch<'0'||ch>'9') {if (ch=='-') sign=-1; ch=getchar();}
    15     while(ch>='0'&&ch<='9') {v=v*10+ch-'0'; ch=getchar();}
    16     return v*=sign;
    17 }
    18 /*******************tamplate********************/
    19 const int N=100086;
    20 typedef long long LL;
    21 LL n,m,P,fac[N],inv[N],ans;
    22 LL pow(LL a,LL b){
    23     LL r=1,base=a;
    24     for(;b;b>>=1,base=base*base%P)
    25         if (b&1) r=r*base%P;
    26     return r;
    27 }
    28 inline LL c(LL n,LL m){
    29     if (n<m) return 0;
    30     if (n<P && m<P){
    31         LL a=1,b=1;
    32         for(;m;m--,n--){
    33             a=a*n%P;
    34             b=b*m%P;
    35         }
    36         return a*pow(b,P-2)%P;
    37     }
    38     return c(n%P,m%P)*c(n/P,m/P)%P;
    39 }
    40 int main(){
    41 //    freopen("input.txt","r",stdin);
    42     int T=getint();
    43     while(T--){
    44         n=getint(); m=getint(); P=getint();
    45         printf("%lld
    ",c(n+m,m));
    46     }
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    ZoomBar 设计
    旋转toast 自定义toast方向,支持多个方向的显示,自定义View
    NA
    ISCSI共享
    DFS序
    矩阵快速幂
    SOJ4389 川大贴吧水王 队列
    ST表学习总结
    HDU 5724 Chess(SG函数)
    2017 计蒜之道 初赛 第一场 A、B题
  • 原文地址:https://www.cnblogs.com/Tunix/p/4297177.html
Copyright © 2011-2022 走看看