Atcoder Beginner Contest 156E(隔板法,组合数学)
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + ;
const long long mod=1e9+;
long long fac[N],inv[N];
long long qpow(long long a,long long b){
long long ans=;
while(b){
if(b&)
ans=a*ans%mod;
b>>=;
a=a*a%mod;
}
return ans;
}
void P(){
fac[]=;
for(int i=;i<N;i++)
fac[i]=fac[i-]*i%mod;
inv[N-]=qpow(fac[N-],mod-);
for(int i=N-;i>=;i--)
inv[i]=inv[i+]*(i+)%mod;
}
long long C(int a,int b){
return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
int n,k;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
P();
cin>>n>>k;
if(k>=n)//如果k>=n的话,所有1都可以移动,相当于n个相同球放到n个相同盒中,盒子可以空。
cout<<C(n+n-,n-);//n个球和n个盒子总共有2n-1个空隙,把n个盒子插到空隙里,即为C(2n-1,n)=C(2n-1,n-1)
else{
long long tot=C(*n-,n-);
for(int i=;i<n-k;i++){//n-k个位置至少为1,容斥减去不足n-k个位置至少为1的情况
tot-=1ll*C(n,i)*C(n-,i-)%mod;//n个球放到i个盒子里,每个盒子都不为空
//先从n个盒子里选出i个盒子,然后n个球之间存在n-1个空隙,把i-1块挡板插到n-1个空隙里,就可以把n个球分为i份且每一份都不为空
tot+=mod;
tot%=mod;
}
cout<<tot<<'\n';
}
return ;
}
Atcoder Beginner Contest 156E(隔板法,组合数学)的更多相关文章
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
随机推荐
- VMware与Centos系统安装之重置root密码
VMware与Centos系统安装之重置root密码 今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟 ...
- 第6章.BitArray类
参考链接: https://www.cnblogs.com/wang7/archive/2012/04/13/2446353.html https://www.runoob.com/csharp/cs ...
- 剑指offer-面试题44-数字序列中某一位的数字-脑筋急转弯
/* 题目: 数字以0123456789101112131415…的格式序列化到一个字符序列中. 在这个序列中,第5位(从0开始计数,即从第0位开始)是5,第13位是1,第19位是4,等等. 请写一个 ...
- 疑问:Iterator 遍历器和数据集合各种遍历方法的区别
https://es6.ruanyifeng.com/#docs/iterator Iterator(遍历器)的概念 Iterator 接口主要供for...of消费 Iterator 的遍历过程是: ...
- 【剑指Offer】63、二叉搜索树的第k个结点
题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. 题解一:DFS 借助栈实现 // 中序非递归 public ...
- bitset刷题记录
大佬的bitset用法小结 https://www.cnblogs.com/zwfymqz/p/8696631.html BZOJ3687简单题 题意:求子集的算术和的异或和,子集大小为n(n< ...
- java简单验证码生成程序
下面的函数,返回的字符串就是所需验证码 public String id(){ Random ra =new Random(); st=""; String [] w= {&quo ...
- 【Spring】利用spring的JdbcTemplate查询返回结果映射到自定义类型
// org.springframework.jdbc.core.JdbcTemplate 中的查询方法基本都有支持参数RowMapper<T> rowMapper的重载方法.下面只是随便 ...
- 【JavaScript】JS总结 – 乱
一. 重要:js中的function函数声明.函数表达式 // 函数声明 // Ex: 会在代码执行之前提前加载到作用域中,即js解析器会优先读取,确保在所有代码执行之前声明已经被解析:所以可以在定义 ...
- python全栈学习 day04
列表基本操作: #!/usr/bin/env python # -*- coding:utf-8 -*- ''' li = ['alex', [1, 2, 3], 'wusir', 'godness' ...