[国家集训队]礼物(扩展Lucas定理)

传送门可以直接戳标题

172.40.23.20 24 .1

答案就是一个式子:

\[{n\choose \Sigma_{i=1}^m w}\times\prod_{i=1}^m {\Sigma_{j=1}^m w_j-\Sigma_{j=1}^{j< i}w_j\choose w_i}
\]

解释一下这个式子怎么来的...

  • 先从所有的礼物里面选出\(\Sigma w\)出来
  • 每个人依次选,选择的方案就是从剩下的礼物中挑出\(w_i\)个

直接扩展Lucas...

#include<bits/stdc++.h>
#define int long long
using namespace std;typedef long long ll;
template < class ccf >
inline ccf qr(ccf b){
register char c=getchar();register int q=1;register ccf x=0;
while(c<48||c>57)q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)x=x*10+c-48,c=getchar();
return q==-1?-x:x;}
inline int qr(){return qr(1);} const int maxn=51;
int n,mod,m;
int w[maxn]; namespace lcs{
inline ll poww(ll a,ll b,ll mod){
ll base=a,ans=1;
while(b){
if(b&1) ans=(1ll*ans*base)%mod;
base=(1ll*base*base)%mod;
b>>=1;
}
return 1ll*ans;
} inline void Exgcd(ll a,ll b,ll &x,ll &y){
if(!b){x=1,y=0;return ;}
Exgcd(b,a%b,y,x);y-=a/b*x;
} inline ll rev(ll k,ll p){
if(!k)return 0;
ll x=0,y=0,a=k,b=p;
Exgcd(a,b,x,y);
x=(x%b+b)%b;
if(!x)x+=b;
return 1ll*x;
}
inline ll mul(ll n,ll p,ll pk){
if(!n)return 1;
ll ans=1;
for(ll i=2;i<=pk;i++)
if(i%p)ans=ans*i%pk;
ans=poww(ans,n/pk,pk);
for(ll i=2;i<=n%pk;i++)
if(i%p)ans=ans*i%pk;
return 1ll*ans*mul(n/p,p,pk)%pk;
} inline ll C(ll n,ll m,ll mod,ll p,ll pk){
if(m>n)return 0;
ll a=mul(n,p,pk),b=mul(m,p,pk),c=mul(n-m,p,pk),k=0; for(ll i=n;i;i/=p)k+=i/p;
for(ll i=m;i;i/=p)k-=i/p;
for(ll i=n-m;i;i/=p)k-=i/p; ll ans=1ll*a*rev(b,pk)%pk*rev(c,pk)%pk*poww(p,k,pk)%pk; return 1ll*ans*(mod/pk)%mod*rev(mod/pk,pk)%mod;
}
inline ll exlucas(int n,int m){
int mod=::mod;
int ret=0;
for(register int t=2;t*t<=mod;++t)
if(mod%t==0){
register int temp=1;
while(mod%t==0) temp*=t,mod/=t;
ret=(ret+C(n,m,::mod,t,temp))%(::mod);
}
if(mod>1) ret=(ret+C(n,m,::mod,mod,mod))%(::mod);
return ret;
} } using lcs::exlucas;
#undef int
int main(){
#define int long long
// freopen("gift.in","r",stdin);
// freopen("gift.out","w",stdout);
mod=qr();
n=qr();m=qr();
for(register int t=1;t<=m;++t)
w[t]=qr(),w[0]+=w[t];
if(w[0]>n) return puts("Impossible"),0;
ll ans=exlucas(n,w[0]);
for(register int t=1;t<=m;++t)
ans=ans*exlucas(w[0],w[t])%mod,w[0]-=w[t];
printf("%lld\n",(ll)ans);
return 0;
}

【题解】国家集训队礼物(Lucas定理)的更多相关文章

  1. 【LG2183】[国家集训队]礼物

    [LG2183][国家集训队]礼物 题面 洛谷 题解 插曲:不知道为什么,一看到这个题目,我就想到了这个人... 如果不是有\(exLucas\),这题就是\(sb\)题... 首先,若\(\sum_ ...

  2. 题解-[国家集训队]Crash的数字表格 / JZPTAB

    题解-[国家集训队]Crash的数字表格 / JZPTAB 前置知识: 莫比乌斯反演 </> [国家集训队]Crash的数字表格 / JZPTAB 单组测试数据,给定 \(n,m\) ,求 ...

  3. BZOJ 2142: 礼物 [Lucas定理]

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

  4. luogu P2183 [国家集训队]礼物

    LINK:礼物 n个物品 m个人 每个人要分得wi 个物品 每个物品互异 分给每个人的物品不分顺序 求方案数. \(n,p\leq 1e9 m\leq 5\) 方案数 那显然是 第一个人拿了w1件物品 ...

  5. 洛谷 P2183 [国家集训队]礼物

    题目描述 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店中购买了n件礼物 ...

  6. 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 ...

  7. P2183 [国家集训队]【一本通提高组合数学】礼物

    [国家集训队]礼物 题目背景 一年一度的圣诞节快要来到了.每年的圣诞节小 E 都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小 E 心目中的重要性不同,在小 E 心中分量越重的人,收到的礼物会 ...

  8. [学习笔记]扩展LUCAS定理

    可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...

  9. Lucas定理和扩展Lucas定理

    1.Lucas定理 首先给出式子:\(C_n^m\%p = C_{\lfloor\frac{n}{p}\rfloor}^{\lfloor\frac{m}{p}\rfloor} * C_{n\%p}^{ ...

随机推荐

  1. 2017.7.12 IDEA热部署(更新jsp或java代码不用重启tomcat即可即时生效)

    选择war explored. 主要在于 On frame deactivation选项配置选择为 Update classes and resourses(当且仅当在Deployment配置页,对应 ...

  2. [PWA] Add Push Notifications to a PWA with React in Chrome and on Android

    On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...

  3. 网页计算器 && 简易网页时钟 && 倒计时时钟

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Win7旗舰版+IIS7没有错误提示怎么办

    在IIS Manger中将ASP的调试属性修改默认值,启用服务端调试和客户端调试都改为True,重启后生效.

  5. 百度兴趣点下载V1.4版

    好东西就是不断的更改,升级出来的,经过连续四个版本的升级,本次版本受到用户的一致好评,也拿来在这里做个分享,期望对更多地用户起到一定的帮助! 还是一样,介绍一下其功能: 1.全国范围内的兴趣点任意区域 ...

  6. Android SDK安装时出错“android Failed to rename directory”的解决方法

    Android SDK安装时出错"android Failed to rename directory"的解决的方法     安装Android SDK时遇到Failed to r ...

  7. Linux 下Office 软件名称

    Linux 下Office 软件名称

  8. 【HTML5】元素<head>的使用

    功能描述 在新建的页面<head>元素中,加入该元素所包含的各类标签,并定义超级链接的样式.当单击"请点击我"标签时,并展示相应效果并进入<base>元素设 ...

  9. MySQL查询缓存设置提高MySQL查询性能

    首先看看MSYQL逻辑框架:图片来自高性能mysql 如果使用了QueryCache,当查询接收到一个和之前同样的查询,服务器将会从查询缓存中检索结果,而不是再次分析和执行相同的查询.这样就能大大提高 ...

  10. Java GUI编程SwingUtilities.invokeLater作用

    1 http://blog.micxp.com/index.php/archives/109/ 2