51nod 1269 Devu and Flowers

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1269
关于容斥原理阔以看一哈这位童鞋滴博客:
https://blog.csdn.net/hnust_xiehonghao/article/details/7893232#comments
这道题就是方程整数解那种模型
题意:
x1+x2+x3+...+x20=K x 1 + x 2 + x 3 + . . . + x 20 = K 求有好多种方法?( K<1e18,x<1e16 K < 1 e 18 , x < 1 e 16 )

这道题盒子很少,20个, 220 2 20 是好多?有没有想状态压缩的赶脚
而这种类型的就是高中那个隔板法来求,分成20坨,要19个隔板,就是 C(K,19) C ( K , 19 ) 但是隔板法是没有限制一坨有好多。。。但是这道题的意思就是说,限制了一坨最多有那么多,所以就要把超过了的这些种情况减掉,思路就是这样,但是怎么减喃?就是用的容斥原理来减的

答案=超了0个 减 超了1个 加 超了2个 减 超了三个…..

#include"iostream"
using namespace std;
const int MOD=1e9+7;
typedef long long LL;
const int maxn=25;
LL N,K;
LL a[maxn];
LL mul(LL a,LL b,LL mod)
{
    double x=a;
    LL res=a*b-(LL)(x*b/mod)*mod;
    return (res+mod)%mod;
}
LL ksm(LL a,LL b,LL mod)
{
    LL res=1,base=a;
    while(b)
    {
        if(b&1)res=mul(res,base,MOD);
        b>>=1;
        base=mul(base,base,MOD);
    }
    return res;
}
LL C(LL n,LL m)
{
    if(m<0||m>n)return 0;
    if(m==0)return 1;
    LL res=1,b=1;
    for(LL i=1;i<=m;i++)
    {
        res=mul(res,n-m+i,MOD);
        b=mul(b,i,MOD);
    }
    res=mul(res,ksm(b,MOD-2,MOD),MOD);
    return res;
}
LL Lucas(LL n,LL m)
{
    if(m<0||m>n)return 0;
    if(m==0)return 1;
    return mul(C(n%MOD,m%MOD),Lucas(n/MOD,m/MOD),MOD);
}

LL solve()
{
    LL res=0,sum,cnt;

    for(int i=0;i<(1<<N);i++)//枚举每一种情况
    {
        sum=K;
        cnt=0;
        for(int j=0;j<N;j++)
        {
            if(i&(1<<j))
            {
                cnt++;//用来看容斥是加还是减
                sum-=a[j]+1;//假如这一种超了
            }

        }
        if(sum<0)continue;
        //剩下的求个组合数
        if(cnt&1)res-=Lucas(sum-1+N,N-1);
        else res+=Lucas(sum-1+N,N-1);
        res=(res+MOD)%MOD;
    }
    return (res+MOD)%MOD;
}
int main()
{
    while(cin>>N>>K)
    {
        for(int i=0;i<N;i++)cin>>a[i];
        cout<<solve()<<"\n";
    }
}


/*母函数的,看不懂*/
//#include<stdio.h>
//#include<string.h>
//#include<math.h>
//#include<iostream>
//#include<algorithm>
//using namespace std;
//#define N 55
//#define ll long long
//#define inf 0x3f3f3f3f
//
//ll mod=1e9+7;
//ll f[22];
//ll n,s;
//ll inv(ll a)//求逆元
//{
//  return a == 1 ? 1 : (long long)(mod - mod / a) * inv(mod % a) % mod;
//}
//ll choose(ll m,ll k)
//{
//  ll ret=1;
//  for(ll i=0; i<k; i++)
//      ret=ret*((m-i)%mod)%mod*inv(i+1)%mod;
//  return ret;
//}
//ll go(ll i,ll s)//求解系数
//{
    cout<<"i="<<i<<" s="<<s<<"\n"; 
//  if(s<0) return 0;
//  if(i==n)
//  {
//      cout<<"i=n="<<i<<"\n";
//      cout<<"s="<<s<<"\n";
//      return choose(n+s-1,n-1);
//  } 
//      
//  return (go(i+1,s)-go(i+1,s-f[i])+mod)%mod;
//}
//int main()
//{
//  
//  cin>>n>>s;
//  for(int i=0; i<n; i++)
//  {
//      cin>>f[i];
//      f[i]++;
//  }
//  cout<<"aaa\n";
//  cout<<(go(0,s)+mod)%mod<<endl;
//  //system("pause");
//} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值