首页 > 其他分享 >CodeForces 960G Bandit Blues

CodeForces 960G Bandit Blues

时间:2023-09-07 13:22:37浏览次数:50  
标签:typedef int res ll CodeForces long Bandit Blues 最大值

洛谷传送门

CF 传送门

发现设排列最大值位置为 \(i\),那么 \([1, i]\) 只可能存在前缀最大值,\([i, n]\) 只可能存在后缀最大值。

由此设 \(f_{i, j}\) 为长度为 \(i\) 的排列,前缀最大值有 \(j\) 个的方案数,有转移:

\[f_{i, j} = f_{i - 1, j - 1} + (i - 1)f_{i - 1, j} \]

意思是每次可以填最大值,或者选一个不是最大值的空隙插进去。

那么答案为:

\[\sum\limits_{i = 1}^n f_{i - 1, a - 1} \times f_{n - i, b - 1} \times \binom{n - 1}{i - 1} \]

容易发现 \(f_{i, j} = \begin{bmatrix} i \\ j \end{bmatrix}\),所以推到这一步应该就可以用 P5409 第一类斯特林数·列 的做法过了,但是这题不止于此。

考虑上式的组合意义就是,先从 \(n - 1\) 个球选出 \(i - 1\) 个,将它们组成 \(a - 1\) 个环,再把剩下的 \(n - i\) 个球组成 \(b - 1\) 个环,\(i\) 取遍 \([1, n]\)。听起来很怪对吧。

我们考虑直接把 \(n - 1\) 个球分成 \(a + b - 2\) 个环,考虑每种这样的方案在答案中被统计的次数。其实这个次数就是在分好的这 \(a + b - 2\) 个环选 \(a - 1\) 个环的方案数,因为 \(i\) 取遍 \([1, n]\)。

所以答案就是 \(\begin{bmatrix} n - 1 \\ a + b - 2 \end{bmatrix} \binom{a + b - 2}{a - 1}\)。使用 P5408 第一类斯特林数·行 的方法计算,复杂度还是 \(O(n \log n)\)。

code
// Problem: G. Bandit Blues
// Contest: Codeforces - Divide by Zero 2018 and Codeforces Round 474 (Div. 1 + Div. 2, combined)
// URL: https://codeforces.com/problemset/problem/960/G
// Memory Limit: 256 MB
// Time Limit: 3500 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 = 500100;
const ll mod = 998244353, G = 3;

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, A, B, r[maxn], fac[maxn], ifac[maxn];

typedef vector<ll> poly;

inline poly NTT(poly a, int op) {
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		if (i < r[i]) {
			swap(a[i], a[r[i]]);
		}
	}
	for (int k = 1; k < n; k <<= 1) {
		ll wn = qpow(op == 1 ? G : qpow(G, mod - 2), (mod - 1) / (k << 1));
		for (int i = 0; i < n; i += (k << 1)) {
			ll w = 1;
			for (int j = 0; j < k; ++j, w = w * wn % mod) {
				ll x = a[i + j], y = w * a[i + j + k] % mod;
				a[i + j] = (x + y) % mod;
				a[i + j + k] = (x - y + mod) % mod;
			}
		}
	}
	if (op == -1) {
		ll inv = qpow(n, mod - 2);
		for (int i = 0; i < n; ++i) {
			a[i] = a[i] * inv % mod;
		}
	}
	return a;
}

inline poly operator * (poly a, poly b) {
	a = NTT(a, 1);
	b = NTT(b, 1);
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		a[i] = a[i] * b[i] % mod;
	}
	a = NTT(a, -1);
	return a;
}

inline poly mul(poly a, poly b) {
	int n = (int)a.size() - 1, m = (int)b.size() - 1, k = 0;
	while ((1 << k) <= n + m + 1) {
		++k;
	}
	for (int i = 1; i < (1 << k); ++i) {
		r[i] = (r[i >> 1] >> 1) | ((i & 1) << (k - 1));
	}
	poly A(1 << k), B(1 << k);
	for (int i = 0; i <= n; ++i) {
		A[i] = a[i];
	}
	for (int i = 0; i <= m; ++i) {
		B[i] = b[i];
	}
	poly res = A * B;
	res.resize(n + m + 1);
	return res;
}

inline poly dmul(poly a, poly b) {
	int n = (int)a.size();
	reverse(a.begin(), a.end());
	a = mul(a, b);
	a.resize(n);
	reverse(a.begin(), a.end());
	return a;
}

inline poly work(poly a) {
	int n = (int)a.size() - 1;
	for (int i = 0; i <= n; ++i) {
		a[i] = a[i] * fac[i] % mod;
	}
	poly b(n + 1);
	ll pw = 1;
	for (int i = 0; i <= n; ++i) {
		b[i] = pw * ifac[i] % mod;
		pw = pw * n % mod;
	}
	poly c = dmul(a, b);
	for (int i = 0; i <= n; ++i) {
		c[i] = c[i] * ifac[i] % mod;
		a[i] = a[i] * ifac[i] % mod;
	}
	poly res = mul(a, c);
	return res;
}

poly dfs(int n) {
	if (n == 1) {
		poly res(2);
		res[1] = 1;
		return res;
	}
	int m = n / 2;
	poly res = dfs(m);
	res = work(res);
	if (n & 1) {
		poly a(2);
		a[0] = n - 1;
		a[1] = 1;
		res = mul(res, a);
	}
	return res;
}

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, &A, &B);
	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;
	}
	if (!A || !B || A + B - 1 > n) {
		puts("0");
		return;
	}
	if (n == 1) {
		puts("1");
		return;
	}
	poly res = dfs(n - 1);
	printf("%lld\n", res[A + B - 2] * C(A + B - 2, A - 1) % mod);
}

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

标签:typedef,int,res,ll,CodeForces,long,Bandit,Blues,最大值
From: https://www.cnblogs.com/zltzlt-blog/p/17684567.html

相关文章

  • Codeforces Round 406 (Div. 2) D. Legacy 线段树优化建图
    传送门题目大意:给定n个点,m个操作,和起点s。其中n和q大于等于1小于等于1e5,s大于等于1小于等于n其中m个操作有三种情况:  1.输入1uvval表示从u号点向v号点连一个权值为val的有向边,其中1<=u<=n,1<=v<=n,1<=val<=1e9  2.输入2ulrval表示从u号点......
  • Educational Codeforces Round 154 (Rated for Div. 2)
    EducationalCodeforcesRound154(RatedforDiv.2)比赛链接我都快忘了还有这一场比赛,今天打开cf看见这场比赛正好有时间就补了!!!2023.9.3也许是出去玩了一下午脑子不够用了??怎么现在读题都有一点读不懂了!!!2023.9.4我靠这场我怎么感觉没什么思路呢????A题PrimeDeletion题目链接......
  • Educational Codeforces Round 154 (Rated for Div. 2)
    Preface太FW了现在,纯纯给队伍拖后腿,马上要成为我们队CFRating最低的了但换句话说徐神和祁神都这么猛,我直接躺着被嘎嘎带飞好像也很爽啊不管怎么样还是要多练,不过接下来可能要按专题重点突破了,明天队里开个会确定下大家的主攻方向再说A.PrimeDeletion因为\(13\)和\(31\)都......
  • Educational Codeforces Round 23 A - F
    EducationalCodeforcesRound23目录EducationalCodeforcesRound23A-TreasureHuntB-MakesAndTheProductC-ReallyBigNumbersD-ImbalancedArrayE-ChoosingTheCommanderF-MEXQueriesA-TreasureHunt往四个方向走,每次操作会变动横坐标和纵坐标,横纵坐标......
  • Educational Codeforces Round 154 (Rated for Div. 2)(A—C)
    A.PrimeDeletion思路:从1到9,每个数后面都可以加一个数构成一个含有两个数的质数,只需要从s[1]~s[9]中找到一个数与s[0]构成质数即可代码实现/*******************************|Author:CHC|Problem:A.PrimeDeletion|Contest:Codeforces-EducationalCodeforcesR......
  • 【题解】Educational Codeforces Round 153(CF1860)
    每次打都想感叹一句,Educational名不虚传。A.NotaSubstring题目描述:有\(t\)组数据,对于每一组数据,你需要判断能否构造一个只由左右括号组成且长度为已经给定字符串的\(2\)倍且已经给定的字符串不是子串的合法字符串。注:合法的字符串是左右括号能完全匹配的字符串。如果能,......
  • Educational Codeforces Round 113
    稳定发挥4题A题文件输出没去掉WA了一发B题特殊情况没判到,WA了好几发,中间还交到D题去了C题简单判断一下无解,然后组合数求一下就行D题其实也挺简单的,考虑严格夹在两条竖线之间的点(不包括边界),如果它们不是在同一水平线上,则必然大于Manhattan距离,而且两个点对之间要么是x方向走多......
  • Educational Codeforces Round 154 (Rated for Div. 2)
    感觉edu的题目都比较有新意;A.PrimeDeletion题意:给定长度为9的数,且1-9每个数字出现一次,求按照原定顺序选几个数组成的质数(起码选择两个);下意识写了一个dfs,过了;1#include<bits/stdc++.h>2usingnamespacestd;3intread(){4charch=getchar();intx=0,f=1;5......
  • Educational Codeforces Round 123
    A.DoorsandKeys#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){strings;cin>>s;map<char,int>pos;for(inti=0;i<6;i++)pos[s[i]]=i;if(pos['r&......
  • Educational Codeforces Round 15 A - E
    EducationalCodeforcesRound15目录EducationalCodeforcesRound15A-MaximumIncreaseB-PowersofTwoC-CellularNetworkD-RoadtoPostOfficeE.AnalysisofPathesinFunctionalGraphA-MaximumIncrease一段上升子数组\([l,r]\)最大化\(r-l+1\),我们......