Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)
传送门
解题思路:
假如只有 s 束花束并且不考虑 f ,那么根据隔板法的可重复的情况时,这里的答案就是

假如说只有一个 f 受到限制,其不合法时一定是取了超过 f 的花束
那么根据组合数,我们仍然可以算出其不合法的解共有:

最后,由于根据容斥,减两遍的东西要加回来,那么含有偶数个 f 的项为正,奇数个时为负。
答案就是:

搜索答案,使用Lucas定理,计算组合数上下约去。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long lnt;
const lnt mod=(lnt)(1e9+);
int n;
lnt s,ans;
lnt f[];
lnt ksm(lnt x,lnt y)
{
lnt ans=;
while(y)
{
if(y&)
ans=ans*x%mod;
x=x*x%mod;
y=y/;
}
return ans;
}
lnt Lucas(lnt n,lnt m)
{
if(n<mod&&m<mod)
{
if(n<m) return ;
if(n==m)return ;
if(m==)return ;
lnt nj=,mj=;
lnt a=n-m,b=m;
if(a>b)
std::swap(a,b);
lnt i=b+;
while(i<=n)
{
nj=(nj*i)%mod;
i++;
}
i=;
while(i<=a)
{
mj=(mj*i)%mod;
i++;
}
return ksm(mj,mod-)*nj%mod;
}
return Lucas(n%mod,m%mod)*Lucas(n/mod,m/mod)%mod;
}
lnt dle(lnt x)
{
return (((&x)^)<<)-;
}
void dfs(int p,int l,lnt sum)
{
if(p==n+)
{
ans=(ans+dle(l)*Lucas(s-sum+n-,n-))%mod;
return ;
}
dfs(p+,l,sum);
dfs(p+,l+,sum+f[p]+);
return ;
}
int main()
{
scanf("%d%I64d",&n,&s);
for(int i=;i<=n;i++)
scanf("%I64d",&f[i]);
ans=;
dfs(,,);
printf("%I64d\n",(ans%mod+mod)%mod);
return ;
}
Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)的更多相关文章
- Codeforces 451 E Devu and Flowers
Discription Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th ...
- 【Codeforces 258E】 Devu and Flowers
[题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...
- CF451E Devu and Flowers (组合数学+容斥)
题目大意:给你$n$个箱子,每个箱子里有$a_{i}$个花,你最多取$s$个花,求所有取花的方案,$n<=20$,$s<=1e14$,$a_{i}<=1e12$ 容斥入门题目 把取花 ...
- Devu and Flowers lucas定理+容斥原理
Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...
- Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥
E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...
- Codeforces 451E Devu and Flowers(容斥原理)
题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- E. Devu and Flowers
E. Devu and Flowers time limit per test 4 seconds memory limit per test 256 megabytes input standard ...
- CF451E Devu and Flowers 解题报告
CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...
随机推荐
- 三星N900(note3)刷机包 颓废N0.8.1 修复已知BUG 集成谷歌服务
ROM介绍 8.1更新信息:攻克了来电后点击HOME出现SECPHONE已经停止的问题 去掉了桌面隐藏信息的选项,官方最新底包暂不支持这功能 增加了网友们须要验证的谷歌服务(不须要的同学同步什么的都关 ...
- 菜鸟之webservice(一) 服务端搭建
首先说一下,为什么取名叫菜鸟之webservice,由于本人技术真的不咋滴,写博客仅仅是为了对所学知识的总结.webservice对于我来说一直都是高大上的感觉,一个java web和javase都没 ...
- linux设备驱动归纳总结(三):4.ioctl的实现
linux设备驱动归纳总结(三):4.ioctl的实现 一.ioctl的简单介绍: 尽管在文件操作结构体"struct file_operations"中有非常多相应的设备操作函数 ...
- strchr函数的实现而不是使用
刚刚在写一个程序的时候突然须要用到定位到一个字符串中第一次出现某个字符的位置,于是就找到了strchr()函数,之前从没实用过的,^_^当然我能够直接调用就可以.可是拥有良好程序素质的洗衣袋决定要想实 ...
- php 读取windows 的系统版本,硬盘,内存,网卡,数据流量等
php 读取windows 的系统版本,硬盘,内存,网卡,数据流量等 <?php header("Content-type: text/html; charset=utf-8" ...
- Visual Studio Code Setup
Windows https://code.visualstudio.com/docs/setup/windows Additional Components and Tools https://cod ...
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) ...
- 智课雅思短语---一、be no exception
智课雅思短语---一.be no exception 一.总结 一句话总结:…也不例外? …be no exception 1.经济的快速发展? the rapid development of ec ...
- ES6特性-带标签的模板字符串(tagged template)
tagged template: 加在模板字符串前面加一个标签(函数). let dessert = = '甜品' drink = '茶' let breakfast = kitchen`今天的早餐是 ...
- Swift学习笔记(3)--基本运算符
基本运行符: +(加法.正数) - (减法.负数) * (乘法) / (除法) % (求余) : 在Swift中,求余可以是浮点数求余. &&(逻辑与) || (逻辑或) ^ (逻 ...