-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandard.cpp
More file actions
204 lines (171 loc) · 7.54 KB
/
standard.cpp
File metadata and controls
204 lines (171 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
class MinimumDistinctChainSolver {
public:
static constexpr long long kMod = 1000000007LL;
// Fast modular exponentiation.
static long long modPow(long long base, long long exponent) {
long long result = 1 % kMod;
base %= kMod;
while (exponent > 0) {
if ((exponent & 1LL) != 0) {
result = (result * base) % kMod;
}
base = (base * base) % kMod;
exponent >>= 1LL;
}
return result;
}
// Modular inverse via Fermat little theorem.
static long long modInverse(long long value) {
return modPow(value, kMod - 2);
}
// Builds factorial and inverse factorial tables.
static void buildFactorials(int maxValue, std::vector<long long>& factorial, std::vector<long long>& inverseFactorial) {
factorial.assign(maxValue + 1, 1LL);
inverseFactorial.assign(maxValue + 1, 1LL);
for (int valueIndex = 1; valueIndex <= maxValue; ++valueIndex) {
factorial[valueIndex] = (factorial[valueIndex - 1] * valueIndex) % kMod;
}
inverseFactorial[maxValue] = modInverse(factorial[maxValue]);
for (int valueIndex = maxValue; valueIndex >= 1; --valueIndex) {
inverseFactorial[valueIndex - 1] = (inverseFactorial[valueIndex] * valueIndex) % kMod;
}
}
// Returns d = (max run length in pattern) + 1.
static int computeMinimumDistinctRequired(const std::string& pattern) {
if (pattern.empty()) {
return 1;
}
int bestRunLength = 1;
int currentRunLength = 1;
for (int positionIndex = 1; positionIndex < static_cast<int>(pattern.size()); ++positionIndex) {
if (pattern[positionIndex] == pattern[positionIndex - 1]) {
++currentRunLength;
bestRunLength = std::max(bestRunLength, currentRunLength);
} else {
currentRunLength = 1;
}
}
return bestRunLength + 1;
}
// Counts valid rank arrays over {1..alphabetSize}, unused ranks allowed.
static long long countValidRankArraysAllowUnused(const std::string& pattern, int alphabetSize) {
if (alphabetSize <= 0) {
return 0;
}
std::vector<long long> waysEndingWithRank(alphabetSize + 1, 0LL);
std::vector<long long> nextWaysEndingWithRank(alphabetSize + 1, 0LL);
for (int rankValue = 1; rankValue <= alphabetSize; ++rankValue) {
waysEndingWithRank[rankValue] = 1;
}
for (char inequalitySymbol : pattern) {
if (inequalitySymbol == '<') {
long long runningPrefixSum = 0;
for (int rankValue = 1; rankValue <= alphabetSize; ++rankValue) {
nextWaysEndingWithRank[rankValue] = runningPrefixSum;
runningPrefixSum += waysEndingWithRank[rankValue];
if (runningPrefixSum >= kMod) {
runningPrefixSum -= kMod;
}
}
} else {
long long runningSuffixSum = 0;
for (int rankValue = alphabetSize; rankValue >= 1; --rankValue) {
nextWaysEndingWithRank[rankValue] = runningSuffixSum;
runningSuffixSum += waysEndingWithRank[rankValue];
if (runningSuffixSum >= kMod) {
runningSuffixSum -= kMod;
}
}
}
waysEndingWithRank.swap(nextWaysEndingWithRank);
}
long long totalWays = 0;
for (int rankValue = 1; rankValue <= alphabetSize; ++rankValue) {
totalWays += waysEndingWithRank[rankValue];
if (totalWays >= kMod) {
totalWays -= kMod;
}
}
return totalWays;
}
// Computes C(largeNumber, smallK) mod kMod.
static long long computeBinomialLargeNSmallK(long long largeNumber, int smallK, const std::vector<long long>& inverseFactorial) {
if (smallK < 0 || largeNumber < smallK) {
return 0;
}
long long numeratorProduct = 1;
for (int termIndex = 0; termIndex < smallK; ++termIndex) {
numeratorProduct = (numeratorProduct * (largeNumber - termIndex)) % kMod;
}
return (numeratorProduct * inverseFactorial[smallK]) % kMod;
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int testCaseCount = 0;
std::cin >> testCaseCount;
for (int testCaseIndex = 0; testCaseIndex < testCaseCount; ++testCaseIndex) {
int patternLength = 0;
long long maxBrightness = 0;
std::cin >> patternLength >> maxBrightness;
std::string pattern;
std::cin >> pattern;
if (static_cast<int>(pattern.size()) != patternLength) {
patternLength = static_cast<int>(pattern.size());
}
const int minimumDistinctRequired = MinimumDistinctChainSolver::computeMinimumDistinctRequired(pattern);
if (maxBrightness < minimumDistinctRequired) {
std::cout << 0;
if (testCaseIndex != testCaseCount - 1) std::cout << "\n";
continue;
}
std::vector<long long> factorial;
std::vector<long long> inverseFactorial;
MinimumDistinctChainSolver::buildFactorials(minimumDistinctRequired, factorial, inverseFactorial);
auto binomialCoefficient = [&](int chooseCount) -> long long {
return factorial[minimumDistinctRequired]
* inverseFactorial[chooseCount] % MinimumDistinctChainSolver::kMod
* inverseFactorial[minimumDistinctRequired - chooseCount] % MinimumDistinctChainSolver::kMod;
};
std::vector<long long> validCountAllowUnused(minimumDistinctRequired + 1, 0LL);
for (int alphabetSize = 1; alphabetSize <= minimumDistinctRequired; ++alphabetSize) {
validCountAllowUnused[alphabetSize] =
MinimumDistinctChainSolver::countValidRankArraysAllowUnused(pattern, alphabetSize);
}
long long validCountUseAllSymbols = 0;
for (int missingSymbolCount = 0; missingSymbolCount <= minimumDistinctRequired; ++missingSymbolCount) {
const int remainingAlphabetSize = minimumDistinctRequired - missingSymbolCount;
const long long inclusionExclusionTerm =
binomialCoefficient(missingSymbolCount)
* validCountAllowUnused[remainingAlphabetSize] % MinimumDistinctChainSolver::kMod;
if ((missingSymbolCount & 1) != 0) {
validCountUseAllSymbols -= inclusionExclusionTerm;
if (validCountUseAllSymbols < 0) {
validCountUseAllSymbols += MinimumDistinctChainSolver::kMod;
}
} else {
validCountUseAllSymbols += inclusionExclusionTerm;
if (validCountUseAllSymbols >= MinimumDistinctChainSolver::kMod) {
validCountUseAllSymbols -= MinimumDistinctChainSolver::kMod;
}
}
}
const long long chooseBrightnessLevels = MinimumDistinctChainSolver::computeBinomialLargeNSmallK(
maxBrightness,
minimumDistinctRequired,
inverseFactorial
);
const long long answer =
chooseBrightnessLevels * validCountUseAllSymbols % MinimumDistinctChainSolver::kMod;
std::cout << answer;
if (testCaseIndex != testCaseCount - 1) {
std::cout << "\n";
}
}
return 0;
}