组合计算的性质:

C(n,m)= m! / (n!(m-n)!)

C(n,m)=C(m-n,m); C(n,m)=C(n,m-1)+C(n-1,m-1);

二项式定理:(a+b)^n=sigema(k=0~n) C(k,n)*a^k*b^(n-k)

lucas定理:C(n,m)≡C(n%p,m%p)*C(n/p,m/p)  (mod p)

catalan数: Cat(n)=C(n,2n)/n+1  Cat(n)=Cat(n-1)*(4n-2)/(n+1)

计算系数 通过二项式定理变形其实就是求C(n,k)*a^n*b^m,用下逆元

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL mod=; LL quick_pow(LL A,LL p)
{
int ret=;
while(p!=)
{
if(p%==)ret=(ret*A)%mod;
A=(A*A)%mod;p/=;
}
return ret;
}
LL jiecheng(LL k)
{
LL ret=;
for(int i=;i<=k;i++)ret=(ret*i)%mod;
return ret;
}
LL getniyuan(LL k)
{
return quick_pow(k,mod-);
} int main()
{
LL a,b,k,n,m;
scanf("%lld%lld%lld%lld%lld",&a,&b,&k,&n,&m);
LL ans=( (jiecheng(k)*getniyuan( jiecheng(n)*jiecheng(k-n)%mod )%mod) *
(quick_pow(a,n)*quick_pow(b,m)%mod) )%mod;
printf("%lld\n",ans);
return ;
}

计算系数

Counting Swaps 神仙题,%lyd吧。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL mod=1e9+; LL quick_pow(LL A,LL p)
{
int ret=;
while(p!=)
{
if(p%==)ret=(ret*A)%mod;
A=(A*A)%mod;p/=;
}
return ret;
}
LL jiecheng(LL k)
{
LL ret=;
for(int i=;i<=k;i++)ret=(ret*i)%mod;
return ret;
}
LL getniyuan(LL k)
{
return quick_pow(k,mod-);
} int nxt[];
bool v[];
LL dfs(int x,int d)
{
if(v[x]==true)return d;
v[x]=true;
dfs(nxt[x],d+);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&nxt[i]); LL tot=,ans=,cnt;cnt=n;
memset(v,false,sizeof(v));
for(int i=;i<=n;i++)
{
if(v[i]==false)
{
int L=dfs(i,);
if(L>)
{
tot++;
ans=(ans*quick_pow(L,L-)%mod)*getniyuan(jiecheng(L-))%mod;
}
else cnt--;
}
}
if(tot==)printf("1\n");
else
{
ans=ans*jiecheng(cnt-tot)%mod;
printf("%lld\n",ans);
}
}
return ;
}

Count Swaps

bzoj1951 由欧拉定理的推论,变成计算sigema(d|n)C(d,n)%(mod-1),这个用卢卡斯定理搞,但是mod不是质数,那么分解分别搞,然后得到4个同余方程,解出就是指数了,最后快速幂即可。

upd:感觉之前的阶乘模数有点假。。但是应该是对的

快速幂+crt并没有exgcd优秀的说。。。卡了一波常数才过的。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL mod=;
const LL md[]={,,,}; int quick_pow(int A,int p,int mod)
{
int ret=;
while(p!=)
{
if(p%==)ret=(LL)ret*A%mod;
A=(LL)A*A%mod;p/=;
}
return ret;
}
LL fac[][],fac_inv[][];
void initC()
{
for(int j=;j<=;j++)fac[j][]=,fac_inv[j][]=;
for(int i=;i<=md[];i++)
for(int j=;j<=;j++)
fac[j][i]=fac[j][i-]*i%md[j],fac_inv[j][i]=quick_pow(fac[j][i],md[j]-,md[j]);
}
LL getC(LL n,LL m,LL p){return fac[p][n]*fac_inv[p][m]%md[p]*fac_inv[p][n-m]%md[p];}
LL lucas(LL n,LL m,LL p)
{
LL ans=;
while(m>)
{
LL a=n%md[p],b=m%md[p];
if(a<b)return ;
ans=ans*getC(a,b,p)%md[p];
n/=md[p],m/=md[p];
}
return ans;
} LL a[];
LL solve(LL n,LL k)
{
for(int i=;i<=;i++)a[i]=lucas(n,k,i);
LL M=mod-,x=;
for(int i=;i<=;i++)//crt
x=(x+a[i]*(M/md[i])*quick_pow(M/md[i],md[i]-,md[i]))%(mod-);
return x;
} int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
initC();
int n,G,p=;
scanf("%d%d",&n,&G);
if(G==mod){printf("0\n");return ;}
for(int i=;i*i<=n;i++)
if(n%i==)
{
p=(p+solve(n,i))%(mod-);
if(i*i!=n)p=(p+solve(n,n/i))%(mod-);
}
printf("%d\n",quick_pow(G,p,mod));
return ;
}

