跳到正文

题解

P12028 [USACO25OPEN] Moo Decomposition G 题解

yuyc约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年5月23日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟
获赞
2
题目
P12028

版本

  1. v1当前

其实从前往后做也是可以的。

解析

先不考虑 LL。

一个很自然的想法是从前往后让每个 O\texttt{O} 跟一个在它前面的 M\texttt{M} 配对。问题在于 KK 的限制难以满足。

所以先满足 KK 的限制,对于每个 M\texttt{M},在它后面预留 KK 个空位用来放 O\texttt{O}。这样对于每个 O\texttt{O},只需要随便找一个在它前面的 M\texttt{M} 的一个空位放进去就好,这一步的方案数就是当前空位个数,利用乘法原理就可以求出总方案数。

但是我们发现这样放的话 O\texttt{O} 的顺序是乱的,实际上 O\texttt{O} 的顺序应该是按照其在 SS 中的位置来排的,所以对于每个 M\texttt{M},需要除一个排列数。

然后来考虑 LL。

如果 TT 无解,要么是当前的 O\texttt{O} 找不到一个合适的 M\texttt{M} 匹配,要么是 O\texttt{O} 不够拿来匹配。对于前者,不管后面拼多少个 TT 都匹配不了前面的 O\texttt{O};对于后者,不管后面拼多少个 TT 都拿不出多余的 O\texttt{O}。也就是说,一个无解的 TT,不管拼接多少次,它都是无解的。

如果 TT 有解,考虑拼接后前一段的 M\texttt{M} 会不会匹配上后一段的 O\texttt{O},答案是不会,因为这样的话前一段就会有 O\texttt{O} 匹配不上了。

综上,每一段对答案做的贡献是独立且相同的,最终要求的答案就是单段方案数的 LL 次幂。

代码

C++
#include <bits/stdc++.h>
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define mid ((l + r) >> 1)
#define eps 0.000001
using namespace std;
typedef __int128 i128;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int> pii;
const int N = 1e5 + 5, M = 2e5 + 5,base1 = 13331,base2 = 131,mod = 1e9 + 7;
int qmi(int a,ll b){
	int res = 1;
	while(b){
		if(b & 1) res = 1ll * res * a % mod;
		b >>= 1;
		a = 1ll * a * a % mod;
	}
	return res;
} 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	ll k,n,l;
	cin>>k>>n>>l;
	int fac = 1;
	for(int i=1;i<=k;i++){
		fac = 1ll * i * fac % mod;
	}
	string s;
	cin>>s;
	int now = 0,res = 1,cntm = 0;
	for(int i=0;i<s.size();i++){
		if(s[i] == 'M') now += k,cntm++;
		else{
			res = 1ll * res * now % mod;
			now--;
		}
	}
	assert(now == 0);
//	cout<<s.size()<<" "<<res<<" "<<fac<<" "<<cntm<<'\n';
	cout<<qmi(1ll * res * qmi(qmi(fac,cntm),mod - 2) % mod,l);
	return 0;
}
原文