【题解】国家集训队礼物(Lucas定理)
[国家集训队]礼物(扩展Lucas定理)
传送门可以直接戳标题
172.40.23.20 24 .1
答案就是一个式子:
\]
解释一下这个式子怎么来的...
- 先从所有的礼物里面选出\(\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定理)的更多相关文章
- 【LG2183】[国家集训队]礼物
[LG2183][国家集训队]礼物 题面 洛谷 题解 插曲:不知道为什么,一看到这个题目,我就想到了这个人... 如果不是有\(exLucas\),这题就是\(sb\)题... 首先,若\(\sum_ ...
- 题解-[国家集训队]Crash的数字表格 / JZPTAB
题解-[国家集训队]Crash的数字表格 / JZPTAB 前置知识: 莫比乌斯反演 </> [国家集训队]Crash的数字表格 / JZPTAB 单组测试数据,给定 \(n,m\) ,求 ...
- BZOJ 2142: 礼物 [Lucas定理]
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1294 Solved: 534[Submit][Status][Discuss] ...
- luogu P2183 [国家集训队]礼物
LINK:礼物 n个物品 m个人 每个人要分得wi 个物品 每个物品互异 分给每个人的物品不分顺序 求方案数. \(n,p\leq 1e9 m\leq 5\) 方案数 那显然是 第一个人拿了w1件物品 ...
- 洛谷 P2183 [国家集训队]礼物
题目描述 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店中购买了n件礼物 ...
- 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 心中分量越重的人,收到的礼物会 ...
- [学习笔记]扩展LUCAS定理
可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...
- Lucas定理和扩展Lucas定理
1.Lucas定理 首先给出式子:\(C_n^m\%p = C_{\lfloor\frac{n}{p}\rfloor}^{\lfloor\frac{m}{p}\rfloor} * C_{n\%p}^{ ...
随机推荐
- NetBean 远程开发的好文1 --> NetBeans的远程Linux C开发实践
from: http://blog.csdn.net/jacktan/article/details/9268535 一直以来总觉得NetBeans生活在Eclipse的阴影下,同样做为一款不错的基 ...
- [GraphQL] Query a GraphQL API with graphql-request
To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation ...
- 【Scala-ML】怎样利用Scala构建并行机器学习系统
引言 在学习Scala的过程中,我发现其在构建大规模分布式计算系统上有与生俱来的特质. 其丰富的类型系统能够帮助编程设计提供非常好的信息隐藏和抽象,其monoids和monads概念利用Scala高阶 ...
- javascript - 封装ajax
封装,使用有示例. // 封装示例: function ajax(url, method, params, done) { var xhr = null; method = method.toUppe ...
- Vue-router(vue2.0)用法示例
一.新建3个组件 1./src/components/post.vue <template> <div> hello world! this is POST! </div ...
- Mybatis 存在多个日志时设置日志
mybatis默认使用log4j,当有self4j这个日志jar包存在时会无法打印sql,请移除或者在工程启动时显示设置mybatis使用的日志类 log4j.logger.org.apache.ib ...
- vim命令行模式
1. 激活命令行模式 : 进入命令行模式 <Esc> 退出命令行模式 2. 常用命令 :p 打印 (:print) :e 读入文件 (:edit) :w 写入文件 (:write) :t ...
- css 猫头鹰选择器
除了第一个以外的兄弟选择器.由于看起来像猫头鹰,也叫猫头鹰选择器 .TD-Breadcrumb > li + li:before { padding: 0 5px; color: #ccc; c ...
- Spring事务管理简介
© 版权声明:本文为博主原创文章,转载请注明出处 1.什么是事务 - 事务是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败 2.事务特性(ACID) - 1.原子性(Atomicity):指事 ...
- Redis用LPUSH和RPOP实现消息队列
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceS ...