두 개의 문자열 str1과 str2가 있다고 가정하고 길이가 L인 마법의 쌍을 찾아야 합니다. 모든 인덱스 I에 대해 str1[i]
접근 방식은 간단합니다. 우리가 볼 수 있듯이 길이가 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