📌  相关文章
📜  第i个项为i ^ k –(i-1)^ k的第N个项的级数和

📅  最后修改于: 2021-04-29 07:26:08             🧑  作者: Mango

给定N和K的值。任务是找到直到第N个项的序列之和,第i个项由T i = i k +(i – 1) k给出。由于该系列的总和可能非常大,因此请计算其总和为1000000007。
例子:

Input : n = 5, k = 2
Output: 25
first 5 term of the series :
T1 = ( 1 )2 + ( 1 - 1 )2  = 1
T2 = ( 2 )2 + ( 2 - 1 )2  = 3
T3 = ( 3 )2 + ( 3 - 1 )2  = 5
T4 = ( 4 )2 + ( 4 - 1 )2  = 7
T4 = ( 5 )2 + ( 5 - 1 )2  = 9
Sum of the series = 1 + 3 + 5 + 7 + 9 =  25


Input: n = 4, k = 3
Output : 64
First 4 term of the series:
T2 = ( 1 )3 + ( 1 - 1 )3  = 1
T3 = ( 2 )3 + ( 2 - 1 )3  = 7
T4 = ( 3 )3 + ( 3 - 1 )3  = 19
T4 = ( 4 )3 + ( 4 - 1 )3  = 37
Sum of the series = 1 + 7 + 19 + 37 = 64 

朴素的方法一个简单的解决方案是生成所有项,直到n个并将其相加。时间复杂度将为O(N)。由于N非常大,因此该方法将导致TLE。
更好的解决方案
让我们看一下序列模式。

以下是天真的计算N K的方法

CPP
// CPP Code to find sum of series
 
#include 
using namespace std;
 
#define MOD 1000000007
 
// function to calculate sum of series
int calculateSum(int n, int k)
{
    // Initialize result
    long long res = 1;
 
    // loop to calculate n^k
    for (int i = 0; i < k; i++) {
        res = (res * n) % MOD;
    }
 
    return res;
}
 
// Driver code
int main()
{
    int n = 4, k = 3;
 
    // function calling
    cout << calculateSum(n, k);
 
    return 0;
}


Java
// JAVA code to find
// Sum of series
 
class GFG {
 
    // function to calculate sum of series
    static long calculateSum(int n, int k)
    {
 
        // Initialize res and MOD
 
        long res = 1;
        long MOD = 1000000007;
 
        // loop to calculate n^k % MOD
        for (int i = 0; i < k; i++) {
 
            res = (res * n) % MOD;
        }
 
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int n = 4;
        int k = 3;
        System.out.print(calculateSum(n, k));
    }
};


Python3
# Python 3 code to find
# the sum of series
 
 
# function to calculate sum of series
def calculateSum(n, k):
     
    # initialize res
    res = 1
  
    # initialize MOD
    MOD = 1000000007
     
    # loop to calculate n ^ k % MOD
    for i in range ( 0, k):
        res = ( res * n ) % MOD
 
    return res
 
 
# Driver code
n = 4
k = 3
 
# function calling
print(calculateSum(n, k))


C#
// C# code to find the
// sum of the series
 
using System;
 
class GFG {
    // function to calculate sum of the series
    static int calculateSum(int n, int k)
    {
        // initialize res
        int res = 1;
 
        // Initialize MOD
        int MOD = 1000000007;
 
        // loop to calculate n^k % MOD
        for (int i = 0; i < k; i++) {
 
            res = (res * n) % MOD;
        }
        return res;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 4;
        int k = 3;
 
        // function calling
        Console.WriteLine(calculateSum(n, k));
    }
}


PHP
// PHP code to find
// the sum of series
 


Javascript


CPP
// CPP Code to find sum of series
#include 
using namespace std;
 
#define MOD 1000000007
 
// function to calculate sum of series
int calculateSum(int n, int k)
{
    // initialize res
    long long res = 1;
 
    // loop to calculate n^k % MOD
    // using modular Arithmetic
    while (k > 0) {
 
        // if k is odd
        // multiply it with res
        if (k & 1)
            res = (res * n) % MOD;
 
        // k must be even now
        // change k to k/2
        k = k / 2;
 
        // change n to  n^2
        n = (n * n) % MOD;
    }
    return res;
}
 
// Driver code
int main()
{
    int n = 4, k = 3;
    cout << calculateSum(n, k);
    return 0;
}


Java
// JAVA code to find sum of series
 
class GFG {
 
    // Function to calculate sum of series
    static long calculateSum(int n, int k)
    {
 
        // initialize res and MOD
        int res = 1;
        int MOD = 1000000007;
 
        // loop to calculate n^k % MOD
        // using modular Arithmetic
        while (k > 0) {
            // if k is odd
            // multiply n with res
            if ((k & 1) == 1)
                res = (res * n) % MOD;
 
            // k must be even now
            // change k to k / 2
            k = k / 2;
 
            // change n to n^2
            n = (n * n) % MOD;
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4, k = 3;
        System.out.print(calculateSum(n, k));
    }
};


Python3
# Python code to calculate sum of series
 
# function to calculate sum of series
def calculateSum(n, k):
     
    # Initialize res
    res = 1
    
