Acwing.889 满足条件的01序列

题目

给定n个0和n个1,它们将按照某种顺序排成长度为2n的序列,求它们能排列成的所有序列中,能够满足任意前缀序列中0的个数都不少于1的个数的序列有多少个。

输出的答案对109+7取模。

输入格式

共一行,包含整数n。

输出格式

共一行,包含一个整数,表示答案。

数据范围

1 ≤n ≤ 105

  • 输入样例:
cpp 复制代码
3
  • 输出样例:
cpp 复制代码
5

题解

cpp 复制代码
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;

const int mod - 1e9 + 7;

int qmi(int a,int k, int p)
{
	int res = 1;
	while (k)
	{
		if (k & 1) res = (LL)res * a % p;
		a = (LL)a * a % p;
		k >>=1;
	}	
	return res;
}	
int main()
{
	int n;
	cin >> n;
	int a = 2 * n, b = n;
	int res = 1;
	
	for (int i = a; i > a - b; i -- ) res = (LL)res* i % mod;
	for (int i = l; i <= b; i ++ ) res = (LL)res * qmi(i,mod - 2,mod) % mod;

	res = (LL)res * qmi(n + 1, mod - 2, mod) % mod;

	cout << res << endl;
	return 0;
}	

思路

将这道题转换为路径为题,然后再用分组求解,思路如下图

相关推荐
tryxr7 分钟前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_311 分钟前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Selvaggia21 分钟前
DMD(Distribution Matching Distillation,分布匹配蒸馏)
算法
Navigator_Z37 分钟前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.41 分钟前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.1 小时前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
wzdark1 小时前
数据压缩算法的时间复杂度与压缩率权衡4
算法
小陈phd2 小时前
深入理解agent学习笔记(一)——现代agent介绍
人工智能·算法
晴天的雨.9925 小时前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
aramae11 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法