Codeforces 960G Bandit Blues

题目大意

链接:CF960G

给定正整数\(n\),表示有\(1\sim n\)个元素,求有多少种全排列满足:

从左往右按贪心原则去最大值,共取出\(a\)个元素;从右往左按贪心原则去最大值,共取出\(b\)个元素。

答案对\(998244353\)取模,数据满足\(1\leq n\leq 10^5,1\leq a,b\leq n\)。

题目分析

我们先考虑一个递推做法。

设\(f(i,j)\)表示\(1\sim i\),按贪心原则会取\(j\)个数的方案数。

若第\(i\)个数为\(i\),则取的数一定会\(+1\),对答案的贡献为\(f(i-1,j-1)\)。

若第\(i\)个数不为\(i\),则取的数一定不变,对答案的贡献为\((i-1)\cdot f(i-1,j)\)。

由此,我们有了递推公式:
\[
f(i,j)=f(i-1,j-1)+(i-1)\cdot f(i-1,j)这是一个$O(n^2)$的式子,答案为
\]

这是一个\(O(n^2)\)的式子,答案为
\[
\sum_{i=1}^{n}C_{n-1}^{i-1}\cdot f(i-1,a-1)\cdot f(n-i,b-1)
\]

意思是,枚举最大值所在的位置\(i\);

首先,需要把剩下的数分为两份,一份大小\(i-1\),另一份大小为\(n-i\),方案数为\(C_{n-1}^{i-1}\)。

前\(i-1\)个数需要按贪心原则取\(a-1\)个,方案数\(f(i-1,a-1)\);

后\(n-i\)个数需要按贪心原则取\(b-1\)个,方案数\(f(n-i,b-1)\)。



看着上面的递推公式,你有没有恍然大悟:欸,这不就是无符号的第一类Stirling数吗????

所以,我们有
\[
\begin{split}
ans&=\sum_{i=1}^{n}C_{n-1}^{i-1}\cdot f(i-1,a-1)\cdot f(n-i,b-1)\&=\sum_{i=1}^nC_{n-1}^{i-1}\cdot \begin{bmatrix}i-1\\a-1\end{bmatrix}\cdot \begin{bmatrix}n-i\\b-1\end{bmatrix}\cdot \binom{n-1}{i-1}
\end{split}
\]

此时,我们可以把对\(f\)的理解回归到无符号的第一类Stirling数上,即:

用\(i-1\)个数构成\(a-1\)个圆排列,用\(n-i\)个数构成\(b-1\)个圆排列的方案数。

整体来说,就是:用\(n-1\)个数构成\(a+b-2\)个圆排列,并把圆排列分为两部分,其中一部分大小为\(a-1\)的方案数,即:
\[
ans=\begin{bmatrix}n-1\\a+b-2\end{bmatrix}\cdot \binom{a+b-2}{a-1}
\]

对于\(\begin{bmatrix}n-1\\a+b-2\end{bmatrix}\),可以使用分治+NTT计算。

