Luogu2183 礼物 ExLucas、CRT
证明自己学过exLucas
这题计算的是本质不相同的排列数量,不难得到答案是\(\frac{n!}{\prod\limits_{i=1}^m w_i! \times (n - \sum\limits_{i=1}^m w_i)!}\)
但是模数不一定是质数,于是用exLucas计算即可。
#include<bits/stdc++.h>
#define int long long
//This code is written by Itst
using namespace std;
int peo[7] , ans[10][2];
int cnt , N , M , P;
inline int poww(int a , int b , int mod = 1e15){
int times = 1;
while(b){
if(b & 1)
times = times * a % mod;
a = a * a % mod;
b >>= 1;
}
return times;
}
void exgcd(int a , int b , int &x , int &y){
!b ? (x = 1 , y = 0) : (exgcd(b , a % b , y , x) , y -= a / b * x);
}
inline int inv(int a , int b){
a %= b;
int x , y;
exgcd(a , b , x , y);
return (x + b) % b;
}
int jc(int N , int p , int mod){
if(!N)
return 1;
int times = 1;
for(int j = 1 ; j < mod ; ++j)
if(j % p)
times = times * j % mod;
times = poww(times , N / mod , mod);
for(int j = N / mod * mod + 1 ; j <= N ; ++j)
if(j % p)
times = times * j % mod;
return times * jc(N / p , p , mod) % mod;
}
int cntp(int N , int p){
int sum = 0;
while(N >= p)
sum += (N /= p);
return sum;
}
void calc(int p , int k){
int mod = poww(p , k) , c = cntp(N , p);
for(int j = 1 ; j <= M ; ++j)
c -= cntp(peo[j] , p);
ans[++cnt][0] = mod;
if(c < k){
ans[cnt][1] = poww(p , c) * jc(N , p , mod) % mod;
for(int j = 1 ; j <= M ; ++j)
ans[cnt][1] = ans[cnt][1] * inv(jc(peo[j] , p , mod) , mod) % mod;
}
}
signed main(){
#ifndef ONLINE_JUDGE
//freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
cin >> P >> N >> M;
for(int i = 1 ; i <= M ; ++i){
cin >> peo[i];
peo[M + 1] += peo[i];
}
if(peo[M + 1] > N){
puts("Impossible");
return 0;
}
peo[M + 1] = N - peo[M + 1];
++M;
int tmp = P;
for(int i = 2 ; i * i <= tmp ; ++i)
if(tmp % i == 0){
int cnt = 0;
while(tmp % i == 0){
++cnt;
tmp /= i;
}
calc(i , cnt);
}
if(tmp - 1)
calc(tmp , 1);
int sum = 0;
for(int i = 1 ; i <= cnt ; ++i)
sum = (sum + ans[i][1] * (P / ans[i][0]) % P * inv(P / ans[i][0] , ans[i][0])) % P;
cout << sum;
return 0;
}
Luogu2183 礼物 ExLucas、CRT的更多相关文章
- BZOJ 2142 礼物 组合数学 CRT 中国剩余定理
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1450 Solved: 593[Submit][Status][Discuss] ...
- 【BZOJ2142】礼物 组合数+CRT
[BZOJ2142]礼物 Description 小E从商店中购买了n件礼物,打算送给m个人,其中送给第i个人礼物数量为wi.请你帮忙计算出送礼物的方案数(两个方案被认为是不同的,当且仅当存在某个人在 ...
- [CSP-S模拟测试]:visit(组合数学+exLucas+CRT)
题目传送门(内部题6) 输入格式 第一行包含两个整数$T$,$MOD$:第二行包含两个整数$n$,$m$,表示$dirty$房子的位置. 输出格式 一行一个整数,表示对$MOD$取模之后的答案. 样例 ...
- 【Foreign】无聊的计算姬 [Lucas][BSGS]
无聊的计算姬 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 6 2 2 3 4 3 2 ...
- BZOJ_2142_礼物_扩展lucas+组合数取模+CRT
BZOJ_2142_礼物_扩展lucas+组合数取模 Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同 ...
- CRT and exlucas
CRT 解同余方程,形如\(x \equiv c_i \ mod \ m_i\),我们对每个方程构造一个解满足: 对于第\(i\)个方程:\(x \equiv 1 \ mod \ m_i\),\(x ...
- Algorithm: CRT、EX-CRT & Lucas、Ex-Lucas
中国剩余定理 中国剩余定理,Chinese Remainder Theorem,又称孙子定理,给出了一元线性同余方程组的有解判定条件,并用构造法给出了通解的具体形式. \[ \begin{aligne ...
- 4.18 省选模拟赛 无聊的计算器 CRT EXBSGS EXLucas
算是一道很毒瘤的题目 考试的时候码+调了3h才搞定. op==1 显然是快速幂. op==2 有些点可以使用BSGS 不过后面的点是EXBSGS. 这个以前学过了 考试的时候还是懵逼.(当时还是看着花 ...
- Luogu2183【国家集训队】礼物
题面 题解 易得答案为 $$ \sum_{i=1}^m\binom{n-\sum_{j=1}^{i-1}w_j}{\sum_{j=1}^iw_j} $$ 扩展$\text{Lucas}$即可 代码 # ...
随机推荐
- github版本控制相关
Git版本控制: 安装Github http://blog.csdn.net/huangyuan_xuan/article/details/49125597 Git本地版本控制 http://blog ...
- JS获取当前星期几的简易写法
var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay()); mark在此,方便日后复制 原文https ...
- macOS 下NFS 文件系统挂载
主要有两种方式: 使用:resvport选项, mount 挂载命令时. 使用:insecure选项, exportfs 文件配置时. sudo mount -o resvport IP:Addr b ...
- c# 遍历类中的方法名称
//Assembly.Load("namespace").GetType("namespace.class名称"); var t = Assembly.Load ...
- RD340服务器安装windows2003系统
RD340服务器安装windows2003系统云修网
- linux之在当前目录下按照文件大小进行排序的三种方法
当前目录下按照文件大小排序 [root@test23 script]# ls -lSh 总用量 44K -rw-r--r-- 1 root root 2.4K 12月 8 17:24 test.con ...
- 解决pycharm下代码报错的问题
file-----settings------project:Pythonstudy------project structure-----选择要导入的文件并点击OK
- Linux 小知识翻译 - 「虚拟化技术」
这次聊聊「虚拟化技术」. 虚拟化技术,有时简称为「虚拟化」,最近经常听人说它.但是却不太清楚它的意思.到底虚拟了什么东西?本来是用来干什么的? 有名的虚拟化软件要数 VMware 和 VirtualB ...
- visual studio code前端插件及常用快捷键【转】
通用插件 HTML Snippets 超级实用且初级的 H5代码片段以及提示 HTML CSS Support 让 html 标签上写class 智能提示当前项目所支持的样式新版已经支持scss文件检 ...
- luogu P3293 [SCOI2016]美味
题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...