Educational Codeforces Round 80 C. Two Arrays(组合数快速取模)
2024-08-31 11:14:58
You are given two integers nn and mm . Calculate the number of pairs of arrays (a,b)(a,b) such that:
- the length of both arrays is equal to mm ;
- each element of each array is an integer between 11 and nn (inclusive);
- ai≤biai≤bi for any index ii from 11 to mm ;
- array aa is sorted in non-descending order;
- array bb is sorted in non-ascending order.
As the result can be very large, you should print it modulo 109+7109+7 .
Input
The only line contains two integers nn and mm (1≤n≤10001≤n≤1000 , 1≤m≤101≤m≤10 ).
Output
Print one integer – the number of arrays aa and bb satisfying the conditions described above modulo 109+7109+7 .
Examples
Input
2 2
Output
5
Input
10 1
Output
55
Input
723 9
Output
157557417 由题意可知,这个题所求的两个序列实际上可以合并成一个,将递增序列的头部接到递减序列的尾部,所得一个长为2m的序列。原问题可以转化为求范围1到n的2m个数组成的递增序列的个数(一个序列里可以有两个重复的数)。官方题解直接用Python+组合数公式
from math import factorial as fact
mod = 10**9 + 7 def C(n, k):
return fact(n) // (fact(k) * fact(n - k)) n, m = map(int, input().split())
print(C(n + 2*m - 1, 2*m) % mod)
从原理入手,当这2m个数里有i个数不相同时,先从n个位置里用C(n,i)算出当前有几种选法,接下来有2m-i个数是和刚刚选出来的序列的部分数相同,可以看作同球入不同盒问题直接应用公式计算即可,每次循环更新ans。题目要求取模,这里借鉴了https://www.csdn.net/gather_2b/NtzaIgxsNzQtYmxvZwO0O0OO0O0O.html里组合数快速取模的知识。
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
long long n,m;
int inv(int a)
{
return a==?:(long long)(MOD-MOD/a)*inv(MOD%a)%MOD;
}
long long C(long long n,long long m)//组合数快速取模
{
if(m<)return ;
if(n<m)return ;
if(m>n-m)m=n-m;
long long up=,down=;
long long i;
for(i=;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+)%MOD;
}
return up*inv(down)%MOD;
}
int main()
{
cin>>n>>m;
int i;
long long ans=;
for(i=*m;i>=;i--)//有i个不同的数
{
if(i>n)continue;
ans=(ans+C(n,i)*/*同球入不同盒 2*m-i入i */ C(*m-i+i-,i-))%MOD;
}
printf("%lld",ans);
return ;
}
最新文章
- Windows 8 下离线安装。net Framework 3.5
- Android课程---添加黑名单的练习(课堂讲解)
- 普通硬件就能破解GSM A5加密算法
- Eclipse取消设置项目默认空间
- 前端框架——AmazeUI学习
- JSON.parse()和JSON.stringify()(转载)
- SteamVR Unity工具包(VRTK)之概览和控制器事件
- [转]PLS-S-00201, identifier &#39;CALLDEMO.GET_EMPLOYEES&#39; must be declared 预编译错误原因及解决办法
- CPU/寄存器/内存
- Spring Web MVC(一)
- kotlin web开发教程【一】从零搭建kotlin与spring boot开发环境
- GoLand、Pycharm注册码
- linux下安装好mysql后,登录时提示libgcc_s.so.1 must be installed for pthread_cancel to work
- java学习之路--StringBuffer常见的功能和实例
- python学习笔记四——循环及冒泡排序
- Mysql 删除重复数据只保留id最小的
- Mybatis之trim标签的理解
- Redis持久化--RDB+AOF(转)
- tomcat之日志切割
- IE6 验证码刷新失败显示空白解决办法