📌  相关文章
📜  计算2的值,将其乘以N的二进制表示形式的两倍的幂

📅  最后修改于: 2021-04-29 03:21:30             🧑  作者: Mango

给定正整数N ,任务是找到(2 2 * X )的值,其中XN的二进制表示形式。由于答案可能非常大,因此请以10 9 + 7为模数打印。
例子:

天真的方法:解决此问题的最简单方法是生成N的二进制表示形式X ,并使用模幂计算出(2 2 * X )%(10 9 + 7)的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define M 1000000007
 
// Function to find the value
// of power(X, Y) in O(log Y)
long long power(long long X,
                long long Y)
{
    // Stores power(X, Y)
    long long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
        return 0;
 
    // Calculate power(X, Y)
    while (Y > 0) {
 
        // If Y is an odd number
        if (Y & 1) {
 
            // Update res
            res = (res * X) % M;
        }
 
        // Update Y
        Y = Y >> 1;
 
        // Update X
        X = (X * X) % M;
    }
 
    return res;
}
 
// Function to calculate
// (2^(2 * x)) % (10^9 + 7)
int findValue(long long int n)
{
 
    // Stores binary
    // representation of n
    long long X = 0;
 
    // Stores power of 10
    long long pow_10 = 1;
 
    // Calculate the binary
    // representation of n
    while (n) {
 
        // If n is an odd number
        if (n & 1) {
 
            // Update X
            X += pow_10;
        }
 
        // Update pow_10
        pow_10 *= 10;
 
        // Update n
        n /= 2;
    }
 
    // Double the value of X
    X = (X * 2) % M;
 
    // Stores the value of
    // (2^(2 * x)) % (10^9 + 7)
    long long res = power(2, X);
 
    return res;
}
 
