[国家集训队]礼物(扩展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. Node.js 极简入门Helloworld版服务器例子

    粗浅得很,纯属备忘. // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); ...

  2. 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 24.0怎么办

    24.0 位置偏差过大保护   读取驱动器参数之后,在基本的014项目把设定值设置为最大,然后点击传送,EEP写入驱动器后重启驱动器即可     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空 ...

  3. react-native 常见操作 及 git 补充

    一. react-native 常见操作 1.创建项目 react-native init Market(项目名称,首字母大写) 2.安装常用插件 npm install react-native-t ...

  4. php 记录图片浏览次数次数

    <?php $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; $la ...

  5. Devops成功的八大炫酷工具

    原文链接:http://www.infoworld.com/article/3031009/devops/8-more-cool-tools-for-devops-success.html 为自动化和 ...

  6. 自动make工具--CMake

    http://www.cnblogs.com/lyq105/archive/2010/12/03/1895067.html http://www.linuxidc.com/Linux/2015-10/ ...

  7. tigervnc环境搭建

    在root用户下执行以下操作: 1.安装tigervnc yum install tigervnc tigervnc-server 2.配置tigervnc 编辑vncservers文件,执行如下命令 ...

  8. Dijkstra 算法——计算有权最短路径(边有权值)

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在理解 Dijkstra 的思想并用源代码加以实现: 0.2)最短路径算法的基础知识,参见 http://blog. ...

  9. 趣味编程:C#中Specification模式的实现(参考答案 - 下)

    一篇文章中我们利用C#语言的特性实现了一种轻量级的Specification模式,它的关键在于抛弃了具体的Specification类型,而是使用一个委托对象代替唯一关键的IsSatisfiedBy方 ...

  10. linux卸载一个源码包安装的软件的流程

    完全卸载memcached的方法(CentOS) 我的大内存vps(centos系统)曾经安装过memcached,想给论坛提速,实际上不但没有明显效果,反倒耗费内存,看着碍眼,于是想卸载,于是网上各 ...