题目大意:给定一个方程$X_{1}+X_{2}+X_{3}+X_{4}+...+X_{n}=M$,$\forall X_{i}<=A_{i} (i<=n1)$ $\forall X_{i}>=A_{i} (n1<i<=n2)$在保证的合法整数解个数n1<=8,n2<=8

一波三折的数学题,调了半天才发现我的Lucas是错的,但它竟然通过了洛谷那一道模板题的全部数据....

后面n1~n2的部分很好处理,直接用M减掉这个部分就行了

因为是求正整数解,所以这个组合数的形式可以用隔板法处理,即每两个物品之间设为一个空位,现在要分成n个部分,则把n-1个隔板放进空位

即$C_{m-1}^{k-1}$

前面1~n1的部分依然使用容斥的方法处理,类似于CF451E,状压+容斥

接下来就是拓展Lucas了,讲解传送门

 #include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 10
#define ll long long
using namespace std; ll exgcd(ll a,ll b,ll &x,ll &y){
if(!b) {x=,y=;return a;}
ll g=exgcd(b,a%b,x,y);
ll t=x;x=y,y=t-a/b*y;
return g;
}
ll qmul(ll x,ll y,const ll &mo){
ll ans=;
while(y){
if(y&) ans=(ans+x)%mo;
x=(x+x)%mo,y>>=;
}return ans;
}
ll qpow(ll x,ll y,const ll &mo){
ll ans=;
while(y){
if(y&) ans=ans*x%mo;
x=x*x%mo,y>>=;
}return ans;
}
ll son1[]={};
ll son2[]={,,,,};
ll son3[]={,,}; namespace exlucas{
ll ans=,M=;
ll son[],pw[];
int num;
void Pre(ll p)
{
if(p==){
num=,son[]=son1[],pw[]=son1[];
}else if(p==){
num=;
for(int i=;i<num;i++)
son[i]=son2[i],pw[i]=son[i];
}else{
num=;
for(int i=;i<num;i++)
son[i]=son3[i];
pw[]=,pw[]=,pw[]=;
}
}
int excrt_ins(ll A,ll B)
{
ll a=A,b=B,c=(a-ans%b+b)%b,x,y;
ll g=exgcd(M,b,x,y);ll bg=b/g;
if(c%g!=) return -;
//x=x*(c/g)%bg;
x=qmul(x,c/g,bg);
ans+=x*M,M*=bg,ans=(ans%M+M)%M;
return ;
}
ll get_mul(ll n,ll p,ll &sum,const ll &mo,int type)
{
if(n==) return ;
ll ans=;
for(int i=;i<=min(n,mo);i++)
if(i%p) ans=ans*i%mo;
ans=qpow(ans,n/mo,mo);
for(int i=;i<=n%mo;i++)
if(i%p) ans=ans*i%mo;
sum+=1ll*(n/p)*type;
return ans*get_mul(n/p,p,sum,mo,type)%mo;
}
ll get_C(ll n,ll m,ll p,const ll &mo)
{
if(m>n) return ;
ll sum=;ll y;
ll nn=get_mul(n,p,sum,mo,);
ll mm=get_mul(m,p,sum,mo,-);
ll nm=get_mul(n-m,p,sum,mo,-);
exgcd(mm,mo,mm,y);
mm=(mm%mo+mo)%mo;
exgcd(nm,mo,nm,y);
nm=(nm%mo+mo)%mo;
return nn*mm%mo*nm%mo*qpow(p,sum,mo)%mo;
}
ll C(ll n,ll m,const ll &mo)
{
if(m>n) return ;
ll ret=;
for(int i=;i<num;i++){
ll val=get_C(n,m,son[i],pw[i]);
excrt_ins(val,pw[i]);
}
ret=ans,M=,ans=;
return ret;
}
}; int T;ll p;
ll n,m,n1,n2;
ll a[N],b[N]; int main()
{
// freopen("t1.in","r",stdin);
scanf("%d%lld",&T,&p);
exlucas::Pre(p);
while(T--)
{
scanf("%lld%lld%lld%lld",&n,&n1,&n2,&m);
memset(a,,sizeof(a));memset(b,,sizeof(b));
for(int i=;i<n1;i++) scanf("%lld",&a[i]);
for(int i=;i<=n2;i++) scanf("%lld",&b[i]),m-=(b[i]-);
if(m<) printf("0\n");
int tot=(<<n1);ll ans=;
for(int s=;s<tot;s++)
{
ll res=m;int cnt=;
for(int i=;i<n1;i++) if(s&(<<i)) res-=a[i],cnt++;
if(res<=) {continue;}
ll w=(cnt&?-1ll:1ll)*exlucas::C(res-,n-,p);
(ans+=w)%=p;
}
printf("%lld\n",(ans%p+p)%p);
}
return ;
}