    # initialize MOD
    MOD = 1000000007
     
 
    # loop to calculate n ^ k % MOD
    # using Modular airthmatic
    while k > 0 :
 
          # if k is odd
          # Multiply n with res  
          if ( k & 1 ) == 1 :
              res = ( res * n ) % MOD
 
 
          # k must be even now
          # change k to k / 2
          k = k // 2
   
          # change n to n ^ 2
          n =( n * n ) % MOD
           
 
    return res
 
 
# Driver code
 
n = 4
k = 3
 
print(calculateSum(n, k))


C#
// C# code to calculate
// sum of series
 
using System;
 
class GFG {
 
    // Function to calculate sum of series
    static int calculateSum(int n, int k)
    {
        int res = 1; // Initialize res
 
        int MOD = 1000000007;
 
        // Loop to calculate n^k % MOD
        while (k > 0) {
            // If y is odd
            // multiply  res with n
            if ((k & 1) == 1)
                res = (res * n) % MOD;
 
            // k must be even now
            // change k to k/2
            k = k / 2;
 
            // change n to n^2
            n = (n * n) % MOD;
        }
        return res;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 4;
        int k = 3;
 
        // Function calling
        Console.WriteLine(calculateSum(n, k));
    }
}


PHP
// PHP code to calculate
// Sum of series
 
 0)
    {
          
        // If y is odd
        // multiply with result
        if ($k & 1)
            $res = ( $res * $n ) % $MOD;
  
        // k must be even now
        // Change k to k/2 
        $k = $k / 2;
          
        // Change n to n^2
        $n =( $n * $n ) % $MOD;
    }
    return $res;
}
  
// Driver Code
$n = 4;
$k = 3;
 
// Function calling
echo calculateSum($n, $k);
 
?>


Javascript


输出
64

时间复杂度:O(K)
ÑK可在为O(log N)使用模幂来计算。

CPP

// CPP Code to find sum of series
#include 
using namespace std;
 
#define MOD 1000000007
 
// function to calculate sum of series
int calculateSum(int n, int k)
{
    // initialize res
    long long res = 1;
 
    // loop to calculate n^k % MOD
    // using modular Arithmetic
    while (k > 0) {
 
        // if k is odd
        // multiply it with res
        if (k & 1)
            res = (res * n) % MOD;
 
        // k must be even now
        // change k to k/2
        k = k / 2;
 
        // change n to  n^2
        n = (n * n) % MOD;
    }
    return res;
}
 
// Driver code
int main()
{
    int n = 4, k = 3;
    cout << calculateSum(n, k);
    return 0;
}

Java

// JAVA code to find sum of series
 
class GFG {
 
    // Function to calculate sum of series
    static long calculateSum(int n, int k)
    {
 
        // initialize res and MOD
        int res = 1;
        int MOD = 1000000007;
 
        // loop to calculate n^k % MOD
        // using modular Arithmetic
        while (k > 0) {
            // if k is odd
            // multiply n with res
            if ((k & 1) == 1)
                res = (res * n) % MOD;
 
            // k must be even now
            // change k to k / 2
            k = k / 2;
 
            // change n to n^2
            n = (n * n) % MOD;
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4, k = 3;
        System.out.print(calculateSum(n, k));
    }
};

Python3

# Python code to calculate sum of series
 
# function to calculate sum of series
def calculateSum(n, k):
     
    # Initialize res
    res = 1
    
    # initialize MOD
    MOD = 1000000007
     
 
    # loop to calculate n ^ k % MOD
    # using Modular airthmatic
    while k > 0 :
 
          # if k is odd
          # Multiply n with res  
          if ( k & 1 ) == 1 :
              res = ( res * n ) % MOD
 
 
          # k must be even now
          # change k to k / 2
          k = k // 2
   
          # change n to n ^ 2
          n =( n * n ) % MOD
           
 
    return res
 
 
# Driver code
 
n = 4
k = 3
 
print(calculateSum(n, k))

C#

// C# code to calculate
// sum of series
 
using System;
 
class GFG {
 
    // Function to calculate sum of series
    static int calculateSum(int n, int k)
    {
        int res = 1; // Initialize res
 
        int MOD = 1000000007;
 
        // Loop to calculate n^k % MOD
        while (k > 0) {
            // If y is odd
            // multiply  res with n
            if ((k & 1) == 1)
                res = (res * n) % MOD;
 
            // k must be even now
            // change k to k/2
            k = k / 2;
 
            // change n to n^2
            n = (n * n) % MOD;
        }
        return res;
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 4;
        int k = 3;
 
        // Function calling
        Console.WriteLine(calculateSum(n, k));
    }
}

的PHP

// PHP code to calculate
// Sum of series
 
 0)
    {
          
        // If y is odd
        // multiply with result
        if ($k & 1)
            $res = ( $res * $n ) % $MOD;
  
        // k must be even now
        // Change k to k/2 
        $k = $k / 2;
          
        // Change n to n^2
        $n =( $n * $n ) % $MOD;
    }
    return $res;
}
  
// Driver Code
$n = 4;
$k = 3;
 
// Function calling
echo calculateSum($n, $k);
 
?>

Java脚本


输出
64

时间复杂度: O(log n)