BZOJ_2142_礼物_扩展lucas+组合数取模+CRT
BZOJ_2142_礼物_扩展lucas+组合数取模
Description
Input
Output
若不存在可行方案,则输出“Impossible”,否则输出一个整数,表示模P后的方案数。
Sample Input
4 2
1
2
Sample Output
【样例说明】
下面是对样例1的说明。
以“/”分割,“/”前后分别表示送给第一个人和第二个人的礼物编号。12种方案详情如下:
1/23 1/24 1/34
2/13 2/14 2/34
3/12 3/14 3/24
4/12 4/13 4/23
【数据规模和约定】
设P=p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct,pi为质数。
对于100%的数据,1≤n≤109,1≤m≤5,1≤pi^ci≤10^5。
$=(1*2*4*5*7*8)*(10*11*13*14*16*17)*3^{6}*6!$
$=fac[pk]^{\lfloor \frac {n} {pk} \rfloor}*p^{\lfloor \frac {n} {p} \rfloor}*(\lfloor \frac {n} {p} \rfloor)! $
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
int n,m;
ll fac[100050];
ll w[10],sum,mods[10050],ks[10050],MOD;
ll qp(ll x,ll y,ll mod) {ll re=1;for(;y;y>>=1ll,x=x*x%mod) if(y&1ll)re=re*x%mod; return re;}
void exgcd(ll a,ll b,ll &x,ll &y,ll &p) {
if(!b) {p=a; x=1; y=0; return ;}
exgcd(b,a%b,y,x,p); y-=a/b*x;
}
ll INV(ll a,ll b) {
ll x,y,d;
exgcd(a,b,x,y,d);
return d==1?(x%b+b)%b:-1;
}
ll Fac(ll x,ll p,ll pk) {
if(!x) return 1;
return qp(fac[pk],x/pk,pk)*fac[x%pk]%pk*Fac(x/p,p,pk)%pk;
}
ll C(ll x,ll y,ll p,ll pk) {
if(x<y) return 0;
ll i,re=0;
for(i=x;i;i/=p) re+=i/p;
for(i=y;i;i/=p) re-=i/p;
for(i=x-y;i;i/=p) re-=i/p;
re=qp(p,re,pk);
if(!re) return 0;
for(fac[0]=1,i=1;i<=pk;i++) fac[i]=i%p?fac[i-1]*i%pk:fac[i-1];
return re*Fac(x,p,pk)%pk*INV(Fac(y,p,pk),pk)%pk*INV(Fac(x-y,p,pk),pk)%pk;
}
ll crt(ll x,ll y) {
ll ans=0;int i;
for(i=1;i<=mods[0];i++) {
ll Mi=MOD/ks[i],Ai=C(x,y,mods[i],ks[i]),Ti=INV(Mi,ks[i]);
ans=(ans+Mi*Ai%MOD*Ti%MOD)%MOD;
}
return ans;
}
int main() {
scanf("%lld%d%d",&MOD,&n,&m);
int i;
for(i=1;i<=m;i++) scanf("%lld",&w[i]),sum+=w[i];
if(sum>n) {
puts("Impossible"); return 0;
}
ll j=MOD;
for(i=2;1ll*i*i<=j;i++) {
if(j%i==0) {
mods[++mods[0]]=i; ks[mods[0]]=1;
while(j%i==0) j/=i,ks[mods[0]]*=i;
}
}
if(j!=1) mods[++mods[0]]=j,ks[mods[0]]=j;
ll ans=1;
for(i=1;i<=m;i++) {
ans=ans*crt(n,w[i])%MOD;
n-=w[i];
}
printf("%lld\n",ans);
}
BZOJ_2142_礼物_扩展lucas+组合数取模+CRT的更多相关文章
- BZOJ 3656: 异或 (组合数取模 CRT)
http://www.lydsy.com/JudgeOnline/problem.php?id=3656 大意:经过一通推导,问题变成求\[\binom N M \mod P\],其中N,M<= ...
- LightOJ 1226 - One Unit Machine Lucas/组合数取模
题意:按要求完成n个任务,每个任务必须进行a[i]次才算完成,且按要求,第i个任务必须在大于i任务完成之前完成,问有多少种完成顺序的组合.(n<=1000 a[i] <= 1e6 mod ...
- 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)
J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- lucas定理解决大组合数取模
LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...
- 组合数取模(lucas定理+CRT合并)(AC)
#include<bits/stdc++.h> #define re register #define int long long using namespace std; ; inlin ...
随机推荐
- Supervisor安装、配置、开启启动
1.安装Python包管理工具(easy_install) wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - ...
- WSGI及gunicorn指北(二)
pyg0已经大概了解了wsgi.现在他决定深入探索他们实际在生产环境里用到的web 服务器 -gunicorn. 先来看看官网的介绍:Gunicorn 是一个运行在Unix上的python WSGI ...
- 第一次作业 orm环境构建(hibernate)及基本的demo
一.数据库 1.创建数据库hibernate01-1514010311 2.创建表 customer CREATE TABLE customer( id int(11) not null auto_i ...
- gitlab钩子搭建
目标:在本地开发机上push代码到GitLab仓库时,通过钩子同步到测试服务器 准备工作GitLab 服务器一台测试服务器一台本地开发服务器一台 1.在gitlab上新建一个项目,名称test2.在本 ...
- JavaScipt浅谈——全局变量和局部变量
全局变量的作用域为所属的整个程序. 全局变量的定义形式有: (1)在函数外定义 (2)在函数内定义,但不加var声明 (3)使用 window.变量名 的形式定义 (4) ...
- Spring Boot 事件和监听
Application Events and Listeners 1.自定义事件和监听 1.1.定义事件 package com.cjs.boot.event; import lombok.Data; ...
- angular 表单验证
最近在用angular写表单验证时 , 不小心把ng-model全替换删掉了, 然后发现之前写的验证都失效, 在查阅资料和反复修改摸索后, 发现angular中的表单验证, 都是基于ng-model的 ...
- vue项目在移动端(手机)调试
查了很长一段时间的资料才搞好. 感悟就是:原来那么简单呐. 首要条件:同一局域网下(大致理解为链接相同的wifi) 1:命令行运行 ipconfig 2: 得到ipv4值, 用该值替换localhos ...
- 洛谷 P1462 解题报告
P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...
- 第六章 MySQL 查询
查询数据表 语法: SELECT {* | <字段列表>} [ FROM <表1>, <表2>.... [ where <表达式> ] [ group ...