组合计算的性质:

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,用下逆元

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<cmath>
  7. using namespace std;
  8. typedef long long LL;
  9. const LL mod=;
  10.  
  11. LL quick_pow(LL A,LL p)
  12. {
  13. int ret=;
  14. while(p!=)
  15. {
  16. if(p%==)ret=(ret*A)%mod;
  17. A=(A*A)%mod;p/=;
  18. }
  19. return ret;
  20. }
  21. LL jiecheng(LL k)
  22. {
  23. LL ret=;
  24. for(int i=;i<=k;i++)ret=(ret*i)%mod;
  25. return ret;
  26. }
  27. LL getniyuan(LL k)
  28. {
  29. return quick_pow(k,mod-);
  30. }
  31.  
  32. int main()
  33. {
  34. LL a,b,k,n,m;
  35. scanf("%lld%lld%lld%lld%lld",&a,&b,&k,&n,&m);
  36. LL ans=( (jiecheng(k)*getniyuan( jiecheng(n)*jiecheng(k-n)%mod )%mod) *
  37. (quick_pow(a,n)*quick_pow(b,m)%mod) )%mod;
  38. printf("%lld\n",ans);
  39. return ;
  40. }

计算系数

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

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<cmath>
  7. using namespace std;
  8. typedef long long LL;
  9. const LL mod=1e9+;
  10.  
  11. LL quick_pow(LL A,LL p)
  12. {
  13. int ret=;
  14. while(p!=)
  15. {
  16. if(p%==)ret=(ret*A)%mod;
  17. A=(A*A)%mod;p/=;
  18. }
  19. return ret;
  20. }
  21. LL jiecheng(LL k)
  22. {
  23. LL ret=;
  24. for(int i=;i<=k;i++)ret=(ret*i)%mod;
  25. return ret;
  26. }
  27. LL getniyuan(LL k)
  28. {
  29. return quick_pow(k,mod-);
  30. }
  31.  
  32. int nxt[];
  33. bool v[];
  34. LL dfs(int x,int d)
  35. {
  36. if(v[x]==true)return d;
  37. v[x]=true;
  38. dfs(nxt[x],d+);
  39. }
  40.  
  41. int main()
  42. {
  43. int T;
  44. scanf("%d",&T);
  45. while(T--)
  46. {
  47. int n;
  48. scanf("%d",&n);
  49. for(int i=;i<=n;i++)scanf("%d",&nxt[i]);
  50.  
  51. LL tot=,ans=,cnt;cnt=n;
  52. memset(v,false,sizeof(v));
  53. for(int i=;i<=n;i++)
  54. {
  55. if(v[i]==false)
  56. {
  57. int L=dfs(i,);
  58. if(L>)
  59. {
  60. tot++;
  61. ans=(ans*quick_pow(L,L-)%mod)*getniyuan(jiecheng(L-))%mod;
  62. }
  63. else cnt--;
  64. }
  65. }
  66. if(tot==)printf("1\n");
  67. else
  68. {
  69. ans=ans*jiecheng(cnt-tot)%mod;
  70. printf("%lld\n",ans);
  71. }
  72. }
  73. return ;
  74. }

Count Swaps

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

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

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

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<cmath>
  7. using namespace std;
  8. typedef long long LL;
  9. const LL mod=;
  10. const LL md[]={,,,};
  11.  
  12. int quick_pow(int A,int p,int mod)
  13. {
  14. int ret=;
  15. while(p!=)
  16. {
  17. if(p%==)ret=(LL)ret*A%mod;
  18. A=(LL)A*A%mod;p/=;
  19. }
  20. return ret;
  21. }
  22. LL fac[][],fac_inv[][];
  23. void initC()
  24. {
  25. for(int j=;j<=;j++)fac[j][]=,fac_inv[j][]=;
  26. for(int i=;i<=md[];i++)
  27. for(int j=;j<=;j++)
  28. fac[j][i]=fac[j][i-]*i%md[j],fac_inv[j][i]=quick_pow(fac[j][i],md[j]-,md[j]);
  29. }
  30. 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];}
  31. LL lucas(LL n,LL m,LL p)
  32. {
  33. LL ans=;
  34. while(m>)
  35. {
  36. LL a=n%md[p],b=m%md[p];
  37. if(a<b)return ;
  38. ans=ans*getC(a,b,p)%md[p];
  39. n/=md[p],m/=md[p];
  40. }
  41. return ans;
  42. }
  43.  
  44. LL a[];
  45. LL solve(LL n,LL k)
  46. {
  47. for(int i=;i<=;i++)a[i]=lucas(n,k,i);
  48. LL M=mod-,x=;
  49. for(int i=;i<=;i++)//crt
  50. x=(x+a[i]*(M/md[i])*quick_pow(M/md[i],md[i]-,md[i]))%(mod-);
  51. return x;
  52. }
  53.  
  54. int main()
  55. {
  56. freopen("a.in","r",stdin);
  57. freopen("a.out","w",stdout);
  58. initC();
  59. int n,G,p=;
  60. scanf("%d%d",&n,&G);
  61. if(G==mod){printf("0\n");return ;}
  62. for(int i=;i*i<=n;i++)
  63. if(n%i==)
  64. {
  65. p=(p+solve(n,i))%(mod-);
  66. if(i*i!=n)p=(p+solve(n,n/i))%(mod-);
  67. }
  68. printf("%d\n",quick_pow(G,p,mod));
  69. return ;
  70. }

