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}$即可 代码 # ...
随机推荐
- 【代码笔记】Web-ionic-颜色
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Power BI 与 Azure Analysis Services 的数据关联:2、Azure Analysis Services与 本地版本的 SQL Analysis Services 连接
Power BI 与 Azure Analysis Services 的数据关联:2.Azure Analysis Services与 本地版本的 SQL Analysis Services ...
- python自动化开发-5a
python的常用模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包 ...
- RxJava2.0的使用详解
RxJava2.0的使用详解 1,初识RxJava RxJava就是一种用Java语言实现的响应式编程,来创建基于事件的异步程序 RxJava是一个基于事件订阅的异步执行的一个类库,目前比较火的一些技 ...
- Android解析XML文件
XML文件和获取XML值 XML文件样例 <?xml version="1.0" encoding="utf-8"?> <citys> ...
- Flume组件汇总2
Component Interface Type Alias Implementation Class org.apache.flume.Channel memory org.apache.flume ...
- 12.2、多线程通信:queue
queue: 什么是队列:是一种特殊的结构,类似于列表.不过就像排队一样,队列中的元素一旦取出,那么就会从队列中删除. 线程之间的通信可以使用队列queue来进行 线程如何使用queue.Queue[ ...
- VMware虚拟机安装教程详解图文
学习Linux系统最好的方式就是在自己的虚拟机上安装Linux:接下来就给大家简单介绍一下VMware虚拟机的安装以及Linux的安装:VMware虚拟机只是为了更好的学习Linux: ...
- 分享一下我研究SQLSERVER以来收集的笔记
分享一下我研究SQLSERVER以来收集的笔记 前言 为什麽分享??因为像现在网上很多人攻城师那样,转行去卖水果,卖早餐,总有一日我也会离开这个行业的 由于本人不是在大公司上班工资很低,我希望有一天存 ...
- datagridview 行高列宽的自动设置
1) 设定行高和列宽自动调整 [C#]// 设定包括Header和所有单元格的列宽自动调整 DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSi ...