题面
来源:USACO 官网。模型收到的是同一份题面的文字。
You have a long string $S$ of Ms and Os and an integer $K \geq 1$. Count the number of ways of ways to decompose $S$ into subsequences such that each subsequence is MOOOO....O with exactly $K$ Os, modulo $10^9+7$.
Since the string is very long, you are not given it explicitly. Instead, you are given an integer $L$ ($1 \leq L \leq 10^{18}$), and a string $T$ of length $N$ ($1 \leq N \leq 10^6$). The string $S$ is the concatenation of $L$ copies of the string $T$.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $K$, $N$, and $L$.The second line contains the string $T$ of length $N$. Every character is either an M or an O.
It is guaranteed that the number of decompositions of $S$ is nonzero.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the number of decompositions of string $S$, modulo $10^9+7$.SAMPLE INPUT:
2 6 1 MOOMOO
SAMPLE OUTPUT:
1
The only way to decompose $S$ into MOOs is to let the first three characters form a MOO and the last three characters form another MOO.
SAMPLE INPUT:
2 6 1 MMOOOO
SAMPLE OUTPUT:
6
There are six distinct ways to decompose the string into subsequences (uppercase letters form one MOO, lowercase letters form another):
- MmOOoo
- MmOoOo
- MmOooO
- MmoOOo
- MmoOoO
- MmooOO
SAMPLE INPUT:
1 4 2 MMOO
SAMPLE OUTPUT:
4
SAMPLE INPUT:
1 4 100 MMOO
SAMPLE OUTPUT:
976371285
Make sure to take the answer modulo $10^9+7$.
SCORING:
- Inputs 5-7: $K=1$, $L = 1$
- Inputs 8-10: $K=2$, $N\leq 1000$, $L = 1$
- Inputs 11-13: $K=1$
- Inputs 14-19: $L = 1$
- Inputs 20-25: No additional constraints.
Problem credits: Dhruv Rohatgi
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 7.5k平均输出 token(含思考) ↓
- 21.4输出速度 tokens/s ↑
- 292 秒延迟 ↓
- 0.08×CPU 时间 / 官方解 ↓
- 不确定 3时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 13.6k平均输出 token(含思考) ↓
- 192.2输出速度 tokens/s ↑
- 69 秒延迟 ↓
- 0.05×CPU 时间 / 官方解 ↓
- 不确定 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 14.8k平均输出 token(含思考) ↓
- 165.1输出速度 tokens/s ↑
- 82 秒延迟 ↓
- 0.06×CPU 时间 / 官方解 ↓
- 不确定 3时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long K, N, L; string T; cin >> K >> N >> L >> T; long long fact = 1; for (long long i = 2; i <= K; ++i) { fact = fact * i % MOD; } long long invFact = modpow(fact, MOD - 2); long long coefficient = 1; long long degree = 0; for (char c : T) { if (c == 'M') { coefficient = coefficient * invFact % MOD; degree += K; } else { coefficient = coefficient * (degree % MOD) % MOD; --degree; } } cout << modpow(coefficient, L) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long K, N, L; string T; cin >> K >> N >> L >> T; long long factK = 1; for (long long i = 2; i <= K; ++i) { factK = factK * i % MOD; } long long m = 0, o = 0, product = 1; for (char c : T) { if (c == 'M') { ++m; } else { long long factor = K * m - o; product = product * (factor % MOD) % MOD; ++o; } } long long denominator = modpow(factK, m); long long oneCopy = product * modpow(denominator, MOD - 2) % MOD; cout << modpow(oneCopy, L) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long K, N, L; string T; cin >> K >> N >> L >> T; long long remaining = 0; long long product = 1; long long m = 0; for (char c : T) { if (c == 'M') { remaining += K; ++m; } else { product = product * (remaining % MOD) % MOD; --remaining; } } long long factK = 1; for (long long i = 2; i <= K; ++i) { factK = factK * i % MOD; } long long oneCopy = product * modpow(modpow(factK, m), MOD - 2) % MOD; cout << modpow(oneCopy, L) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long K, L; int N; cin >> K >> N >> L; string T; cin >> T; long long mCount = 0, oCount = 0; long long prod = 1; long long Kmod = K % MOD; for (char c : T) { if (c == 'M') { ++mCount; } else { long long factor = (Kmod * (mCount % MOD) % MOD - (oCount % MOD) + MOD) % MOD; prod = prod * factor % MOD; ++oCount; } } // A decomposition is possible only if the total number of Os is K times // the total number of Ms. __int128 lhs = (__int128)K * mCount; if (lhs != (__int128)oCount) { cout << 0 << '\n'; return 0; } int kk = (int)K; long long fact = 1; for (int i = 2; i <= kk; ++i) { fact = fact * i % MOD; } long long invFact = modpow(fact, MOD - 2); // Ways for one copy of T. long long waysT = prod * modpow(invFact, mCount) % MOD; // The copies are independent because each period uses all capacity before // the next period starts. long long answer = modpow(waysT, L); cout << answer << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { a %= MOD; long long r = 1; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long Kll, L; int N; cin >> Kll >> N >> L; string T; cin >> T; long long M = 0, O = 0; for (char c : T) { if (c == 'M') ++M; else ++O; } // A valid decomposition requires O = K * M. if (M == 0 || Kll < 1 || Kll > O || O != Kll * M) { cout << 0 << '\n'; return 0; } int K = (int)Kll; int MT = (int)M; long long fact = 1; for (int i = 1; i <= K; ++i) { fact = fact * i % MOD; } long long invFact = modpow(fact, MOD - 2); long long numerator = 1; long long r = 0; long long p = 0; for (char c : T) { if (c == 'O') { ++p; } else { ++r; long long A = Kll * r - p; if (A < K) { cout << 0 << '\n'; return 0; } long long x = A % MOD; for (int t = 0; t < K; ++t) { long long term = x - t; if (term < 0) term += MOD; numerator = numerator * term % MOD; } } } long long perCopy = numerator * modpow(invFact, MT) % MOD; long long answer = modpow(perCopy, L); cout << answer << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; long long modpow(long long a, long long e) { long long r = 1 % MOD; a %= MOD; while (e > 0) { if (e & 1) r = r * a % MOD; a = a * a % MOD; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long K, L; int N; cin >> K >> N >> L; string T; cin >> T; long long M = 0; for (char ch : T) { if (ch == 'M') ++M; } long long O = N - M; // A valid decomposition must use every character: // total Os = K * total Ms. if (K > N || K * M != O) { cout << 0 << '\n'; return 0; } long long available = 0; long long numerator = 1; long long kMod = K % MOD; for (char ch : T) { if (ch == 'M') { available += kMod; if (available >= MOD) available -= MOD; } else { numerator = numerator * available % MOD; --available; if (available < 0) available += MOD; } } long long factK = 1; for (long long i = 2; i <= K; ++i) { factK = factK * i % MOD; } long long invFactK = modpow(factK, MOD - 2); long long denominatorInverse = modpow(invFactK, M); long long perPeriod = numerator * denominatorInverse % MOD; long long answer = modpow(perPeriod, L); cout << answer << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int64 MOD = 1'000'000'007; int64 modPow(int64 base, int64 exp) { int64 result = 1; while (exp > 0) { if (exp & 1) result = result * base % MOD; base = base * base % MOD; exp >>= 1; } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64 K, L; int N; cin >> K >> N >> L; string T; cin >> T; int64 seenM = 0, seenO = 0; int64 numerator = 1; for (char c : T) { if (c == 'M') { ++seenM; } else { ++seenO; int64 choices = K * seenM - (seenO - 1); choices %= MOD; if (choices < 0) choices += MOD; numerator = numerator * choices % MOD; } } // A nonzero decomposition requires #O(T) = K * #M(T). if (seenO != K * seenM) { cout << 0 << '\n'; return 0; } int64 factorial = 1; for (int64 i = 1; i <= K; ++i) { factorial = factorial * i % MOD; } int64 denominator = modPow(factorial, seenM); int64 oneCopy = numerator * modPow(denominator, MOD - 2) % MOD; cout << modPow(oneCopy, L) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int64 MOD = 1'000'000'007; int64 modPow(int64 base, int64 exp) { int64 result = 1; while (exp > 0) { if (exp & 1) result = result * base % MOD; base = base * base % MOD; exp >>= 1; } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64 K, L; int N; cin >> K >> N >> L; string T; cin >> T; int64 countM = 0, countO = 0; int64 product = 1; for (char c : T) { if (c == 'M') { ++countM; } else { // Number of unused labeled O-slots belonging to earlier Ms. int64 choices = K * countM - countO; product = product * (choices % MOD) % MOD; ++countO; } } // A valid decomposition implies countO = K * countM and K <= N. int64 factorial = 1; for (int64 i = 2; i <= K; ++i) { factorial = factorial * i % MOD; } int64 inverseFactorial = modPow(factorial, MOD - 2); int64 perCopy = product * modPow(inverseFactorial, countM) % MOD; cout << modPow(perCopy, L) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using uint64 = unsigned long long; constexpr int64 MOD = 1'000'000'007LL; int64 modPow(int64 base, uint64 exponent) { int64 result = 1; while (exponent > 0) { if (exponent & 1ULL) { result = result * base % MOD; } base = base * base % MOD; exponent >>= 1ULL; } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64 K; int N; uint64 L; cin >> K >> N >> L; string T; cin >> T; int64 mSeen = 0; int64 oBefore = 0; int64 numerator = 1; for (char c : T) { if (c == 'O') { ++oBefore; } else { ++mSeen; // This M contributes C(K * mSeen - oBefore, K). // Accumulate the numerators of these binomial coefficients. int64 available = K * mSeen - oBefore; for (int64 q = 0; q < K; ++q) { int64 factor = (available - q) % MOD; if (factor < 0) factor += MOD; numerator = numerator * factor % MOD; } } } int64 factorialK = 1; for (int64 i = 2; i <= K; ++i) { factorialK = factorialK * i % MOD; } // Divide by (K!)^mSeen. int64 inverseFactorialK = modPow(factorialK, MOD - 2); int64 inverseDenominator = modPow(inverseFactorialK, mSeen); int64 waysPerCopy = numerator * inverseDenominator % MOD; cout << modPow(waysPerCopy, L) << '\n'; return 0; }