在 C++ 中查找长度为 L 的成对神奇字符串的数量。
假设我们有两个字符串 str1 和 str2,我们必须找出长度为 L 的神奇字符串对数量。如果每个索引 I 的 str1[i] < str2[i],则两个字符串将是神奇的。我们必须计算对的数量,因为数字很大,然后使用模 109 返回答案。字符串将仅包含小写字母。
方法很简单。正如我们所见,如果长度为 L = 1,并且索引 i = 1 保持为“a”,在 str1 中,则 str2 的索引 i = 1 将从“b”保持到“z”的 25 种组合,对于下一个字符,它将为 24 种组合,因此它将为 25 + 24 + . . . + 1 = 325。现在对于 L = 2,它将为 3252。对于长度 L,它将为 325L。如果它很大,请查找模 109。
示例
#include<iostream>
#include<cmath>
using namespace std;
int power(int a, unsigned int b, int mod) {
int res = 1;
a = a % mod;
while (b > 0) {
if (b & 1)
res = (res * a) % mod;
b = b >> 1;
a = (a * a) % mod;
}
return res;
}
int main() {
int L = 2, P = pow(10, 9);
int res = power(325, L, P);
cout << "Combinations: " << res << endl;
}输出
Combinations: 105625
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP