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<in+1), i i i 前面的空格每一个都有 26 26 26 种情况,总共 ( 2 6 i − 1 ) (26^{i-1}) (26i1)种情况, i i i 后面的空格,先选 s . l e n g t h − 1 s.length-1 s.length1 各格子放入 s 的其他字母,有 ( C k − i l e n − 1 ) (C_{k-i}^{len-1}) (Ckilen1)种方法,然后每个 s 串字母后面不选相同的,用于去重,有 ( 2 5 k − i − l e n + 1 ) (25^{k-i-len+1} ) (25kilen+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(nlog2n),所以只有打表打出幂。

但是还有一个问题,阶乘的逆元怎么求呢?


引入一个 N B NB NB 的东西,阶乘逆元

a x ≡ 1 ( m o d p ) ( a = i ! ) ax \equiv 1 \pmod {p} (a = i!) ax1(modp)(a=i!)
a i ∗ y ≡ 1 ( m o d p ) \frac {a}{i} * y \equiv 1 \pmod p iay1(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 iaix1(modp)
( a − 1 ) ! ∗ i x ≡ 1 ( m o d p ) (a-1)! *ix \equiv 1 \pmod p (a1)!ix1(modp)
a ! x ≡ 1 ( m o d p ) a!x \equiv1 \pmod p a!x1(modp)

所以 ( a − 1 ) ! (a - 1)! (a1)! 的逆元为 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值