📜  查找长度为L的魔幻字符串对数

📅  最后修改于: 2021-05-08 18:30:52             🧑  作者: Mango

一对字符串sr的被称为神奇如果对于每个索引i S的字符小于Rs [I] 。任务是计算长度为L的可能的字符串对数。由于该值可能很大,请以模10 9为模给出答案。

注意:该字符串仅包含小写英文字母。

例子:

方法:对于L = 1 ,总可能性为325 。对于L = 2 ,总可能性为325 2L的任何值的总可能性为325 L。由于此值可能很大,请打印答案模10 9

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Iterative Function to calculate (x^y)%p in O(log y)
int power(int x, unsigned int y, int p)
{
  
    // Initialize result
    int res = 1;
  
    // Update x if it is >= p
    x = x % p;
  
    while (y > 0) {
  
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
  
        // Y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
  
// Driver Code
int main()
{
  
    int L = 2, P = pow(10, 9);
  
    int ans = power(325, L, P);
  
    cout << ans << "\n";
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG
{
  
    // Iterative Function to calculate (x^y)%p in O(log y)
    static int power(int x, int y, int p) 
    {
  
        // Initialize result
        int res = 1;
  
        // Update x if it is >= p
        x = x % p;
  
        while (y > 0)
        {
  
            // If y is odd, multiply x with result
            if (y % 2 == 1)
            {
                res = (res * x) % p;
            }
  
            // Y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
        return res;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int L = 2;
        int P = (int) Math.pow(10, 9);
  
        int ans = power(325, L, P);
        System.out.println(ans);
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python implementation of the approach
   
# Iterative Function to calculate (x^y)%p in O(log y)
def power(x, y, p):
   
    # Initialize result
    res = 1;
   
    # Update x if it is >= p
    x = x % p;
   
    while (y > 0):
   
        # If y is odd, multiply x with result
        if (y %2== 1):
            res = (res * x) % p;
   
        # Y must be even now
        y = y >> 1; # y = y/2
        x = (x * x) % p;
    return res;
   
# Driver Code
L = 2; P = pow(10, 9);
ans = power(325, L, P);
print(ans);
  
  
#  This code contributed by Rajput-Ji


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Iterative Function to calculate (x^y)%p in O(log y) 
    static int power(int x, int y, int p) 
    { 
  
        // Initialize result 
        int res = 1; 
  
        // Update x if it is >= p 
        x = x % p; 
  
        while (y > 0) 
        { 
  
            // If y is odd, multiply x with result 
            if (y % 2 == 1) 
            { 
                res = (res * x) % p; 
            } 
  
            // Y must be even now 
            y = y >> 1; // y = y/2 
            x = (x * x) % p; 
        } 
        return res; 
    } 
  
    // Driver Code 
    public static void Main() 
    { 
        int L = 2; 
        int P = (int) Math.Pow(10, 9); 
  
        int ans = power(325, L, P); 
        Console.WriteLine(ans); 
    } 
} 
  
// This code is contributed by AnkitRai01


PHP
= p
    $x = $x % $p;
  
    while ($y > 0)
    {
  
        // If y is odd, multiply x with result
        if ($y & 1)
            $res = ($res * $x) % $p;
  
        // Y must be even now
        $y = $y >> 1; // y = y/2
        $x = ($x * $x) % $p;
    }
    return $res;
}
  
// Driver Code
  
$L = 2;
$P = pow(10, 9);
  
$ans = power(325, $L, $P);
  
echo $ans , "\n";
  
// This code is contributed by ajit.
?>


输出:
105625