传送门


证明自己学过exLucas

这题计算的是本质不相同的排列数量,不难得到答案是\(\frac{n!}{\prod\limits_{i=1}^m w_i! \times (n - \sum\limits_{i=1}^m w_i)!}\)

但是模数不一定是质数,于是用exLucas计算即可。

  1. #include<bits/stdc++.h>
  2. #define int long long
  3. //This code is written by Itst
  4. using namespace std;
  5. int peo[7] , ans[10][2];
  6. int cnt , N , M , P;
  7. inline int poww(int a , int b , int mod = 1e15){
  8. int times = 1;
  9. while(b){
  10. if(b & 1)
  11. times = times * a % mod;
  12. a = a * a % mod;
  13. b >>= 1;
  14. }
  15. return times;
  16. }
  17. void exgcd(int a , int b , int &x , int &y){
  18. !b ? (x = 1 , y = 0) : (exgcd(b , a % b , y , x) , y -= a / b * x);
  19. }
  20. inline int inv(int a , int b){
  21. a %= b;
  22. int x , y;
  23. exgcd(a , b , x , y);
  24. return (x + b) % b;
  25. }
  26. int jc(int N , int p , int mod){
  27. if(!N)
  28. return 1;
  29. int times = 1;
  30. for(int j = 1 ; j < mod ; ++j)
  31. if(j % p)
  32. times = times * j % mod;
  33. times = poww(times , N / mod , mod);
  34. for(int j = N / mod * mod + 1 ; j <= N ; ++j)
  35. if(j % p)
  36. times = times * j % mod;
  37. return times * jc(N / p , p , mod) % mod;
  38. }
  39. int cntp(int N , int p){
  40. int sum = 0;
  41. while(N >= p)
  42. sum += (N /= p);
  43. return sum;
  44. }
  45. void calc(int p , int k){
  46. int mod = poww(p , k) , c = cntp(N , p);
  47. for(int j = 1 ; j <= M ; ++j)
  48. c -= cntp(peo[j] , p);
  49. ans[++cnt][0] = mod;
  50. if(c < k){
  51. ans[cnt][1] = poww(p , c) * jc(N , p , mod) % mod;
  52. for(int j = 1 ; j <= M ; ++j)
  53. ans[cnt][1] = ans[cnt][1] * inv(jc(peo[j] , p , mod) , mod) % mod;
  54. }
  55. }
  56. signed main(){
  57. #ifndef ONLINE_JUDGE
  58. //freopen("in" , "r" , stdin);
  59. //freopen("out" , "w" , stdout);
  60. #endif
  61. cin >> P >> N >> M;
  62. for(int i = 1 ; i <= M ; ++i){
  63. cin >> peo[i];
  64. peo[M + 1] += peo[i];
  65. }
  66. if(peo[M + 1] > N){
  67. puts("Impossible");
  68. return 0;
  69. }
  70. peo[M + 1] = N - peo[M + 1];
  71. ++M;
  72. int tmp = P;
  73. for(int i = 2 ; i * i <= tmp ; ++i)
  74. if(tmp % i == 0){
  75. int cnt = 0;
  76. while(tmp % i == 0){
  77. ++cnt;
  78. tmp /= i;
  79. }
  80. calc(i , cnt);
  81. }
  82. if(tmp - 1)
  83. calc(tmp , 1);
  84. int sum = 0;
  85. for(int i = 1 ; i <= cnt ; ++i)
  86. sum = (sum + ans[i][1] * (P / ans[i][0]) % P * inv(P / ans[i][0] , ans[i][0])) % P;
  87. cout << sum;
  88. return 0;
  89. }

