📌  相关文章
📜  等式F(N)=(2 * F(N – 1))%10 ^ 9 + 7的给定N项中的第一项

📅  最后修改于: 2021-04-29 09:49:38             🧑  作者: Mango

给定一个整数N和一个整数F N ,它表示线性方程F(N)=(2 * F(N – 1))%M的第N项,其中M为10 9 + 7 ,任务是找到F(1)的值。

例子:

天真的方法:解决此问题的最简单方法是尝试在[1,M – 1]范围内尝试所有可能的F(1)值,并检查是否有任何值满足给定的线性方程式。如果发现为真,则打印F(1)的值。

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

高效方法:为了优化上述方法,该思想基于以下观察结果:

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

  • 计算在模M2 (N – 1)的模乘逆,即modInv
  • 最后,打印F(N)* modInv的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define M 1000000007
 
// Function to find the value
// of power(X, N) % M
long long power(long long x,
                long long N)
{
    // Stores the value
    // of (X ^ N) % M
    long long res = 1;
 
    // Calculate the value of
    // power(x, N) % M
    while (N > 0) {
 
        // If N is odd
        if (N & 1) {
 
            // Update res
            res = (res * x) % M;
        }
 
        // Update x
        x = (x * x) % M;
 
        // Update N
        N = N >> 1;
    }
    return res;
}
 
// Function to find modulo multiplicative
// inverse of X under modulo M
long long moduloInverse(long long X)
{
    return power(X, M - 2);
}
 
// Function to find the value of F(1)
long long F_1(long long N,
              long long F_N)
{
 
    // Stores power(2, N - 1)
    long long P_2 = power(2, N - 1);
 
    // Stores modulo multiplicative
    // inverse of P_2 under modulo M
    long long modInv = moduloInverse(P_2);
 
    // Stores the value of F(1)
    long long res;
 
    // Update res
    res = ((modInv % M) * (F_N % M)) % M;
 
    return res;
}
 
// Driver code
int main()
{
 
    long long N = 3;
    long long F_N = 6;
    cout << F_1(N, F_N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
static final int M = 1000000007;
 
// Function to find the
// value of power(X, N) % M
static long power(long x,
                  long N)
{
  // Stores the value
  // of (X ^ N) % M
  long res = 1;
 
  // Calculate the value
  // of power(x, N) % M
  while (N > 0)
  {
    // If N is odd
    if (N % 2 == 1)
    {
      // Update res
      res = (res * x) % M;
    }
 
    // Update x
    x = (x * x) % M;
 
    // Update N
    N = N >> 1;
  }
  return res;
}
 
// Function to find modulo
// multiplicative inverse
// of X under modulo M
static long moduloInverse(long X)
{
  return power(X, M - 2);
}
 
// Function to find the
// value of F(1)
static long F_1(long N,
                long F_N)
{
  // Stores power(2, N - 1)
  long P_2 = power(2, N - 1);
 
  // Stores modulo multiplicative
  // inverse of P_2 under modulo M
  long modInv = moduloInverse(P_2);
 
  // Stores the value of F(1)
  long res;
 
  // Update res
  res = ((modInv % M) *
         (F_N % M)) % M;
 
  return res;
}
 
// Driver code
public static void main(String[] args)
{
  long N = 3;
  long F_N = 6;
  System.out.print(F_1(N, F_N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
M = 1000000007
 
# Function to find the value
# of power(X, N) % M
def power(x, N):
     
    # Stores the value
    # of (X ^ N) % M
    res = 1
 
    # Calculate the value of
    # power(x, N) % M
    while (N > 0):
 
        # If N is odd
        if (N & 1):
 
            # Update res
            res = (res * x) % M
 
        # Update x
        x = (x * x) % M
 
        # Update N
        N = N >> 1
         
    return res
 
# Function to find modulo multiplicative
# inverse of X under modulo M
def moduloInverse(X):
     
    return power(X, M - 2)
 
#Function to find the value of F(1)
def F_1(N, F_N):
 
    # Stores power(2, N - 1)
    P_2 = power(2, N - 1)
 
    # Stores modulo multiplicative
    # inverse of P_2 under modulo M
    modInv = moduloInverse(P_2)
 
    # Stores the value of F(1)
    res = 0
 
    # Update res
    res = ((modInv % M) * (F_N % M)) % M
 
    return res
 
# Driver code
if __name__ == '__main__':
     
    N = 3
    F_N = 6
     
    print(F_1(N, F_N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
static readonly int M = 1000000007;
 
// Function to find the
// value of power(X, N) % M
static long power(long x,
                  long N)
{
  // Stores the value
  // of (X ^ N) % M
  long res = 1;
 
  // Calculate the value
  // of power(x, N) % M
  while (N > 0)
  {
    // If N is odd
    if (N % 2 == 1)
    {
      // Update res
      res = (res * x) % M;
    }
 
    // Update x
    x = (x * x) % M;
 
    // Update N
    N = N >> 1;
  }
  return res;
}
 
// Function to find modulo
// multiplicative inverse
// of X under modulo M
static long moduloInverse(long X)
{
  return power(X, M - 2);
}
 
// Function to find the
// value of F(1)
static long F_1(long N,
                long F_N)
{
  // Stores power(2, N - 1)
  long P_2 = power(2, N - 1);
 
  // Stores modulo multiplicative
  // inverse of P_2 under modulo M
  long modInv = moduloInverse(P_2);
 
  // Stores the value of F(1)
  long res;
 
  // Update res
  res = ((modInv % M) *
         (F_N % M)) % M;
 
  return res;
}
 
// Driver code
public static void Main(String[] args)
{
  long N = 3;
  long F_N = 6;
  Console.Write(F_1(N, F_N));
}
}
 
// This code is contributed by shikhasingrajput


输出:
500000005












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