zoukankan      html  css  js  c++  java
  • 【BZOJ】【1272】【BeiJingWC2008】Gate of Babylon

    组合数学+容斥原理

      Orz zyf-zyf

      多重集组合数0.0还带个数限制?  ——>  《组合数学》第6章  6.2带重复的组合

      组合数还要模P 0.0? ——> Lucas定理

      啊……要算组合数啊……除以阶乘神马的太麻烦肿么办?还要模P……没关系~我们可以搞预处理啊= =预处理粗来【阶乘%P】和【阶乘在模P意义下的逆元】

    1 void calc(){
    2     fac[0]=1;
    3     F(i,1,P-1) fac[i]=fac[i-1]*i%P;
    4     inv[P-1]=pow(fac[P-1],P-2); inv[0]=1;
    5     D(i,P-2,1) inv[i]=inv[i+1]*(i+1)%P;
    6 }
    预处理

      然后容斥么……反正只有15种暴力容斥搞搞就好了……就是枚举么……dfs一下

     1 /**************************************************************
     2     Problem: 1272
     3     User: Tunix
     4     Language: C++
     5     Result: Accepted
     6     Time:96 ms
     7     Memory:2840 kb
     8 ****************************************************************/
     9  
    10 //BZOJ 1272
    11 #include<cstdio>
    12 #include<cstdlib>
    13 #include<cstring>
    14 #include<iostream>
    15 #include<algorithm>
    16 #define F(i,j,n) for(int i=j;i<=n;++i)
    17 #define D(i,j,n) for(int i=j;i>=n;--i)
    18 using namespace std;
    19  
    20 int getint(){
    21     int v=0,sign=1; char ch=getchar();
    22     while(ch<'0'||ch>'9') {if (ch=='-') sign=-1; ch=getchar();}
    23     while(ch>='0'&&ch<='9') {v=v*10+ch-'0'; ch=getchar();}
    24     return v*=sign;
    25 }
    26 /*******************tamplate********************/
    27 typedef long long LL;
    28 const int N=100086;
    29 LL n,T,m,P,ans,fac[N],inv[N];
    30 int b[20];
    31 LL pow(LL a,LL b){
    32     LL r=1,base=a;
    33     for(;b;b>>=1,base=base*base%P)
    34         if (b&1) r=r*base%P;
    35     return r;
    36 }
    37 inline LL c(LL n,LL m){
    38     if (n<m) return 0;
    39     if (n<P && m<P) return fac[n]*inv[m]%P*inv[n-m]%P;
    40     return c(n%P,m%P)*c(n/P,m/P)%P;
    41 }
    42  
    43 void calc(){
    44     fac[0]=1;
    45     F(i,1,P-1) fac[i]=fac[i-1]*i%P;
    46     inv[P-1]=pow(fac[P-1],P-2); inv[0]=1;
    47     D(i,P-2,1) inv[i]=inv[i+1]*(i+1)%P;
    48 }
    49 void dfs(int x,int w1,LL w2){
    50     if (x==T+1){
    51         ans=(ans + w1*(c(m+n-w2,n)-c(n-w2-1,n)))%P;
    52         return;
    53     }
    54     dfs(x+1,-w1,w2+b[x]+1);
    55     dfs(x+1,w1,w2);
    56 }
    57 int main(){
    58 //  freopen("input.txt","r",stdin);
    59     n=getint(); T=getint(); m=getint(); P=getint();
    60     F(i,1,T) b[i]=getint();
    61     calc();
    62     dfs(1,1,0);
    63     printf("%lld
    ",(ans+P)%P);
    64     return 0;
    65 }
    View Code

    1272: [BeiJingWc2008]Gate Of Babylon

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 197  Solved: 92
    [Submit][Status][Discuss]

    Description

    Input

    Output

    Sample Input

    Sample Output

    12

    HINT

    Source

    [Submit][Status][Discuss]

      

      

  • 相关阅读:
    tfrecord
    数据挖掘模型中的IV和WOE详解
    GBDT
    tensorflow笔记 :常用函数说明
    GAN
    牛客挑战赛 39 牛牛与序列 隔板法 容斥 dp
    4.19 省选模拟赛 跳跃 倍增 二分 线段树 建图
    牛客挑战赛39 牛牛的等差数列
    luogu P6224 [BJWC2014]数据 KD-tree 标准板子 重构+二维平面内最近最远距离查询
    牛客挑战赛39 D 牛牛的数学题 NTT FMT FWT
  • 原文地址:https://www.cnblogs.com/Tunix/p/4297122.html
Copyright © 2011-2022 走看看