// Driver Code
int main()
{
 
    long long n = 2;
    cout << findValue(n);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  static int M  = 1000000007;
 
  // Function to find the value
  // of power(X, Y) in O(log Y)
  static int  power(int  X,
                    int  Y)
  {
 
    // Stores power(X, Y)
    int  res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
      return 0;
 
    // Calculate power(X, Y)
    while (Y > 0)
    {
 
      // If Y is an odd number
      if ((Y & 1) != 0)
      {
 
        // Update res
        res = (res * X) % M;
      }
 
      // Update Y
      Y = Y >> 1;
 
      // Update X
      X = (X * X) % M;
    }
    return res;
  }
  
  // Function to calculate
  // (2^(2 * x)) % (10^9 + 7)
  static int findValue(int n)
  {
 
    // Stores binary
    // representation of n
    int  X = 0;
 
    // Stores power of 10
    int  pow_10 = 1;
 
    // Calculate the binary
    // representation of n
    while (n != 0)
    {
 
      // If n is an odd number
      if ((n & 1) != 0)
      {
 
        // Update X
        X += pow_10;
      }
 
      // Update pow_10
      pow_10 *= 10;
 
      // Update n
      n /= 2;
    }
 
    // Double the value of X
    X = (X * 2) % M;
 
    // Stores the value of
    // (2^(2 * x)) % (10^9 + 7)
    int  res = power(2, X);
    return res;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int  n = 2;
    System.out.println(findValue(n));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python3 program to implement
# the above approach
M = 1000000007
 
# Function to find the value
# of power(X, Y) in O(log Y)
def power(X, Y):
     
    # Stores power(X, Y)
    res = 1
 
    # Update X
    X = X % M
 
    # Base Case
    if (X == 0):
        return 0
 
    # Calculate power(X, Y)
    while (Y > 0):
 
        # If Y is an odd number
        if (Y & 1):
 
            # Update res
            res = (res * X) % M
 
        # Update Y
        Y = Y >> 1
 
        # Update X
        X = (X * X) % M
 
    return res
 
# Function to calculate
# (2^(2 * x)) % (10^9 + 7)
def findValue(n):
 
    # Stores binary
    # representation of n
    X = 0
 
    # Stores power of 10
    pow_10 = 1
 
    # Calculate the binary
    # representation of n
    while(n):
 
        # If n is an odd number
        if (n & 1):
             
            # Update X
            X += pow_10
 
        # Update pow_10
        pow_10 *= 10
 
        # Update n
        n //= 2
 
    # Double the value of X
    X = (X * 2) % M
 
    # Stores the value of
    # (2^(2 * x)) % (10^9 + 7)
    res = power(2, X)
 
    return res
 
# Driver Code
if __name__ == "__main__":
 
    n = 2
     
    print(findValue(n))
     
# This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  static int M  = 1000000007;
 
  // Function to find the value
  // of power(X, Y) in O(log Y)
  static int  power(int  X,
                    int  Y)
  {
 
    // Stores power(X, Y)
    int  res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
      return 0;
 
    // Calculate power(X, Y)
    while (Y > 0)
    {
 
      // If Y is an odd number
      if ((Y & 1) != 0)
      {
 
        // Update res
        res = (res * X) % M;
      }
 
      // Update Y
      Y = Y >> 1;
 
      // Update X
      X = (X * X) % M;
    }
    return res;
  }
  
  // Function to calculate
  // (2^(2 * x)) % (10^9 + 7)
  static int findValue(int n)
  {
 
    // Stores binary
    // representation of n
    int  X = 0;
 
    // Stores power of 10
    int  pow_10 = 1;
 
    // Calculate the binary
    // representation of n
    while (n != 0)
    {
 
      // If n is an odd number
      if ((n & 1) != 0)
      {
 
        // Update X
        X += pow_10;
      }
 
      // Update pow_10
      pow_10 *= 10;
 
      // Update n
      n /= 2;
    }
 
    // Double the value of X
    X = (X * 2) % M;
 
    // Stores the value of
    // (2^(2 * x)) % (10^9 + 7)
    int  res = power(2, X);
    return res;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int  n = 2;
    Console.WriteLine(findValue(n));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define M 1000000007
 
// Function to find the value
// of power(X, Y) in O(log Y)
long long power(long long X,
                long long Y)
{
    // Stores power(X, Y)
    long long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
        return 0;
 
    // Calculate power(X, Y)
    while (Y > 0) {
 
        // If Y is an odd number
        if (Y & 1) {
 
            // Update res
            res = (res * X) % M;
        }
 
        // Update Y
        Y = Y >> 1;
 
        // Update X
        X = (X * X) % M;
    }
 
    return res;
}
 
// Function to calculate
// (2^(2 * x)) % (10^9 + 7)
long long findValue(long long N)
{
 
    // dp[N] * dp[N]: Stores value
    // of (2^(2 * x)) % (10^9 + 7)
    long long dp[N + 1];
 
    // Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    // Iterate over the range [3, N]
    for (int i = 3; i <= N; i++) {
 
        // Stores righmost
        // bit of i
        int y = (i & (-i));
 
        // Stores the value
        // of (i - y)
        int x = i - y;
 
        // If x is power
        // of 2
        if (x == 0) {
 
            // Update dp[i]
            dp[i]
                = power(dp[i / 2], 10);
        }
        else {
 
            // Update dp[i]
            dp[i]
                = (dp[x] * dp[y]) % M;
        }
    }
 
    return (dp[N] * dp[N]) % M;
}
 
// Driver Code
int main()
{
 
    long long n = 150;
    cout << findValue(n);
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG
{
  static final long M = 1000000007;
 
  // Function to find the value
  // of power(X, Y) in O(log Y)
  static long power(long X,
                    long Y)
  {
 
    // Stores power(X, Y)
    long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
      return 0;
 
    // Calculate power(X, Y)
    while (Y > 0)
    {
 
      // If Y is an odd number
      if (Y % 2 == 1)
      {
 
        // Update res
        res = (res * X) % M;
      }
 
      // Update Y
      Y = Y >> 1;
 
      // Update X
      X = (X * X) % M;
    }
 
    return res;
  }
 
  // Function to calculate
  // (2^(2 * x)) % (10^9 + 7)
  static long findValue(int N)
  {
 
    // dp[N] * dp[N]: Stores value
    // of (2^(2 * x)) % (10^9 + 7)
    long []dp = new long[N + 1];
 
    // Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    // Iterate over the range [3, N]
    for (int i = 3; i <= N; i++)
    {
 
      // Stores righmost
      // bit of i
      int y = (i & (-i));
 
      // Stores the value
      // of (i - y)
      int x = i - y;
 
      // If x is power
      // of 2
      if (x == 0)
      {
 
        // Update dp[i]
        dp[i]
          = power(dp[i / 2], 10);
      }
      else
      {
 
        // Update dp[i]
        dp[i]
          = (dp[x] * dp[y]) % M;
      }
    }
    return (dp[N] * dp[N]) % M;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int n = 150;
    System.out.print(findValue(n));
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to implement
# the above approach
M = 1000000007;
 
# Function to find the value
# of power(X, Y) in O(log Y)
def power(X, Y):
     
    # Stores power(X, Y)
    res = 1;
 
    # Update X
    X = X % M;
 
    # Base Case
    if (X == 0):
        return 0;
 
    # Calculate power(X, Y)
    while (Y > 0):
 
        # If Y is an odd number
        if (Y % 2 == 1):
             
            # Update res
            res = (res * X) % M;
 
        # Update Y
        Y = Y >> 1;
 
        # Update X
        X = (X * X) % M;
    return res;
 
# Function to calculate
# (2^(2 * x)) % (10^9 + 7)
def findValue(N):
 
    # dp[N] * dp[N]: Stores value
    # of (2^(2 * x)) % (10^9 + 7)
    dp = [0]*(N + 1);
 
    # Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    # Iterate over the range [3, N]
    for i in range(3, N + 1):
 
        # Stores righmost
        # bit of i
        y = (i & (-i));
 
        # Stores the value
        # of (i - y)
        x = i - y;
 
        # If x is power
        # of 2
        if (x == 0):
 
            # Update dp[i]
            dp[i] = power(dp[i // 2], 10);
        else:
 
            # Update dp[i]
            dp[i] = (dp[x] * dp[y]) % M;
 
    return (dp[N] * dp[N]) % M;
 
# Driver Code
if __name__ == '__main__':
    n = 150;
    print(findValue(n));
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
  static readonly long M = 1000000007;
 
  // Function to find the value
  // of power(X, Y) in O(log Y)
  static long power(long X,
                    long Y)
  {
 
    // Stores power(X, Y)
    long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
      return 0;
 
    // Calculate power(X, Y)
    while (Y > 0)
    {
 
      // If Y is an odd number
      if (Y % 2 == 1)
      {
 
        // Update res
        res = (res * X) % M;
      }
 
      // Update Y
      Y = Y >> 1;
 
      // Update X
      X = (X * X) % M;
    }
 
    return res;
  }
 
  // Function to calculate
  // (2^(2 * x)) % (10^9 + 7)
  static long findValue(int N)
  {
 
    // dp[N] * dp[N]: Stores value
    // of (2^(2 * x)) % (10^9 + 7)
    long []dp = new long[N + 1];
 
    // Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    // Iterate over the range [3, N]
    for (int i = 3; i <= N; i++)
    {
 
      // Stores righmost
      // bit of i
      int y = (i & (-i));
 
      // Stores the value
      // of (i - y)
      int x = i - y;
 
      // If x is power
      // of 2
      if (x == 0)
      {
 
        // Update dp[i]
        dp[i]
          = power(dp[i / 2], 10);
      }
      else
      {
 
        // Update dp[i]
        dp[i]
          = (dp[x] * dp[y]) % M;
      }
    }
    return (dp[N] * dp[N]) % M;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int n = 150;
    Console.Write(findValue(n));
  }
}
 
// This code contributed by shikhasingrajput


输出:
1048576

时间复杂度: O(log 2 (X)),其中X是N的二进制表示形式
辅助空间: O(1)

高效方法:基于以下观察,可以使用动态编程来优化上述方法:

请按照以下步骤解决问题:

  • 初始化一个数组,例如dp [] ,使dp [i] 2存储(2 2 * X )%(10 9 + 7)的值,其中Xi的二进制表示。
  • 使用变量i在范围[3,N]上进行迭代并使用制表法找到dp [i]的所有可能值。
  • 最后,打印dp [N] 2的值

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define M 1000000007
 
// Function to find the value
// of power(X, Y) in O(log Y)
long long power(long long X,
                long long Y)
{
    // Stores power(X, Y)
    long long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
        return 0;
 
    // Calculate power(X, Y)
    while (Y > 0) {
 
        // If Y is an odd number
        if (Y & 1) {
 
            // Update res
            res = (res * X) % M;
        }
 
        // Update Y
        Y = Y >> 1;
 
        // Update X
        X = (X * X) % M;
    }
 
    return res;
}
 
// Function to calculate
// (2^(2 * x)) % (10^9 + 7)
long long findValue(long long N)
{
 
    // dp[N] * dp[N]: Stores value
    // of (2^(2 * x)) % (10^9 + 7)
    long long dp[N + 1];
 
    // Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    // Iterate over the range [3, N]
    for (int i = 3; i <= N; i++) {
 
        // Stores righmost
        // bit of i
        int y = (i & (-i));
 
        // Stores the value
        // of (i - y)
        int x = i - y;
 
        // If x is power
        // of 2
        if (x == 0) {
 
            // Update dp[i]
            dp[i]
                = power(dp[i / 2], 10);
        }
        else {
 
            // Update dp[i]
            dp[i]
                = (dp[x] * dp[y]) % M;
        }
    }
 
    return (dp[N] * dp[N]) % M;
}
 
// Driver Code
int main()
{
 
    long long n = 150;
    cout << findValue(n);
    return 0;
}

Java

// Java program to implement
// the above approach
class GFG
{
  static final long M = 1000000007;
 
  // Function to find the value
  // of power(X, Y) in O(log Y)
  static long power(long X,
                    long Y)
  {
 
    // Stores power(X, Y)
    long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
      return 0;
 
    // Calculate power(X, Y)
    while (Y > 0)
    {
 
      // If Y is an odd number
      if (Y % 2 == 1)
      {
 
        // Update res
        res = (res * X) % M;
      }
 
      // Update Y
      Y = Y >> 1;
 
      // Update X
      X = (X * X) % M;
    }
 
    return res;
  }
 
  // Function to calculate
  // (2^(2 * x)) % (10^9 + 7)
  static long findValue(int N)
  {
 
    // dp[N] * dp[N]: Stores value
    // of (2^(2 * x)) % (10^9 + 7)
    long []dp = new long[N + 1];
 
    // Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    // Iterate over the range [3, N]
    for (int i = 3; i <= N; i++)
    {
 
      // Stores righmost
      // bit of i
      int y = (i & (-i));
 
      // Stores the value
      // of (i - y)
      int x = i - y;
 
      // If x is power
      // of 2
      if (x == 0)
      {
 
        // Update dp[i]
        dp[i]
          = power(dp[i / 2], 10);
      }
      else
      {
 
        // Update dp[i]
        dp[i]
          = (dp[x] * dp[y]) % M;
      }
    }
    return (dp[N] * dp[N]) % M;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int n = 150;
    System.out.print(findValue(n));
  }
}
 
// This code is contributed by shikhasingrajput

Python3

# Python3 program to implement
# the above approach
M = 1000000007;
 
# Function to find the value
# of power(X, Y) in O(log Y)
def power(X, Y):
     
    # Stores power(X, Y)
    res = 1;
 
    # Update X
    X = X % M;
 
    # Base Case
    if (X == 0):
        return 0;
 
    # Calculate power(X, Y)
    while (Y > 0):
 
        # If Y is an odd number
        if (Y % 2 == 1):
             
            # Update res
            res = (res * X) % M;
 
        # Update Y
        Y = Y >> 1;
 
        # Update X
        X = (X * X) % M;
    return res;
 
# Function to calculate
# (2^(2 * x)) % (10^9 + 7)
def findValue(N):
 
    # dp[N] * dp[N]: Stores value
    # of (2^(2 * x)) % (10^9 + 7)
    dp = [0]*(N + 1);
 
    # Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    # Iterate over the range [3, N]
    for i in range(3, N + 1):
 
        # Stores righmost
        # bit of i
        y = (i & (-i));
 
        # Stores the value
        # of (i - y)
        x = i - y;
 
        # If x is power
        # of 2
        if (x == 0):
 
            # Update dp[i]
            dp[i] = power(dp[i // 2], 10);
        else:
 
            # Update dp[i]
            dp[i] = (dp[x] * dp[y]) % M;
 
    return (dp[N] * dp[N]) % M;
 
# Driver Code
if __name__ == '__main__':
    n = 150;
    print(findValue(n));
 
# This code is contributed by 29AjayKumar

C#

// C# program to implement
// the above approach
using System;
class GFG
{
  static readonly long M = 1000000007;
 
  // Function to find the value
  // of power(X, Y) in O(log Y)
  static long power(long X,
                    long Y)
  {
 
    // Stores power(X, Y)
    long res = 1;
 
    // Update X
    X = X % M;
 
    // Base Case
    if (X == 0)
      return 0;
 
    // Calculate power(X, Y)
    while (Y > 0)
    {
 
      // If Y is an odd number
      if (Y % 2 == 1)
      {
 
        // Update res
        res = (res * X) % M;
      }
 
      // Update Y
      Y = Y >> 1;
 
      // Update X
      X = (X * X) % M;
    }
 
    return res;
  }
 
  // Function to calculate
  // (2^(2 * x)) % (10^9 + 7)
  static long findValue(int N)
  {
 
    // dp[N] * dp[N]: Stores value
    // of (2^(2 * x)) % (10^9 + 7)
    long []dp = new long[N + 1];
 
    // Base Case
    dp[1] = 2;
    dp[2] = 1024;
 
    // Iterate over the range [3, N]
    for (int i = 3; i <= N; i++)
    {
 
      // Stores righmost
      // bit of i
      int y = (i & (-i));
 
      // Stores the value
      // of (i - y)
      int x = i - y;
 
      // If x is power
      // of 2
      if (x == 0)
      {
 
        // Update dp[i]
        dp[i]
          = power(dp[i / 2], 10);
      }
      else
      {
 
        // Update dp[i]
        dp[i]
          = (dp[x] * dp[y]) % M;
      }
    }
    return (dp[N] * dp[N]) % M;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int n = 150;
    Console.Write(findValue(n));
  }
}
 
// This code contributed by shikhasingrajput
输出:
654430484

时间复杂度: O(N * log(N))
辅助空间: O(N)