0x36 组合计数
组合计算的性质:
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 组合计数的更多相关文章
- 算法竞赛进阶指南0x36组合计数
概述 AcWing211. 计算系数 #include <bits/stdc++.h> using namespace std; const int mod = 10007 ; int k ...
- bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)
黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...
- 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是第二类斯特林 ...
- 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是第二类斯特林 ...
- 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)
[HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...
- [总结]数论和组合计数类数学相关(定理&证明&板子)
0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...
- 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)
[BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...
- 【BZOJ5305】[HAOI2018]苹果树(组合计数)
[BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...
- 【BZOJ3142】[HNOI2013]数列(组合计数)
[BZOJ3142][HNOI2013]数列(组合计数) 题面 BZOJ 洛谷 题解 唯一考虑的就是把一段值给分配给\(k-1\)天,假设这\(k-1\)天分配好了,第\(i\)天是\(a_i\),假 ...
随机推荐
- 前端之HEML
HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen ...
- Entity Framework 的懒加载、预先加载、显示加载
1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...
- Super超级ERP系统---(5)采购管理--采购入库
采购商品完成后,下一步要进行入库操作.为了做到精细化管理,入库操作主要分以下几个步骤,采购到货确认,采购入库,入库完成.接下来我们看看这些步骤是怎样实现的. 1.到货确认 采购商品到达仓库后,仓库收货 ...
- Dalvik虚拟机和JVM的对比
Dalvik虚拟机与Java虚拟机有着很多相似的特性,都支持GC,JIT,JNI等等.其主要区别在于文件格式以及指令集不同,下面对两者的特性进行比较与讨论. Difference1:文件格式 Dalv ...
- 资源帖:CV代码库搜集
2013计算机视觉代码合集一: 原文链接:http://www.yuanyong.org/blog/cv/cv-code-one 切记:一定要看原文链接 原文链接: http://blog.csdn. ...
- Java中数组的概念与特点
数组概念: 数组其实也是一个容器,可以用来存储固定个数相同类型的数据数组的定义 数组中存储的数据叫做元素 特点: 1.数组是引用数据类型 2.数组的长度是固定的,也就是说可以存储固定个数的数据 3.数 ...
- css 里面怎么改链接颜色
a.color1:link{color: #FFFFFF ; text-decoration:none;} /*常规时候的样式*/a.color1:visited{color: #FFFFFF; te ...
- PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB
转自:http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/ ...
- [luogu2052 NOI2011] 道路修建 (树形dp)
传送门 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 ...
- PuTTY_0.67.0.0工具链接linux
1.虚拟机设置 在网络适配器中选中桥接模式,勾选复制物理网络链接状态(p)选项.点击确认. 2.开启虚拟机,检查是否安装有ssh服务器 a.查看是否启动ssh服务器 ps -a | grep ssh ...