📜  包含X和Y且算术项最少的算术级数

📅  最后修改于: 2021-05-06 21:48:23             🧑  作者: Mango

给定三个整数NXY ,任务是找到一个N长度算术级数序列,该序列的第一项最少,包含XY。

例子:

天真的方法:最简单的方法是迭代从1到abs(XY)的所有可能的公共差值,并检查是否存在第一个项大于0且包含XYN长度AP。

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

高效方法:该方法基于以下想法:要在序列中同时包含X和Y,则AP的共同差异必须是abs(XY)的因数。以下是解决问题的步骤:

  1. 1迭代到sqrt(abs(XY))并仅考虑那些是abs(XY)因子的常见差异。
  2. 对于每个可能的共同差异,说diffabs(XY)除,使用二进制搜索算法找到大于0的最小第一项。
  3. 存储最小的第一项和相应的共同差以打印N长度的算术级数。

下面是上述方法的实现:

C++
// C++ program for the approach
#include 
using namespace std;
 
// Function that finds the minimum
// positive first term including X with given
// common difference and the number of terms
int minFirstTerm(int X, int diff, int N)
{
 
    // Stores the first term
    int first_term;
 
    // Initialize the low and high
    int low = 0, high = N;
 
    // Perform binary search
    while (low <= high) {
 
        // Find the mid
        int mid = (low + high) / 2;
 
        // Check if first term is
        // greater than 0
        if (X - mid * diff > 0) {
 
            // Store the possible first term
            first_term = X - mid * diff;
 
            // Search between mid + 1 to high
            low = mid + 1;
        }
        else
 
            // Search between low to mid-1
            high = mid - 1;
    }
 
    // Return the minimum first term
    return first_term;
}
 
// Function that finds the Arithmetic
// Progression with minimum possible
// first term containing X and Y
void printAP(int N, int X, int Y)
{
    // Considering X to be
    // smaller than Y always
    if (X > Y)
        swap(X, Y);
 
    // Stores the max common difference
    int maxDiff = Y - X;
 
    // Stores the minimum first term
    // and the corresponding common
    // difference of the resultant AP
    int first_term = INT_MAX, diff;
 
    // Iterate over all the common difference
    for (int i = 1; i * i <= maxDiff; i++) {
 
        // Check if X and Y is included
        // for current common difference
        if (maxDiff % i == 0) {
 
            // Store the possible
            // common difference
            int diff1 = i;
            int diff2 = maxDiff / diff1;
 
            // Number of terms from
            // X to Y with diff1
            // common difference
            int terms1 = diff2 + 1;
 
            // Number of terms from
            // X to Y with diff2
            // common difference
            int terms2 = diff1 + 1;
 
            // Find the corresponding first
            // terms with diff1 and diff2
            int first_term1
                = minFirstTerm(X, diff1, N - terms1);
            int first_term2
                = minFirstTerm(X, diff2, N - terms2);
 
            // Store the minimum first term
            // and the corresponding
            // common difference
            if (first_term1 < first_term) {
                first_term = first_term1;
                diff = diff1;
            }
            if (first_term2 < first_term) {
                first_term = first_term2;
                diff = diff2;
            }
        }
    }
 
    // Print the resultant AP
    for (int i = 0; i < N; i++) {
        cout << first_term << " ";
        first_term += diff;
    }
}
 
// Driver Code
int main()
{
    // Given length of AP
    // and the two terms
    int N = 5, X = 10, Y = 15;
 
    // Function Call
    printAP(N, X, Y);
 
    return 0;
}


Java
// Java program for the approach
import java.util.*;
 
