20190811 小W的魔术——magic
思路
考虑可行的字符串有多少个,对于一个可行的字符串,枚举它与s最长公共前缀,若为$0$,则为$26^{n-l}$,否则为$25\times{26^{n-l-1}}$,用总的字符串数减去不可行的字符串数即可,答案为$26^n-26^{n-l}-l\times{25}\times{26^{n-l-1}}$。
代码
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
| #include<bits/stdc++.h>
#define mo 998244353
#define ll long long
using namespace std;
ll n, l;
string s;
ll po(ll x, ll y)
{
if (y < 0)return 0;
ll z = 1;
while (y)
{
if (y % 2)z = x * z % mo;
x = x * x % mo;
y /= 2;
}
return z;
}
int main()
{
// freopen("magic.in", "r", stdin);
// freopen("magic.out", "w", stdout);
ios::sync_with_stdio(false);
cin >> n >> s;
l = s.length();
if (n < l) cerr << "gg" << endl;
cout << (po(26, n) + mo - po(26, n - l) + (mo - l) * (25 * po(26, n - l - 1) % mo)) % mo;
return 0;
}
|