首页 > 其他分享 >CodeForces 1924D Balanced Subsequences

CodeForces 1924D Balanced Subsequences

时间:2024-01-28 10:55:06浏览次数:24  
标签:typedef 半边 res ll long 1924D Subsequences Balanced define

洛谷传送门

CF 传送门

发现去掉匹配的 \(2k\) 个括号后,剩下的串一定形如 \()) \ldots )(( \ldots (\),其中右括号数量为 \(a = m - k\),左括号数量为 \(b = n - k\)。

考虑把剩下的串像 \()) \ldots ) \mid (( \ldots (\) 一样分成两半。枚举左半边加入了 \(\frac{i}{2}\) 对匹配,则长度为 \(a + i\),右半边长度为 \(b + 2k - i\)。根据乘法原理把两半的方案数相乘即可。下面只讨论左半边的计算,右半边类似。

考虑折线图。相当于每一步可以向右上或右下走,求 \((0, 0) \to (a + i, -a)\) 且不接触直线 \(y = -a - 1\) 的方案数。这是经典问题。考虑容斥,总方案数 \(\binom{a + i}{a + \frac{i}{2}}\) 减去接触的方案数。把折线与 \(y = -a - 1\) 最后一个交点之前的部分对称过去,相当于 \((0, -2a - 2) \to (a + i, -a)\)。可以解得需要向右上走 \(a + \frac{i}{2} + 1\) 步,所以接触的方案数就是 \(\binom{a + i}{a + \frac{i}{2} + 1}\)。

注意为了避免算重,我们钦定左半边到达 \((a + i, -a)\) 是最后一次接触直线 \(y = -a\)。也就是说右半边除了第一步外不能接触直线 \(y = -a\)。钦定第一步向右上走后也可以类似地计算。

时间复杂度 \(O(\sum k)\)。

code
// Problem: D. Balanced Subsequences
// Contest: Codeforces - Codeforces Round 921 (Div. 1)
// URL: https://codeforces.com/contest/1924/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 4020;
const int N = 4000;
const ll mod = 1000000007;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll n, m, K, fac[maxn], ifac[maxn];

inline void init() {
	fac[0] = 1;
	for (int i = 1; i <= N; ++i) {
		fac[i] = fac[i - 1] * i % mod;
	}
	ifac[N] = qpow(fac[N], mod - 2);
	for (int i = N - 1; ~i; --i) {
		ifac[i] = ifac[i + 1] * (i + 1) % mod;
	}
}

inline ll C(ll n, ll m) {
	if (n < m || n < 0 || m < 0) {
		return 0;
	} else {
		return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
	}
}

void solve() {
	scanf("%lld%lld%lld", &n, &m, &K);
	if (K > n || K > m) {
		puts("0");
		return;
	}
	ll a = m - K, b = n - K, ans = 0;
	for (int i = 0; i <= K * 2; i += 2) {
		ll la = a + i, lb = b + K * 2 - i;
		ll x = (C(la, i / 2) - C(la, i / 2 - 1) + mod) % mod;
		if (b + K - i / 2 == 0) {
			ans = (ans + x) % mod;
			continue;
		}
		if (b == 0) {
			continue;
		}
		ll y = (C(lb - 1, K - i / 2) - C(lb - 1, K - i / 2 - 1) + mod) % mod;
		ans = (ans + x * y) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	init();
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:typedef,半边,res,ll,long,1924D,Subsequences,Balanced,define
From: https://www.cnblogs.com/zltzlt-blog/p/17992560

相关文章

  • Check for balanced parentheses using stack【1月20日学习笔记】
    点击查看代码//Checkforbalancedparenthesesusingstack#include<iostream>#include<stack>//stackfromstandardtemplatelibrary(STL)#include<string>usingnamespacestd;boolarepair(charopening,charclosing){ if(opening=='(&#......
  • CodeForces 1237H Balanced Reversals
    洛谷传送门CF传送门容易想到把\(s,t\)分成长度为\(2\)的段考虑。容易发现\(00,11\)的个数在操作过程中不会改变,所以若两串的\(00\)或\(11\)个数不相等则无解。考虑依次对\(i=2,4,\ldots,n\)构造\(s[1:i]=t[n-i+1:n]\)。对于\(s_{j-1}s_j=y......
  • CF1621G Weighted Increasing Subsequences
    CF1621GWeightedIncreasingSubsequences你有一个长度为\(n\)的序列,定义\(a\)的一个长度为\(k\)的子序列为\(a_{i_1},a_{i_2},\dots,a_{i_k}\)。由此,我们不难发现,\(a\)的一个长度为\(k\)的子序列为上升子序列,当且仅当\(\forallj\in[1,k)\),\(a_{i_j}<a_{i_{j+1}}\)......
  • 『做题记录』[AGC032B] Balanced Neighbors
    [AGC032B]BalancedNeighborslink:https://atcoder.jp/contests/agc032/tasks/agc032_bDescription  给定整数\(N\),构造一个从\(1\)到\(N\)编号的\(N\)个节点的无向图,使得:该图不含有重边和自环,并且是连通的。每个节点的所有邻接节点的编号之和相同。  \(N\l......
  • Paper Reading: Oversampling with Reliably Expanding Minority Class Regions for I
    目录研究动机研究背景研究目的文章贡献本文方法可靠的扩展少数类区域的过采样方法描述方法分析多分类的OREM-MOREM和Boosting的结合计算复杂度实验结果二分类数据集实验实验设置对比实验消融实验调参实验多分类数据集实验对比实验消融实验OREMBoost实验实验设置对比实验优点......
  • [CF83E] Two Subsequences 题解
    [CF83E]TwoSubsequences题解思路定义\(overlap(a,b)\)为字符串\(a\)的后缀与\(b\)的前缀的最大相等的长度,有\(|f(a,b)|=|a|+|b|-overlap(a,b)\),下文称匹配为相邻两串的\(overlap\)。观察到每次操作之后,一定有一个序列是以\(a_i\)为结尾的。所以根据这个......
  • The 2023 ICPC Asia Hefei Regional Contest Test D. Balanced Array
    Preface这题赛场上出了个关键点基本都想到的做法,但因为一个地方卡住了没过去导致不得不选择弃掉这道题赛后补了下发现\(O(n\logn)\)的做法是只差临门一脚了,但\(O(n)\)的做法还是trick性挺强的Solution首先考虑枚举\(k\),不难发现此时合法的前缀一定是个连续的区间,其中区间的......
  • 【刷题笔记】115. Distinct Subsequences
    题目Giventwostrings s and t,return thenumberofdistinctsubsequencesof s whichequals t.Astring's subsequence isanewstringformedfromtheoriginalstringbydeletingsome(canbenone)ofthecharacterswithoutdisturbingtheremainingch......
  • 【刷题笔记】110. Balanced Binary Tree
    题目Givenabinarytree,determineifitisheight-balanced.Forthisproblem,aheight-balancedbinarytreeisdefinedas:abinarytreeinwhichthedepthofthetwosubtreesofeverynodeneverdifferbymorethan1.Example1:Giventhefollowingtree......
  • Maximum Balanced Circle
    here首先根据题意,我们不难有数字是连续的这种感悟。而且限制是值域上的,从下标入手发现难以突破,便从值域上入手。从小到大考虑每个数字,然后dp,可以参考这篇题解。至于方案的输出,有两种情况。只有自己\(i\)和\(i-1\),直接输出即可。有自己和\(i-1\)的环,定义print输出环,且最大......