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 ...
随机推荐
- 面向对象(this的问题二)
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 修改访问的后缀contant
设置Struts 2处理的请求后缀及Action调用 1.在struts2中默认处理的请求后缀为action,我们可以修改struts.xml 和struts.properties来修改默认的配置,在 ...
- 使用HttpClient进行https连接(一)
一.生成密钥库和证书 1.生成服务器证书库 keytool -validity 365 -genkey -v -alias uyun -keyalg RSA -keystore /opt/UEM/ke ...
- ansible常见模块
模块的使用 查看模块帮助 ansible-doc -l 查看所有模块 ansible-doc -s MODULE_NAME 查看指定模块的详细帮助 ansible命令应用基础 语法: ansible ...
- Angular为什么选择TypeScript?
原文地址:https://vsavkin.com/writing-angular-2-in-typescript-1fa77c78d8e8 本文转自:http://www.chinacion.cn/a ...
- 自定义ListView android
定义一个实体类,作为ListView的适配器的适配类型.其中name为水果名称.imageId为水果的图片(图片资源可以随便弄几个占用) public class Fruit { private St ...
- id为194024的进程当前未运行
.NET MVC 编译运行时 提示 >> id为194024的进程当前未运行 << 清理解决方案,重新运行
- Redis的Java使用入门
因项目需要,最近简单学习了redis的使用 redis在服务器centos环境下安装比较简单. 如果要在windows上安装,可以参考别人的文章 http://blog.csdn.net/renfuf ...
- 面向对象的WebAPI框架XXL-HEX
<面向对象的WebAPI框架XXL-HEX> 一.简介 1.1 概述 XXL-HEX 是一个简单易用的WebAPI框架, 拥有 "面向对象.数据加密.跨语言" 的 ...
- 15.linux基础
1.目录 /:根目录,一般根目录下只存放目录,在Linux下有且只有一个根目录.所有的东西都是从这里开始.当你在终端里输入“/home”,你其实是在告诉电脑,先从/(根目录)开始,再进入到home目录 ...