luogu P2183 [国家集训队]礼物
LINK:礼物
n个物品 m个人 每个人要分得wi 个物品 每个物品互异 分给每个人的物品不分顺序 求方案数。
\(n,p\leq 1e9 m\leq 5\)
方案数 那显然是 第一个人拿了w1件物品 方案为组合数 第二个人在第一个人之后拿 由于礼物不分顺序 所以这么做是正确的。
方案数显然为乘法原理 组合数 是一个1e9的 模数也是1e9的 卢卡斯定理肯定不行。
上扩展卢卡斯 考虑质因数分解p 最后采用中国剩余定理合并即可。
这个模型出过好多遍了 一定要会写。
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<string>
#include<ctime>
#include<cctype>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<vector>
#include<deque>
#include<list>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#include<iomanip>
#define ll long long
#define db double
#define INF 1000000000
#define ld long double
#define pb push_back
#define get(x) x=read()
#define putl(x) printf("%lld\n",x)
#define gt(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define rep(p,n,i) for(RE ll i=p;i<=n;++i)
#define go(x) for(ll i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define pii pair<ll,ll>
#define mk make_pair
#define RE register
#define ull unsigned long long
#define ui unsigned ll
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline ll read()
{
RE ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const ll MAXN=20,maxn=200010;
ll p,n,m,top;
ll a[MAXN],sum,ans=1;
ll w[MAXN],v[MAXN],x,y;
ll ans1[MAXN],f[maxn];
inline ll fj(ll x,ll xx,ll xxx,ll pp)
{
ll cnt=0;
ll ww=pp;
while(ww<=x)
{
cnt+=x/ww;
cnt-=xx/ww;
cnt-=xxx/ww;
ww=ww*pp;
}
return cnt;
}
inline ll ksm(ll b,ll p,ll mod)
{
ll cnt=1;
while(p)
{
if(p&1)cnt=cnt*b%mod;
b=b*b%mod;p=p>>1;
}
return cnt;
}
inline ll jc(ll x,ll pp,ll mod)
{
if(x<=pp)return f[x];
ll ans=1;
ll w=x/mod;
ans=ksm(f[mod],w,mod);
ans=ans*f[x%mod]%mod;
return ans*jc(x/pp,pp,mod)%mod;
}
inline void exgcd(ll a,ll b)
{
if(!b){x=1;y=0;return;}
exgcd(b,a%b);
ll z=x;x=y;y=z-a/b*y;
}
inline ll inv(ll w,ll mod)//x关于mod的逆元
{
exgcd(w,mod);
return (x%mod+mod)%mod;
}
inline ll solve(ll a,ll b,ll pp,ll mod)
{
ll k1=fj(a,b,a-b,pp);f[0]=1;
rep(1,mod,i)if(i%pp)f[i]=f[i-1]*i%mod;
else f[i]=f[i-1];
ll ans1,ans2,ans3;
ans1=jc(a,pp,mod);
ans2=jc(b,pp,mod);
ans3=jc(a-b,pp,mod);
return ans1*inv(ans2,mod)%mod*inv(ans3,mod)%mod*ksm(pp,k1,mod)%mod;
}
inline ll C(ll a,ll b)
{
rep(1,top,i)ans1[i]=solve(a,b,w[i],v[i]);
ll cc=0;
rep(1,top,i)
{
ll M=p/v[i];
ll ww=inv(M,v[i]);
cc=(cc+M*ww%p*ans1[i]%p)%p;
}
return cc;
}
signed main()
{
freopen("1.in","r",stdin);
get(p);get(n);get(m);
rep(1,m,i)get(a[i]),sum+=a[i];
if(sum>n){puts("Impossible");return 0;}
ll c=p;
for(ll i=2;i*i<=c;++i)
if(c%i==0)
{
w[++top]=i;v[top]=1;
while(c%i==0)c/=i,v[top]=v[top]*i;
}
if(c>1){w[++top]=c;v[top]=c;}
ll res=n;
rep(1,m,i)
{
ans=ans*C(res,a[i])%p;
res-=a[i];
}
putl(ans);
return 0;
}
注意有两点 一是 求阶乘的时候要记得提前预处理 这样可以加速 保证复杂度稳稳log^2 而是最后中国剩余定理合并也其实是求逆。
luogu P2183 [国家集训队]礼物的更多相关文章
- Luogu P2183 [国家集训队]礼物 扩展卢卡斯+组合数
好吧学长说是板子...学了之后才发现就是板子qwq 题意:求$ C_n^{w_1}*C_{n-w_1}^{w_2}*C_{n-w_1-w_2}^{w_3}*...\space mod \space P ...
- 洛谷 P2183 [国家集训队]礼物
题目描述 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店中购买了n件礼物 ...
- P2183 [国家集训队]【一本通提高组合数学】礼物
[国家集训队]礼物 题目背景 一年一度的圣诞节快要来到了.每年的圣诞节小 E 都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小 E 心目中的重要性不同,在小 E 心中分量越重的人,收到的礼物会 ...
- luogu P2757 [国家集训队]等差子序列
题目链接 luogu P2757 [国家集训队]等差子序列 题解 线段树好题 我选择暴力 代码 // luogu-judger-enable-o2 #include<cstdio> inl ...
- 【LG2183】[国家集训队]礼物
[LG2183][国家集训队]礼物 题面 洛谷 题解 插曲:不知道为什么,一看到这个题目,我就想到了这个人... 如果不是有\(exLucas\),这题就是\(sb\)题... 首先,若\(\sum_ ...
- luogu P2619 [国家集训队2]Tree I
题目链接 luogu P2619 [国家集训队2]Tree I 题解 普通思路就不说了二分增量,生成树check 说一下坑点 二分时,若黑白边权有相同,因为权值相同优先选白边,若在最有增量时出现黑白等 ...
- 【题解】国家集训队礼物(Lucas定理)
[国家集训队]礼物(扩展Lucas定理) 传送门可以直接戳标题 172.40.23.20 24 .1 答案就是一个式子: \[ {n\choose \Sigma_{i=1}^m w}\times\pr ...
- [Luogu P1829] [国家集训队]Crash的数字表格 / JZPTAB (莫比乌斯反演)
题面 传送门:洛咕 Solution 调到自闭,我好菜啊 为了方便讨论,以下式子\(m>=n\) 为了方便书写,以下式子中的除号均为向下取整 我们来颓柿子吧qwq 显然,题目让我们求: \(\l ...
- Luogu P1297 [国家集训队]单选错位
P1297 [国家集训队]单选错位 题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上 ...
随机推荐
- C#读取Excel转为DataTable
需要的Dll: NPOI.OOXML.dll https://files.cnblogs.com/files/CityOfThousandFires/NPOI.dl.rar /// <su ...
- 输入Javac提示不是内部或外部命令怎么办
首先,我们在电脑上面找到此电脑, 然后右键点击,选择属性. 在属性中,我们找到高级系统设置,点击打开,如图示. 然后在系统设置中,我们可以找到启动和鼓掌恢复,然后点击环境变量,点击打开. ...
- 机器学习实战基础(十九):sklearn中数据集
sklearn提供的自带的数据集 sklearn 的数据集有好多个种 自带的小数据集(packaged dataset):sklearn.datasets.load_<name> 可在 ...
- 数据可视化之分析篇(十)Power BI应用:如何计算在职员工数量?
https://zhuanlan.zhihu.com/p/128652582 经常碰到的一类问题是,如何根据起止日期来计算某个时间点的数量,比如: 已知合同的生效日期和到期日期,特定日期的有效合同有 ...
- np.nan is an invalid document, expected byte or unicode string.
ValueError Traceback (most recent call last) <ipython-input-12-1dc462ae8893> in <module> ...
- HotSpot VM运行时
HotSpot VM运行时系统为HotSpot JIT编译器和垃圾收集器提供服务和通用API,同时还为VM提供启动.线程管理.JNI(Java本地接口)等基本功能.HotSpot VM运行时环境担当许 ...
- Crawlab Lite 正式发布,更轻量的爬虫管理平台
Crawlab 是一款基于 Golang 的分布式爬虫管理平台,产品发布已经一年有余,经过开发团队的不断打磨,即将迭代到 v0.5 版本.在这期间我们为 Crawlab 加入了大量社区用户共同期望的功 ...
- OSCP Learning Notes - Capstone(4)
SickOS 1.2 Walkthrough Preparation: Down load the SickOS virtual machines from the following website ...
- Ethical Hacking - NETWORK PENETRATION TESTING(1)
Pre--Connection-Attacks that can be done before connecting to the network. Gaining Access - How to b ...
- 洛谷 P5350 序列 珂朵莉树
题目描述 分析 操作一.二.三为珂朵莉树的基本操作,操作四.五.六稍作转化即可 不会珂朵莉树请移步至这里 求和操作 把每一段区间分别取出,暴力相加 ll qh(ll l,ll r){ it2=Spli ...