bzoj1951(upd)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL mod=;
const LL md[]={,,,}; LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(a==)
{
x=;y=;
return b;
}
else
{
LL tx,ty;
LL d=exgcd(b%a,a,tx,ty);
x=ty-b/a*tx;
y=tx;
return d;
}
}
LL quick_pow(LL A,LL p)
{
LL ret=;
while(p!=)
{
if(p%==)ret=(ret*A)%mod;
A=(A*A)%mod;p/=;
}
return ret;
}
LL inv(LL a,LL p)
{
LL x,y;
LL d=exgcd(a,p,x,y);
return (x%p+p)%p;
} //--------------------------------------------------- LL jc[];
LL lucas(LL N,LL M,LL p)
{
LL ans=;
while(M>)
{
LL a=N%p,b=M%p;
if(a>b)return ; ans=ans*jc[b]%p;
ans=ans*inv(jc[a],p)%p;
ans=ans*inv(jc[b-a],p)%p; N/=p;M/=p;
}
return ans;
}//lucas
LL g[];
void getzs(LL n)
{
jc[]=;
for(int i=;i<=md[];i++)jc[i]=(jc[i-]*i)%(mod-); for(int i=;i*i<=n;i++)
{
if(n%i==)
{
for(int j=;j<=;j++)
g[j]=(g[j]+lucas(i,n,md[j]))%md[j];
if(i!=n/i)
{
for(int j=;j<=;j++)
g[j]=(g[j]+lucas(n/i,n,md[j]))%md[j];
}
}
}
} //--------------------------------------------------- LL u1,v1,u2,v2;
void merge()
{
LL A=u1,B=u2,K=v2-v1,x,y;
LL d=exgcd(A,B,x,y); x=(x*(K/d)%(B/d)+(B/d))%(B/d);
v1=u1*x+v1;
u1=u1/d*u2;
}
LL solve()
{
u1=md[],v1=g[];
for(int i=;i<=;i++)
u2=md[i], v2=g[i], merge();
return v1;
} int main()
{
LL n,g;
scanf("%lld%lld",&n,&g);g%=mod;
if(g==){printf("0\n");return ;} getzs(n);
LL p=solve();
printf("%lld\n",quick_pow(g,p));
return ;
}

bzoj1951

0x36 组合计数的更多相关文章

  1. 算法竞赛进阶指南0x36组合计数

    概述 AcWing211. 计算系数 #include <bits/stdc++.h> using namespace std; const int mod = 10007 ; int k ...

  2. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  3. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [分治FFT 组合计数 | 多项式求逆]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  4. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [FFT 组合计数 容斥原理]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  5. 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)

    [HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...

  6. [总结]数论和组合计数类数学相关(定理&证明&板子)

    0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...

  7. 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)

    [BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...

  8. 【BZOJ5305】[HAOI2018]苹果树(组合计数)

    [BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...

  9. 【BZOJ3142】[HNOI2013]数列(组合计数)

    [BZOJ3142][HNOI2013]数列(组合计数) 题面 BZOJ 洛谷 题解 唯一考虑的就是把一段值给分配给\(k-1\)天,假设这\(k-1\)天分配好了,第\(i\)天是\(a_i\),假 ...

随机推荐

  1. E - Dividing Orange

    Problem description One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments ...

  2. C#中DBNull问题

    当数据库中一个字段不是必填项时,在往数据库中插入数据的时候往往会插入一个空字符串就草草了事了.在这里用DBNull可以解决这个问题 /// <summary> /// 插入数据 /// & ...

  3. sublime3 install python3

    链接地址:https://blog.csdn.net/Ti__iT/article/details/78830040

  4. 关于React-native的介绍以及环境搭建

    React-Native介绍(后面内容的RN就是指react-native) 由facebook公司推出的,基于react,能开发原生app 原理: 1. 利用react框架写好js代码 2. 利用p ...

  5. UWP App Services in Windows 10

    1.AppServices in Universal Windows Platform(UWP) UWP 应用服务可以提供给另一个UWP应用.在Win10系统中,一个应用服务当作应用的一个方法和机制来 ...

  6. CDR是什么?CorelDRAW矢量绘图

    CorelDRAW是矢量绘图软件 CorelDRAW Graphics Suite是加拿大Corel公司的平面设计软件: CorelDRAW 非凡的设计能力广泛地应用于商标设计.标志制作.模型绘制.插 ...

  7. virtualenv 虚拟环境依赖安装

    虚拟环境依赖安装 开发要学会用 virtualenv 来管理多个开发环境 Ubuntu/Centos/MacOS 下 virtualenvwrapper 使得virtualenv变得更好用,所以我们一 ...

  8. 【JavaScript框架封装】使用Prototype给Array,String,Function对象的方法扩充

    /* * @Author: 我爱科技论坛* @Time: 20180705 * @Desc: 实现一个类似于JQuery功能的框架* V 1.0: 实现了基础框架.事件框架.CSS框架.属性框架.内容 ...

  9. iview datepicker 选择的时间少一天

    使用iview的datepicker时间选择器发现获取的value值是比实际要少一天,严格来说应该是时间格式不一样,datepicker获取的时间是UTC时间格式,也就是:yyyy-MM-ddTHH: ...

  10. BZOJ 4712 洪水 (线段树+树剖动态维护DP)

    题目大意:略 题目传送门 数据结构好题,但据说直接上动态DP会容易处理不少,然而蒟蒻不会.一氧化碳大爷说还有一个$log$的做法,然而我只会$log^{2}$的.. 考虑静态时如何处理,设$f[x]$ ...