Strivore
1. 前言
这道题还挺难的
2.题解
(1).排列组合部分
好心人的提示
在字符串 s s s 中插入 n n n 个小写字母,就相当于在 n + s . l e n g t h n+s.length n+s.length 个格子里面填入小写字母,要求其存在为 s s s 的子序列(不一定要连续)。
先确定 s s s 第一个字母所在的位置,假设在位置 i i i 处,( 0 < i ≤ n + 1 0 < i \leq n+1 0<i≤n+1), i i i 前面的空格每一个都有 26 26 26 种情况,总共 ( 2 6 i − 1 ) (26^{i-1}) (26i−1)种情况, i i i 后面的空格,先选 s . l e n g t h − 1 s.length-1 s.length−1 各格子放入 s 的其他字母,有 ( C k − i l e n − 1 ) (C_{k-i}^{len-1}) (Ck−ilen−1)种方法,然后每个 s 串字母后面不选相同的,用于去重,有 ( 2 5 k − i − l e n + 1 ) (25^{k-i-len+1} ) (25k−i−len+1) 种情况。
不要问我 T a Ta Ta 在讲什么,我也没太看懂,但只要知道思路就行了
为了证明其正确性,我们仅需要证明这样的方法 不重不漏
不重
十分简单,第二个数列中的第 i i i 个空一定不是 s [ 1 ] s[1] s[1],所以一定不会重复
不漏
灵感来自 yxc大巨佬,我不是在打广告啊
我们可以利用集合的思想来解决
一目了然,每一个区域对应着枚举的
i
i
i所对应的方案数,所以没有漏掉任何情况
3.实现
60 p t s 60pts 60pts
逆元加上快速幂乱搞
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define ULL unsigned long long
using namespace std;
template <typename T> int read (T &x) {x = 0; T f = 1;char tem = getchar ();while (tem < '0' || tem > '9') {if (tem == '-') f = -1;tem = getchar ();}while (tem >= '0' && tem <= '9') {x = (x << 1) + (x << 3) + tem - '0';tem = getchar ();}x *= f; return 1;}
template <typename T> void write (T x) {if (x < 0) {x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
const int Maxn = 1e6;
const LL Mod = 1e9 + 7;
int n;
LL fac[Maxn * 2 + 5], w1[Maxn * 2 + 5], w2[Maxn * 2 + 5], inv_fac[Maxn * 2 + 5];
string s;
void exgcd (LL a, LL b, LL &x, LL &y) {
if (b == 0) {
x = 1; y = 0;
return;
}
exgcd (b, a % b, y, x);
y -= a / b * x;
}
LL inv (LL a) {
LL x, y;
exgcd (a, Mod, x, y);
x = ((x % Mod) + Mod) % Mod;
return x;
}
LL C (LL a, LL b) {
return fac[b] * inv_fac[a] % Mod * inv_fac[b - a] % Mod;
}
int main () {
cin >> n >> s;
fac[0] = 1; w1[0] = 1; w2[0] = 1;
for (int i = 1; i <= n + s.length () + 1; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= Mod;
w1[i] = w1[i - 1] * 26;
w1[i] %= Mod;
w2[i] = w2[i - 1] * 25;
w2[i] %= Mod;
}
inv_fac[n + s.length ()] = inv (fac[n + s.length()]);
for(int i = n + s.length () - 1; i >= 0; i--)
inv_fac[i] = inv_fac[i + 1] * (i + 1) % Mod;
LL ans = 0;
for (int i = 1; i <= n + 1; i++) {
ans += w1[i - 1] * C (s.length () - 1, n + s.length () - i) % Mod * w2[n - i + 1] % Mod;
ans %= Mod;
}
cout << ans;
return 0;
}
发现 n ∈ [ 1 , 1 e 6 ] n \in [1, 1e6] n∈[1,1e6],要卡 O ( n ∗ l o g 2 n ) O(n * log_2n) O(n∗log2n),所以只有打表打出幂。
但是还有一个问题,阶乘的逆元怎么求呢?
引入一个 N B NB NB 的东西,阶乘逆元
a
x
≡
1
(
m
o
d
p
)
(
a
=
i
!
)
ax \equiv 1 \pmod {p} (a = i!)
ax≡1(modp)(a=i!)
a
i
∗
y
≡
1
(
m
o
d
p
)
\frac {a}{i} * y \equiv 1 \pmod p
ia∗y≡1(modp)
令
y
=
i
x
y = ix
y=ix
即证
a
i
∗
i
x
≡
1
(
m
o
d
p
)
\frac {a} {i} *ix \equiv 1 \pmod p
ia∗ix≡1(modp)
即
(
a
−
1
)
!
∗
i
x
≡
1
(
m
o
d
p
)
(a-1)! *ix \equiv 1 \pmod p
(a−1)!∗ix≡1(modp)
即
a
!
x
≡
1
(
m
o
d
p
)
a!x \equiv1 \pmod p
a!x≡1(modp)
所以 ( a − 1 ) ! (a - 1)! (a−1)! 的逆元为 i x ix ix
参考代码
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define ULL unsigned long long
using namespace std;
template <typename T> int read (T &x) {x = 0; T f = 1;char tem = getchar ();while (tem < '0' || tem > '9') {if (tem == '-') f = -1;tem = getchar ();}while (tem >= '0' && tem <= '9') {x = (x << 1) + (x << 3) + tem - '0';tem = getchar ();}x *= f; return 1;}
template <typename T> void write (T x) {if (x < 0) {x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
const int Maxn = 1e6;
const LL Mod = 1e9 + 7;
int n;
LL fac[Maxn * 2 + 5], w1[Maxn * 2 + 5], w2[Maxn * 2 + 5], inv_fac[Maxn * 2 + 5];
string s;
void exgcd (LL a, LL b, LL &x, LL &y) {
if (b == 0) {
x = 1; y = 0;
return;
}
exgcd (b, a % b, y, x);
y -= a / b * x;
}
LL inv (LL a) {
LL x, y;
exgcd (a, Mod, x, y);
x = ((x % Mod) + Mod) % Mod;
return x;
}
LL C (LL a, LL b) {
return fac[b] * inv_fac[a] % Mod * inv_fac[b - a] % Mod;
}
int main () {
cin >> n >> s;
fac[0] = 1; w1[0] = 1; w2[0] = 1;
for (int i = 1; i <= n + s.length () + 1; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= Mod;
w1[i] = w1[i - 1] * 26;
w1[i] %= Mod;
w2[i] = w2[i - 1] * 25;
w2[i] %= Mod;
}
inv_fac[n + s.length ()] = inv (fac[n + s.length()]);
for(int i = n + s.length () - 1; i >= 0; i--)
inv_fac[i] = inv_fac[i + 1] * (i + 1) % Mod;
LL ans = 0;
for (int i = 1; i <= n + 1; i++) {
ans += w1[i - 1] * C (s.length () - 1, n + s.length () - i) % Mod * w2[n - i + 1] % Mod;
ans %= Mod;
}
cout << ans;
return 0;
}