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初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上 ...
随机推荐
- POJ 3263 Tallest Cow 题解
题目 FJ's \(N (1 ≤ N ≤ 10,000)\) cows conveniently indexed 1..N are standing in a line. Each cow has a ...
- mysql数据库 创建、查看、重命名、复制和删除的基本操作
在数据库中,表是最重要.最基本的对象,是存储数据的基本单位.数据表从哪里来呢?数据表由关系模式转换而来.但不是简单的转换. 在设计表结构时要考虑下面几个方面: 字段名要通俗易懂且具有代表性,字段名不允 ...
- wsl2 ubuntu20.04 上使用 kubeadm 创建一个单主集群
wsl2 ubuntu20.04 上使用 kubeadm 创建一个单主集群 官方文档使用 kubeadm 创建一个单主集群 环境初始化 建议尽可能初始化环境,命令wsl --unregister Ub ...
- meta viewport相关
<!DOCTYPE html> H5标准声明,使用 HTML5 doctype,不区分大小写 <head lang=”en”> 标准的 lang 属性写法 <meta c ...
- 数据可视化基础专题(三):Pandas基础(二) csv导入与导出
1.csv导入 1.1 csv导入 .read_csv()函数 pandas.read_csv(filepath_or_buffer: Union[str, pathlib.Path, IO[~Any ...
- 李航统计学习方法(第二版)(六):k 近邻算法实现(kd树(kd tree)方法)
1. kd树简介 构造kd树的方法如下:构造根结点,使根结点对应于k维空间中包含所有实例点的超矩形区域;通过下面的递归方法,不断地对k维空间进行切分,生成子结点.在超矩形区域(结点)上选择一个坐标轴和 ...
- Viper解析&加载配置
Viper解析&加载配置 1 Viper是什么 Viper是一个方便Go语言应用程序处理配置信息的库.它可以处理多种格式的配置.它支持的特性: 设置默认值 从JSON.TOML.YAML ...
- Ethical Hacking - NETWORK PENETRATION TESTING(12)
Post Connection Attacks Sophisticated attacks that can be used after connecting to the target AP. Ga ...
- “git pull” 强制覆盖本地文件
放弃本地修改,使用服务器代码覆盖本地的Git命令如下: $ git fetch --all $ git reset --hard origin/master $ git pull 使用master分支 ...
- patelinux 安装
参考文档:https://china.xilinx.com/support/documentation/sw_manuals/xilinx2017_2/ug1144-petalinux-tools-r ...