bzoj1951(upd)

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<cmath>
  7. using namespace std;
  8. typedef long long LL;
  9. const LL mod=;
  10. const LL md[]={,,,};
  11.  
  12. LL exgcd(LL a,LL b,LL &x,LL &y)
  13. {
  14. if(a==)
  15. {
  16. x=;y=;
  17. return b;
  18. }
  19. else
  20. {
  21. LL tx,ty;
  22. LL d=exgcd(b%a,a,tx,ty);
  23. x=ty-b/a*tx;
  24. y=tx;
  25. return d;
  26. }
  27. }
  28. LL quick_pow(LL A,LL p)
  29. {
  30. LL ret=;
  31. while(p!=)
  32. {
  33. if(p%==)ret=(ret*A)%mod;
  34. A=(A*A)%mod;p/=;
  35. }
  36. return ret;
  37. }
  38. LL inv(LL a,LL p)
  39. {
  40. LL x,y;
  41. LL d=exgcd(a,p,x,y);
  42. return (x%p+p)%p;
  43. }
  44.  
  45. //---------------------------------------------------
  46.  
  47. LL jc[];
  48. LL lucas(LL N,LL M,LL p)
  49. {
  50. LL ans=;
  51. while(M>)
  52. {
  53. LL a=N%p,b=M%p;
  54. if(a>b)return ;
  55.  
  56. ans=ans*jc[b]%p;
  57. ans=ans*inv(jc[a],p)%p;
  58. ans=ans*inv(jc[b-a],p)%p;
  59.  
  60. N/=p;M/=p;
  61. }
  62. return ans;
  63. }//lucas
  64. LL g[];
  65. void getzs(LL n)
  66. {
  67. jc[]=;
  68. for(int i=;i<=md[];i++)jc[i]=(jc[i-]*i)%(mod-);
  69.  
  70. for(int i=;i*i<=n;i++)
  71. {
  72. if(n%i==)
  73. {
  74. for(int j=;j<=;j++)
  75. g[j]=(g[j]+lucas(i,n,md[j]))%md[j];
  76. if(i!=n/i)
  77. {
  78. for(int j=;j<=;j++)
  79. g[j]=(g[j]+lucas(n/i,n,md[j]))%md[j];
  80. }
  81. }
  82. }
  83. }
  84.  
  85. //---------------------------------------------------
  86.  
  87. LL u1,v1,u2,v2;
  88. void merge()
  89. {
  90. LL A=u1,B=u2,K=v2-v1,x,y;
  91. LL d=exgcd(A,B,x,y);
  92.  
  93. x=(x*(K/d)%(B/d)+(B/d))%(B/d);
  94. v1=u1*x+v1;
  95. u1=u1/d*u2;
  96. }
  97. LL solve()
  98. {
  99. u1=md[],v1=g[];
  100. for(int i=;i<=;i++)
  101. u2=md[i], v2=g[i], merge();
  102. return v1;
  103. }
  104.  
  105. int main()
  106. {
  107. LL n,g;
  108. scanf("%lld%lld",&n,&g);g%=mod;
  109. if(g==){printf("0\n");return ;}
  110.  
  111. getzs(n);
  112. LL p=solve();
  113. printf("%lld\n",quick_pow(g,p));
  114. return ;
  115. }

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. 前端之HEML

    HTML介绍 Web服务本质   import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen ...

  2. Entity Framework 的懒加载、预先加载、显示加载

    1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...

  3. Super超级ERP系统---(5)采购管理--采购入库

    采购商品完成后,下一步要进行入库操作.为了做到精细化管理,入库操作主要分以下几个步骤,采购到货确认,采购入库,入库完成.接下来我们看看这些步骤是怎样实现的. 1.到货确认 采购商品到达仓库后,仓库收货 ...

  4. Dalvik虚拟机和JVM的对比

    Dalvik虚拟机与Java虚拟机有着很多相似的特性,都支持GC,JIT,JNI等等.其主要区别在于文件格式以及指令集不同,下面对两者的特性进行比较与讨论. Difference1:文件格式 Dalv ...

  5. 资源帖:CV代码库搜集

    2013计算机视觉代码合集一: 原文链接:http://www.yuanyong.org/blog/cv/cv-code-one 切记:一定要看原文链接 原文链接: http://blog.csdn. ...

  6. Java中数组的概念与特点

    数组概念: 数组其实也是一个容器,可以用来存储固定个数相同类型的数据数组的定义 数组中存储的数据叫做元素 特点: 1.数组是引用数据类型 2.数组的长度是固定的,也就是说可以存储固定个数的数据 3.数 ...

  7. css 里面怎么改链接颜色

    a.color1:link{color: #FFFFFF ; text-decoration:none;} /*常规时候的样式*/a.color1:visited{color: #FFFFFF; te ...

  8. PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB

    转自:http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/ ...

  9. [luogu2052 NOI2011] 道路修建 (树形dp)

    传送门 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 ...

  10. PuTTY_0.67.0.0工具链接linux

    1.虚拟机设置 在网络适配器中选中桥接模式,勾选复制物理网络链接状态(p)选项.点击确认. 2.开启虚拟机,检查是否安装有ssh服务器 a.查看是否启动ssh服务器 ps -a | grep ssh ...