Luogu2183【国家集训队】礼物
题面
题解
易得答案为
$$ \sum_{i=1}^m\binom{n-\sum_{j=1}^{i-1}w_j}{\sum_{j=1}^iw_j} $$
扩展$\text{Lucas}$即可
代码
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))
#define int long long
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
inline int fastpow(int x, int y, int Mod)
{
int ans = 1;
while(y)
{
if(y & 1) ans = 1ll * ans * x % Mod;
x = 1ll * x * x % Mod, y >>= 1;
}
return ans;
}
void exgcd(int a, int b, int &d, int &x, int &y)
{
!b ? d = a, x = 1, y = 0 : (exgcd(b, a % b, d, y, x), y -= x * (a / b));
}
inline int Inv(int i, int Mod)
{
if(!i) return 0;
int x, y, d, a = i, b = Mod;
exgcd(a, b, d, x, y);
x = (x % b + b) % b;
if(!x) x += b;
return x;
}
int mul(int n, int p, int k)
{
if(!n) return 1;
int ans = 1;
for(int i = 2; i <= k; i++)
if(i % p) ans = ans * i % k;
ans = fastpow(ans, n / k, k);
for(int i = 2; i <= n % k; i++)
if(i % p) ans = ans * i % k;
return ans * mul(n / p, p, k) % k;
}
inline int C(int n, int m, int Mod, int p, int k)
{
if(m > n) return 0;
int a = mul(n, p, k), b = mul(m, p, k), c = mul(n - m, p, k), _k = 0;
for(int i = n; i; i /= p) _k += i / p;
for(int i = m; i; i /= p) _k -= i / p;
for(int i = n - m; i; i /= p) _k -= i / p;
int ans = a * Inv(b, k) % k * Inv(c, k) % k * fastpow(p, _k, k) % k;
return ans * (Mod / k) % Mod * Inv(Mod / k, k) % Mod;
}
int n, m, sum, Mod, ans = 1, a[20];
signed main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
Mod = read(), n = read(), m = read();
for(signed i = 1; i <= m; i++) sum += (a[i] = read());
if(sum > n) return puts("Impossible") & 0;
for(signed k = 1; k <= m; k++)
{
n -= a[k - 1];
int now = 0, x = Mod;
for(int i = 2; i * i <= Mod; i++)
if(!(x % i))
{
int _k = 1;
while(!(x % i)) _k *= i, x /= i;
now = (now + C(n, a[k], Mod, i, _k)) % Mod;
}
if(x > 1) now = (now + C(n, a[k], Mod, x, x)) % Mod;
ans = ans * now % Mod;
}
printf("%lld\n", ans);
return 0;
}
Luogu2183【国家集训队】礼物的更多相关文章
- 【LG2183】[国家集训队]礼物
[LG2183][国家集训队]礼物 题面 洛谷 题解 插曲:不知道为什么,一看到这个题目,我就想到了这个人... 如果不是有\(exLucas\),这题就是\(sb\)题... 首先,若\(\sum_ ...
- 【题解】国家集训队礼物(Lucas定理)
[国家集训队]礼物(扩展Lucas定理) 传送门可以直接戳标题 172.40.23.20 24 .1 答案就是一个式子: \[ {n\choose \Sigma_{i=1}^m w}\times\pr ...
- 洛谷 P2183 [国家集训队]礼物
题目描述 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店中购买了n件礼物 ...
- luogu P2183 [国家集训队]礼物
LINK:礼物 n个物品 m个人 每个人要分得wi 个物品 每个物品互异 分给每个人的物品不分顺序 求方案数. \(n,p\leq 1e9 m\leq 5\) 方案数 那显然是 第一个人拿了w1件物品 ...
- 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 心中分量越重的人,收到的礼物会 ...
- BZOJ 2039: [2009国家集训队]employ人员雇佣
2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1369 Solved: 667[Submit ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7676 Solved: 3509[Subm ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
随机推荐
- PyCharm创建虚拟环境 和 TVTK库的安装
注:示例系统环境:Windows10 64位 + Anaconda3: 昨天安装TVTK库遇到些麻烦,当时随便将库下到个文件夹安装后(没找到VENV文件夹...), 进行测试:from tvtk.to ...
- Math Summary 数论总结
一.素数 Miller-Rabin 首先介绍一下伪素数:若n是一个正整数,且存在正整数a满足$a^{n-1}\equiv1\;(mod\;n)$ (费马小定理,但n不一定为素数) 那么我们说n是基于a ...
- prority_queue自定义类型使用
struct Tower{ Tower(int h, int p){ height = h; pos = p; } bool operator < (Tower &t) { if (he ...
- Presentation 常用的承接句——技术分享、学术报告串联全场不尴尬
前言 现在即使是搞技术,做科研的,也需要在不同的场合,用ppt来做分享,做汇报,做总结. 如果国际会议,研讨会,或者在外企,国外工作,英文的presentation就更加必不可少.英语的提升需要大家从 ...
- html5物理定位误差大 解决办法
学生党在做比赛作品,项目中需求要用到定位功能并以地图形式展现.所以思路就是用h5的geolocation 获取经纬度,通过百度地图api将经纬度转换成详细的地址以及地图.在笔记本电脑做测试,定位总有超 ...
- 百度Apollo安装说明
前言:最近在和百度Apollo合作,Apollo的人很nice,大家都在全力帮助我们解决问题.但Apollo系统有点难搞,安装起来很费劲,为了避免再次踩坑,留下笔记,流传后人,O(∩_∩)O. 1. ...
- 关于"XML 文档(2, 2)中有错误:不应有 <xml xmlns=''>"错误
XML文件名 <?xml version="1.0" encoding="utf-8"?> <Config xmlns:xsi="h ...
- arcgis js api proxy java 版本配置
<?xml version="1.0" encoding="utf-8" ?> <ProxyConfig allowedReferers=&q ...
- Dubbo实践(十三)Export
Spring在启动Dubbo服务端应用时,会实例化ServiceBean<T>并设置配置属性,然后调用export方法: @SuppressWarnings({"unchecke ...
- Android Asynctask与Handler的比较,优缺点区别,Asynctask源码
1 AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以 ...