Skip to content

Commit

Permalink
add: contest biweekly 117
Browse files Browse the repository at this point in the history
  • Loading branch information
binacs committed Dec 1, 2023
1 parent cc70e9e commit 176f873
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions math/combinatorics/inclusion-exclusion-principle.md
Original file line number Diff line number Diff line change
Expand Up @@ -1285,4 +1285,115 @@ int main() {

<br>

* * *

> [!NOTE] **[LeetCode 2930. 重新排列后包含指定子字符串的字符串数目](https://leetcode.cn/problems/number-of-strings-which-can-be-rearranged-to-contain-substring/)**
>
> 题意: TODO
> [!TIP] **思路**
>
> DP 或 容斥
<details>
<summary>详细代码</summary>
<!-- tabs:start -->

##### **C++ 容斥**

```cpp
class Solution {
public:
// 初步思想: 直接 dp
//
// 进阶 考虑容斥 直接排除"不好"的即可
using LL = long long;
const static int N = 1e5 + 10, MOD = 1e9 + 7;

// 容斥
LL qpow(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1)
ret = ret * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ret;
}
int stringCount(int n) {
// 反向考虑
return ((qpow(26, n) // 所有字符串
- qpow(25, n - 1) * (25 * 3 + n) % MOD // 不含至少某一种 (中间有重复统计)
+ qpow(24, n - 1) * (24 * 3 + n * 2) % MOD // 不含至少其中两种
- qpow(23, n - 1) * (23 + n)) % MOD + MOD) % MOD; // 都不含
}
};
```
##### **C++ DP**
```cpp
class Solution {
public:
// 初步思想: 直接 dp
//
// 进阶 考虑容斥 直接排除"不好"的即可
using LL = long long;
const static int N = 1e5 + 10, MOD = 1e9 + 7;
LL f[N][2][3][2]; // 考虑前 i 个字符: l 个数 0/>=1 e个数0/1/>=2 ...
void madd(LL & a, LL b) {
a = (a + b) % MOD;
}
int stringCount(int n) {
memset(f, 0, sizeof f);
f[0][0][0][0] = 1;
// 正推更好实现
for (int i = 0; i < n; ++ i ) {
for (int x = 0; x < 2; ++ x )
for (int y = 0; y < 3; ++ y )
for (int z = 0; z < 2; ++ z ) {
LL t = f[i][x][y][z];
madd(f[i + 1][x][y][z], t * 23 % MOD);
madd(f[i + 1][min(x + 1, 1)][y][z], t);
madd(f[i + 1][x][min(y + 1, 2)][z], t);
madd(f[i + 1][x][y][min(z + 1, 1)], t);
}
}
// 逆推
for (int i = 1; i <= n; ++ i ) {
for (int x = 0; x < 2; ++ x )
for (int y = 0; y < 3; ++ y )
for (int z = 0; z < 2; ++ z ) {
LL & t = f[i][x][y][z];
madd(t, f[i - 1][x][y][z] * 23 % MOD);
madd(t, f[i - 1][max(x - 1, 0)][y][z]);
madd(t, f[i - 1][x][max(y - 1, 0)][z]);
madd(t, f[i - 1][x][y][max(z - 1, 0)]);
}
}
return f[n][1][2][1];
}
};
```

##### **Python**

```python

```

<!-- tabs:end -->
</details>

<br>

* * *

0 comments on commit 176f873

Please sign in to comment.