Luogu2183 礼物 ExLucas、CRT的更多相关文章

  1. BZOJ 2142 礼物 组合数学 CRT 中国剩余定理

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1450  Solved: 593[Submit][Status][Discuss] ...

  2. 【BZOJ2142】礼物 组合数+CRT

    [BZOJ2142]礼物 Description 小E从商店中购买了n件礼物,打算送给m个人,其中送给第i个人礼物数量为wi.请你帮忙计算出送礼物的方案数(两个方案被认为是不同的,当且仅当存在某个人在 ...

  3. [CSP-S模拟测试]:visit(组合数学+exLucas+CRT)

    题目传送门(内部题6) 输入格式 第一行包含两个整数$T$,$MOD$:第二行包含两个整数$n$,$m$,表示$dirty$房子的位置. 输出格式 一行一个整数,表示对$MOD$取模之后的答案. 样例 ...

  4. 【Foreign】无聊的计算姬 [Lucas][BSGS]

    无聊的计算姬 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 6 2 2 3 4 3 2 ...

  5. BZOJ_2142_礼物_扩展lucas+组合数取模+CRT

    BZOJ_2142_礼物_扩展lucas+组合数取模 Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同 ...

  6. CRT and exlucas

    CRT 解同余方程,形如\(x \equiv c_i \ mod \ m_i\),我们对每个方程构造一个解满足: 对于第\(i\)个方程:\(x \equiv 1 \ mod \ m_i\),\(x ...

  7. Algorithm: CRT、EX-CRT & Lucas、Ex-Lucas

    中国剩余定理 中国剩余定理,Chinese Remainder Theorem,又称孙子定理,给出了一元线性同余方程组的有解判定条件,并用构造法给出了通解的具体形式. \[ \begin{aligne ...

  8. 4.18 省选模拟赛 无聊的计算器 CRT EXBSGS EXLucas

    算是一道很毒瘤的题目 考试的时候码+调了3h才搞定. op==1 显然是快速幂. op==2 有些点可以使用BSGS 不过后面的点是EXBSGS. 这个以前学过了 考试的时候还是懵逼.(当时还是看着花 ...

  9. Luogu2183【国家集训队】礼物

    题面 题解 易得答案为 $$ \sum_{i=1}^m\binom{n-\sum_{j=1}^{i-1}w_j}{\sum_{j=1}^iw_j} $$ 扩展$\text{Lucas}$即可 代码 # ...

随机推荐

  1. github版本控制相关

    Git版本控制: 安装Github http://blog.csdn.net/huangyuan_xuan/article/details/49125597 Git本地版本控制 http://blog ...

  2. JS获取当前星期几的简易写法

    var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay()); mark在此,方便日后复制 原文https ...

  3. macOS 下NFS 文件系统挂载

    主要有两种方式: 使用:resvport选项, mount 挂载命令时. 使用:insecure选项, exportfs 文件配置时. sudo mount -o resvport IP:Addr b ...

  4. c# 遍历类中的方法名称

    //Assembly.Load("namespace").GetType("namespace.class名称"); var t = Assembly.Load ...

  5. RD340服务器安装windows2003系统

    RD340服务器安装windows2003系统云修网

  6. linux之在当前目录下按照文件大小进行排序的三种方法

    当前目录下按照文件大小排序 [root@test23 script]# ls -lSh 总用量 44K -rw-r--r-- 1 root root 2.4K 12月 8 17:24 test.con ...

  7. 解决pycharm下代码报错的问题

    file-----settings------project:Pythonstudy------project structure-----选择要导入的文件并点击OK

  8. Linux 小知识翻译 - 「虚拟化技术」

    这次聊聊「虚拟化技术」. 虚拟化技术,有时简称为「虚拟化」,最近经常听人说它.但是却不太清楚它的意思.到底虚拟了什么东西?本来是用来干什么的? 有名的虚拟化软件要数 VMware 和 VirtualB ...

  9. visual studio code前端插件及常用快捷键【转】

    通用插件 HTML Snippets 超级实用且初级的 H5代码片段以及提示 HTML CSS Support 让 html 标签上写class 智能提示当前项目所支持的样式新版已经支持scss文件检 ...

  10. luogu P3293 [SCOI2016]美味

    题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...