BZOJ 3129 [SDOI2013]方程 (拓展Lucas)的更多相关文章

  1. [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】

    题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...

  2. ●BZOJ 3129 [Sdoi2013]方程

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3129 题解: 容斥,扩展Lucas,中国剩余定理 先看看不管限制,只需要每个位置都是正整数时 ...

  3. BZOJ 3129 SDOI2013 方程

    如果没有限制,答案直接用隔板法C(m-1,n-1) 对于>=x的限制,我们直接在对应位置先放上x-1即可,即m=m-(x-1) 对于<=x的限制,由于限制很小我们可以利用容斥原理将它转化为 ...

  4. bzoj千题计划267:bzoj3129: [Sdoi2013]方程

    http://www.lydsy.com/JudgeOnline/problem.php?id=3129 如果没有Ai的限制,就是隔板法,C(m-1,n-1) >=Ai 的限制:m减去Ai &l ...

  5. bzoj3129[Sdoi2013]方程 exlucas+容斥原理

    3129: [Sdoi2013]方程 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 582  Solved: 338[Submit][Status][ ...

  6. 【BZOJ3129】[SDOI2013]方程(容斥,拓展卢卡斯定理)

    [BZOJ3129][SDOI2013]方程(容斥,拓展卢卡斯定理) 题面 BZOJ 洛谷 题解 因为答案是正整数,所先给每个位置都放一个就行了,然后\(A\)都要减一. 大于的限制和没有的区别不大, ...

  7. 【bzoj2142】【礼物】拓展Lucas定理+孙子定理

    (上不了p站我要死了,侵权度娘背锅) Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同,在小E心中分量 ...

  8. BZOJ3129: [Sdoi2013]方程

    拓展Lucas+容斥原理 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cs ...

  9. BZOJ_3129_[Sdoi2013]方程_组合数学+容斥原理

    BZOJ_3129_[Sdoi2013]方程_组合数学+容斥原理 Description 给定方程     X1+X2+. +Xn=M 我们对第l..N1个变量进行一些限制: Xl < = A ...

随机推荐

  1. MySQL 关闭 binlog 日志

    [关闭binlog日志] 1.vim /etc/my.cnf 注释如下内容: #log-bin=mysql-bin #binlog_format=mixed #server-id = 1 #expir ...

  2. idea编写Swing程序中文乱码的解决办法

    Run -> Edit Configurations ,在图示位置加入-Dfile.encoding=gbk

  3. HTML中使用 js 添加 data-toggle

    情况:<li class="active"><a href="#server1" data-toggle="tab"> ...

  4. 使用sourceMap文件定位小程序错误信息

    sourceMap是什么 在前端开发过程中代码难免会有错误,即便是再小心,也有可能出现 Cannot read property 'xxx' of null 这样的低级失误,debug自然是家常便饭. ...

  5. springboot项目封装为docker镜像

    1.本次镜像的基础镜像是:https://www.cnblogs.com/JoeyWong/p/9173265.html 2.将打包好的项目文件放在与Dockerfile同级的目录下 3.Docker ...

  6. NYIST 1030 Yougth's Game[Ⅲ]

    Yougth's Game[Ⅲ]时间限制:3000 ms | 内存限制:65535 KB难度:4 描述有一个长度为n的整数序列,A和B轮流取数,A先取,每次可以从左端或者右端取一个数,所有数都被取完时 ...

  7. 洛谷 P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  8. POJ3684 Physics Experiment 【物理】

    Physics Experiment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1031   Accepted: 365 ...

  9. lua简单类的实现

    原文地址:http://blog.csdn.net/qqmcy/article/details/37725177 类实现: MyClass = class("MyClass") - ...

  10. adt-bundle-windows加入NDK支持

    近期换了个硬盘,曾经都是用eclipse安装adt插件的,如今老了,图省事就下载了adt-bundle-windows,解压缩出来就直接用.但是这个adt-bundle没有集成NDK支持,于是手动安装 ...