class GFG{
 
// Function that finds the minimum
// positive first term including X
// with given common difference and
// the number of terms
static int minFirstTerm(int X, int diff, int N)
{
 
    // Stores the first term
    int first_term = Integer.MAX_VALUE;
 
    // Initialize the low and high
    int low = 0, high = N;
 
    // Perform binary search
    while (low <= high)
    {
 
        // Find the mid
        int mid = (low + high) / 2;
 
        // Check if first term is
        // greater than 0
        if (X - mid * diff > 0)
        {
 
            // Store the possible first term
            first_term = X - mid * diff;
 
            // Search between mid + 1 to high
            low = mid + 1;
        }
        else
 
            // Search between low to mid-1
            high = mid - 1;
    }
 
    // Return the minimum first term
    return first_term;
}
 
// Function that finds the Arithmetic
// Progression with minimum possible
// first term containing X and Y
static void printAP(int N, int X, int Y)
{
     
    // Considering X to be
    // smaller than Y always
    if (X > Y)
    {
        X = X + Y;
        Y = X - Y;
        X = X - Y;
    }
 
    // Stores the max common difference
    int maxDiff = Y - X;
 
    // Stores the minimum first term
    // and the corresponding common
    // difference of the resultant AP
    int first_term = Integer.MAX_VALUE, diff = 0;
 
    // Iterate over all the common difference
    for(int i = 1; i * i <= maxDiff; i++)
    {
         
        // Check if X and Y is included
        // for current common difference
        if (maxDiff % i == 0)
        {
 
            // Store the possible
            // common difference
            int diff1 = i;
            int diff2 = maxDiff / diff1;
 
            // Number of terms from
            // X to Y with diff1
            // common difference
            int terms1 = diff2 + 1;
 
            // Number of terms from
            // X to Y with diff2
            // common difference
            int terms2 = diff1 + 1;
 
            // Find the corresponding first
            // terms with diff1 and diff2
            int first_term1 = minFirstTerm(X, diff1,
                                           N - terms1);
            int first_term2 = minFirstTerm(X, diff2,
                                           N - terms2);
 
            // Store the minimum first term
            // and the corresponding
            // common difference
            if (first_term1 < first_term)
            {
                first_term = first_term1;
                diff = diff1;
            }
            if (first_term2 < first_term)
            {
                first_term = first_term2;
                diff = diff2;
            }
        }
    }
 
    // Print the resultant AP
    for(int i = 0; i < N; i++)
    {
        System.out.print(first_term + " ");
        first_term += diff;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given length of AP
    // and the two terms
    int N = 5, X = 10, Y = 15;
 
    // Function call
    printAP(N, X, Y);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the approach
import sys
 
# Function that finds the minimum
# positive first term including X with given
# common difference and the number of terms
def minFirstTerm(X, diff, N):
 
    # Stores the first term
    first_term_1 = sys.maxsize
 
    # Initialize the low and high
    low = 0
    high = N
 
    # Perform binary search
    while (low <= high):
 
        # Find the mid
        mid = (low + high) // 2
 
        # Check if first term is
        # greater than 0
        if (X - mid * diff > 0):
 
            # Store the possible first term
            first_term_1 = X - mid * diff
 
            # Search between mid + 1 to high
            low = mid + 1
 
        else:
 
            # Search between low to mid-1
            high = mid - 1
 
    # Return the minimum first term
    return first_term_1
 
# Function that finds the Arithmetic
# Progression with minimum possible
# first term containing X and Y
def printAP(N, X, Y):
     
    # Considering X to be
    # smaller than Y always
    if (X > Y):
        X = X + Y
        Y = X - Y
        X = X - Y
 
    # Stores the max common difference
    maxDiff = Y - X
 
    # Stores the minimum first term
    # and the corresponding common
    # difference of the resultant AP
    first_term = sys.maxsize
    diff = 0
 
    # Iterate over all the common difference
    for i in range(1, maxDiff + 1):
        if i * i > maxDiff:
            break
 
        # Check if X and Y is included
        # for current common difference
        if (maxDiff % i == 0):
 
            # Store the possible
            # common difference
            diff1 = i
            diff2 = maxDiff // diff1
 
            # Number of terms from
            # X to Y with diff1
            # common difference
            terms1 = diff2 + 1
 
            # Number of terms from
            # X to Y with diff2
            # common difference
            terms2 = diff1 + 1
 
            # Find the corresponding first
            # terms with diff1 and diff2
            first_term1 = minFirstTerm(X, diff1,
                                       N - terms1)
            first_term2 = minFirstTerm(X, diff2,
                                       N - terms2)
 
            # Store the minimum first term
            # and the corresponding
            # common difference
            if (first_term1 < first_term):
                first_term = first_term1
                diff = diff1
 
            if (first_term2 < first_term):
                first_term = first_term2
                diff = diff2
 
    # Print the resultant AP
    for i in range(N):
        print(first_term, end = " ")
        first_term += diff
         
# Driver Code
if __name__ == '__main__':
     
    # Given length of AP
    # and the two terms
    N = 5
    X = 10
    Y = 15
 
    # Function call
    printAP(N, X, Y)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the approach
using System;
 
class GFG{
 
// Function that finds the minimum
// positive first term including X
// with given common difference and
// the number of terms
static int minFirstTerm(int X, int diff,
                        int N)
{
 
    // Stores the first term
    int first_term = int.MaxValue;
 
    // Initialize the low and high
    int low = 0, high = N;
 
    // Perform binary search
    while (low <= high)
    {
 
        // Find the mid
        int mid = (low + high) / 2;
 
        // Check if first term is
        // greater than 0
        if (X - mid * diff > 0)
        {
 
            // Store the possible first term
            first_term = X - mid * diff;
 
            // Search between mid + 1 to high
            low = mid + 1;
        }
        else
 
            // Search between low to mid-1
            high = mid - 1;
    }
 
    // Return the minimum first term
    return first_term;
}
 
// Function that finds the Arithmetic
// Progression with minimum possible
// first term containing X and Y
static void printAP(int N, int X, int Y)
{
     
    // Considering X to be
    // smaller than Y always
    if (X > Y)
    {
        X = X + Y;
        Y = X - Y;
        X = X - Y;
    }
 
    // Stores the max common difference
    int maxDiff = Y - X;
 
    // Stores the minimum first term
    // and the corresponding common
    // difference of the resultant AP
    int first_term = int.MaxValue, diff = 0;
 
    // Iterate over all the common difference
    for(int i = 1; i * i <= maxDiff; i++)
    {
         
        // Check if X and Y is included
        // for current common difference
        if (maxDiff % i == 0)
        {
 
            // Store the possible
            // common difference
            int diff1 = i;
            int diff2 = maxDiff / diff1;
 
            // Number of terms from
            // X to Y with diff1
            // common difference
            int terms1 = diff2 + 1;
 
            // Number of terms from
            // X to Y with diff2
            // common difference
            int terms2 = diff1 + 1;
 
            // Find the corresponding first
            // terms with diff1 and diff2
            int first_term1 = minFirstTerm(X, diff1,
                                        N - terms1);
            int first_term2 = minFirstTerm(X, diff2,
                                        N - terms2);
 
            // Store the minimum first term
            // and the corresponding
            // common difference
            if (first_term1 < first_term)
            {
                first_term = first_term1;
                diff = diff1;
            }
            if (first_term2 < first_term)
            {
                first_term = first_term2;
                diff = diff2;
            }
        }
    }
 
    // Print the resultant AP
    for(int i = 0; i < N; i++)
    {
        Console.Write(first_term + " ");
        first_term += diff;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given length of AP
    // and the two terms
    int N = 5, X = 10, Y = 15;
 
    // Function call
    printAP(N, X, Y);
}
}
 
// This code is contributed by Amit Katiyar


输出:
5 10 15 20 25 

时间复杂度: O(sqrt(abs(XY))* log(N))
辅助空间: O(1)