📌  相关文章
📜  较大的N |值时,几何级数的N个项的总和|设置2(使用递归)

📅  最后修改于: 2021-04-22 02:29:34             🧑  作者: Mango

几何级数是连续项之间具有恒定比率的级数。系列的第一项用a表示,公共比率用r表示。该系列看起来像这样:-  a, ar, ar^2, ar^3, ar^4,... .
任务是找到这样一个序列mod M的总和。

例子:

Input:  a = 1, r = 2, N = 10000, M = 10000
Output:  8751

Input:  a = 1, r = 4, N = 10000, M = 100000
Output:  12501

方法:

  1. 求级数之和 a + ar + ar^2 + ar^3 + . . . . + ar^N 我们可以很容易地将a作为通用,并求和 1 + r + r^2 + r^3 + . . . . + r^N 并乘以a。
  2. 步骤来找到上述系列的总和。
    • 在这里,可以解决:
       [1 + r + r^2 + r^3 + . . . + r^(2*n+1)] = (1+r)*(1 + (r^2) + (r^2)^2 + (r^2)^3 + . . . + (r^2)^n)
    • 因此,基本案例是:
      Sum(r, 0) = 1.
                   Sum(r, 1) = 1 + r.
      

下面是上述方法的实现。

C++
// C++ implementation to 
// illustrate the program 
#include 
using namespace std;
  
// Function to calculate the sum 
// recursively 
int SumGPUtil(long long int r,
              long long int n, 
              long long int m)
{ 
      
    // Base cases 
    if (n == 0)
        return 1;
    if (n == 1)
        return (1 + r) % m;
      
    long long int ans;
    // If n is odd 
    if (n % 2 == 1)
    { 
        ans = (1 + r) * 
              SumGPUtil((r * r) % m,
                        (n - 1) / 2, m);
    }
    else
    {
          
        // If n is even 
        ans = 1 + (r * (1 + r) * 
             SumGPUtil((r * r) % m, 
                       (n / 2) - 1, m));
    }
    return (ans % m);
}
  
// Function to print the value of Sum 
void SumGP(long long int a,
           long long int r,
           long long int N,
           long long int M)
{
    long long int answer;
      
    answer = a * SumGPUtil(r, N, M);
    answer = answer % M; 
      
    cout << answer << endl; 
}
  
// Driver Code 
int main()
{
      
    // First element
    long long int a = 1;
      
    // Common diffrence 
    long long int r = 4; 
      
    // Number of elements 
    long long int N = 10000; 
      
    // Mod value 
    long long int M = 100000; 
  
    SumGP(a, r, N, M);
  
    return 0;
}
  
// This code is contributed by sanjoy_62


Java
// Java implementation to 
// illustrate the program 
import java.io.*;
  
class GFG{
  
// Function to calculate the sum 
// recursively 
static long SumGPUtil(long r, long n,
                      long m)
{ 
      
    // Base cases 
    if (n == 0)
        return 1;
    if (n == 1)
        return (1 + r) % m;
      
    long ans;
      
    // If n is odd 
    if (n % 2 == 1)
    { 
        ans = (1 + r) * 
              SumGPUtil((r * r) % m, 
                        (n - 1) / 2, m);
    }
    else
    {
        // If n is even 
        ans = 1 + (r * (1 + r) * 
             SumGPUtil((r * r) % m, 
                       (n / 2) - 1, m));
    }
      
    return (ans % m);
}
  
// Function to prlong the value of Sum 
static void SumGP(long a, long r,
                  long N, long M) 
{
    long answer;
    answer = a * SumGPUtil(r, N, M);
    answer = answer % M; 
      
    System.out.println(answer); 
}
  
// Driver Code 
public static void main (String[] args)
{
      
    // First element 
    long a = 1; 
      
    // Common diffrence 
    long r = 4;
      
    // Number of elements 
    long N = 10000;
      
    // Mod value 
    long M = 100000; 
  
    SumGP(a, r, N, M);
}
}
  
// This code is contributed by sanjoy_62


Python3
# Python3 implementation to illustrate the program 
  
# Function to calculate the sum 
# recursively
def SumGPUtil (r, n, m):
      
    # Base cases
    if n == 0: return 1
    if n == 1: return (1 + r) % m
    
    # If n is odd
    if n % 2 == 1:
        ans = (1 + r) * SumGPUtil(r * r % m,
                                  (n - 1)//2,
                                  m)
    else:
        #If n is even
        ans = 1 + r * (1 + r) * SumGPUtil(r * r % m,
                                          n//2 - 1,
                                          m)
    
    return ans % m
  
# Function to print the value of Sum
def SumGP (a, r, N, M):
      
    answer = a * SumGPUtil(r, N, M)
    answer = answer % M
    print(answer)
  
#Driver Program
if __name__== '__main__':
  
    a = 1 # first element
    r = 4 # common diffrence
    N = 10000 # Number of elements
    M = 100000 # Mod value 
  
    SumGP(a, r, N, M)


C#
// C# implementation to 
// illustrate the program 
using System;
  
class GFG{
  
// Function to calculate the sum 
// recursively 
static long SumGPUtil(long r, long n,
                      long m)
{ 
      
    // Base cases 
    if (n == 0)
        return 1;
    if (n == 1)
        return (1 + r) % m;
      
    long ans;
      
    // If n is odd 
    if (n % 2 == 1)
    { 
        ans = (1 + r) * 
              SumGPUtil((r * r) % m, 
                        (n - 1) / 2, m);
    }
    else
    {
          
        // If n is even 
        ans = 1 + (r * (1 + r) * 
             SumGPUtil((r * r) % m, 
                       (n / 2) - 1, m));
    }
    return (ans % m);
}
  
// Function to prlong the value of Sum 
static void SumGP(long a, long r,
                  long N, long M)
{
    long answer;
    answer = a * SumGPUtil(r, N, M);
    answer = answer % M; 
      
    Console.WriteLine(answer); 
}
  
// Driver Code 
public static void Main() 
{
      
    // First element 
    long a = 1; 
      
    // Common diffrence 
    long r = 4; 
      
    // Number of elements 
    long N = 10000;
      
    // Mod value 
    long M = 100000; 
  
    SumGP(a, r, N, M);
}
}
  
// This code is contributed by sanjoy_62


输出:
12501

时间复杂度: O(log N)