代码实现

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#define MAXN 0x7fffffff
typedef long long LL;
const int N=400005,mod=998244353;
using namespace std;
inline int Getint(){register int x=0,f=1;register char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}return x*f;}
int ksm(int x,int k){
    int ret=1;
    while(k){
        if(k&1)ret=(LL)ret*x%mod;
        x=(LL)x*x%mod,k>>=1;
    }
    return ret;
}
int rev[N];
void NTT(int *a,int x,int K){
    int n=(1<<x);
    for(int i=0;i<n;i++)if(i<rev[i])swap(a[i],a[rev[i]]);
    for(int i=1;i<n;i<<=1){
        int tmp=i<<1,wn=ksm(3,(mod-1)/tmp);
        if(K==-1)wn=ksm(wn,mod-2);
        for(int j=0;j<n;j+=tmp){
            int w=1;
            for(int k=0;k<i;k++,w=(LL)w*wn%mod){
                int x=a[j+k],y=(LL)w*a[i+j+k]%mod;
                a[j+k]=(x+y)%mod;a[i+j+k]=(x-y+mod)%mod;
            }
        }
    }
    if(K==-1){
        int inv=ksm(n,mod-2);
        for(int i=0;i<n;i++)a[i]=(LL)a[i]*inv%mod;
    }
}
void Binary(int *a,int l,int r){
    if(l==r)return a[0]=l,a[1]=1,void();
    int mid=l+r>>1;
    int f[N],g[N];
    memset(f,0,(r-l+1)<<3),memset(g,0,(r-l+1)<<3);
    Binary(f,l,mid),Binary(g,mid+1,r);
    int x=ceil(log2(r-l+2));
    for(int i=0;i<(1<<x);i++)rev[i]=(rev[i>>1]>>1)|((i&1)<<x-1);

    NTT(f,x,1),NTT(g,x,1);
    for(int i=0;i<(1<<x);i++)a[i]=(LL)f[i]*g[i]%mod;
    NTT(a,x,-1);
}
int C(int n,int m){
    if(n<m)return 0;
    int ans=1;
    for(int i=n-m+1;i<=n;i++)ans=(LL)ans*i%mod;
    for(int i=1;i<=m;i++)ans=(LL)ans*ksm(i,mod-2)%mod;
    return ans;
}
int a[N];
int main(){
    int n=Getint()-1,A=Getint(),B=Getint();
    if(!n)cout<<(A==1&&B==1),exit(0);
    if(!A||!B)cout<<0,exit(0);

    Binary(a,0,n-1);
    cout<<(LL)a[A+B-2]*C(A+B-2,A-1)%mod;
    return 0;
}

原文地址:https://www.cnblogs.com/Emiya-wjk/p/10017662.html

时间: 2024-08-12 02:49:58

Codeforces 960G Bandit Blues的相关文章

@codeforces - [email&#160;protected] Bandit Blues

目录 @[email protected] @[email protected] @part - [email protected] @part - [email protected] @accepted [email protected] @[email protected] @[email protected] 求有多少个长度为 n 的排列,从左往右遍历有 a 个数比之前遍历的所有数都大,从右往左遍历有 b 个数比之前遍历的所有数都大. 模 998244323. input 一行三个整数 n

Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)G - Bandit Blues

题意:求满足条件的排列,1:从左往右会遇到a个比当前数大的数,(每次遇到更大的数会更换当前数)2.从右往左会遇到b个比当前数大的数. 题解:1-n的排列,n肯定是从左往右和从右往左的最后一个数. 考虑\(S(n,m)\)是1-n排列中从左往右会遇到m个比当前数大的数,考虑把1放在最左边,即\(S(n-1,m-1)\),考虑1不在最左边,有n-1个位置,1不可能会更换\((n-1)*S(n,m)\).即\(S(n,m)=S(n-1,m-1)+(n-1)*S(n-1,m)\) \(S(n,m)\)即

Codeforces960G Bandit Blues

Problem Codeforces Solution 先找到序列中 \(n\) 的位置,那么在 \(n\) 之前必须有 \(a-1\) 个前缀最大值,之后有 \(b-1\) 个后缀最大值. 设 \(f[i][j]\) 表示长度为 \(i\) 的排列,有 \(j\) 个前缀最大值的方案数. 那么\(ans=\sum_{i=1}^n f[i-1][a-1]\times f[n-i][b-1]\times \binom n {i-1}\) 枚举最小值的位置,那么当且仅当它在第一个位置上时才会贡献一个

组合计数小练

组合数学什么的,最有趣了呢-- [51nod 1251] Fox序列的数量 题意 求满足以下条件的序列数目: 序列长度为 $ n $ ,每个元素都属于 $ [1,m] \cap Z $ : 这个序列单调不降: 这个序列出现次数最多的数是唯一的. 数据范围: $ 1≤n,m≤100000 $ ,答案对 $ 1e9+7 $ 取模 题解 先枚举出现次数最多的数的出现次数 $ k $ ,我们要计算的是 $ x_1+x_2+...+x_{m-1}=n-k, ; x_i≤k-1 $ 的非负整数解数目. 可以

学习总结:斯特林数( Stirling number )

基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{bmatrix}. $$ 第二类斯特林数:将$n$个元素分成$k$个非空集合的方案数.记作 $$ \begin{Bmatrix} n \\ k \end{Bmatrix}. $$ 根据定义,我们有 $$ \sum_{k=0}^n \begin{bmatrix} n \\ k \end{bmatrix

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st