📌  相关文章
📜  GCD最大且和等于n的级数

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

给定整数n,请打印m个递增的数字,以使m个数字的总和等于n,并且在所有可能的序列中m个数字的GCD最大。如果不可能连续打印,则打印“ -1”。
例子 :

Input  : n = 24,
         m = 3  
Output : 4 8 12  
Explanation : (3, 6, 15) is also a series 
of m numbers which sums to N, but gcd = 3
(4, 8, 12) has gcd = 4 which is the maximum
possible.
              
Input  : n = 6 
         m = 4 
Output : -1 
Explanation: It is not possible as the 
least GCD sequence will be 1+2+3+4 which
is greater then n, hence print -1.

方法:
最常见的观察结果是该系列的gcd始终是n的除数。最大gcd可能的值(例如b )将为n / sum,其中sum是1 + 2 + .. m的总和。
如果b变为0,则1 + 2 + 3 .. + k的总和超过n,这是无效的,因此输出“ -1”。
遍历以找出所有可能的除数,直到sqrt(n)为止。如果当前除数为i,则采用该级数的最佳可能方法是考虑i,2 * i,3 * i,…(m-1)* i,它们的和为s,等于i *( m *(m-1))/ 2 。最后一个数字为ns。
除i是除数外,n / i将是另一个除数,因此也请检查该除数。
取最大可能除数(例如r )(应小于或等于b) ,并将序列打印为r,2 * r,…(m-1)* r,ns。
如果找不到这样的除数,则只需输出“ -1”。

C++
// CPP program to find the series with largest
// GCD and sum equals to n
#include 
using namespace std;
 
// function to generate and print the sequence
void print_sequence(int n, int k)
{
    // stores the maximum gcd that can be
    // possible of sequence.
    int b = n / (k * (k + 1) / 2);
 
    // if maximum gcd comes out to be
    // zero then not possible
    if (b == 0) {
        cout << -1 << endl;
 
    } else {
 
        // the smallest gcd possible is 1
        int r = 1;
 
        // traverse the array to find out
        // the max gcd possible
        for (int x = 1; x * x <= n; x++) {
 
            // checks if the number is
            // divisible or not
            if (n % x != 0)
                continue;
 
            // checks if x is smaller then
            // the max gcd possible and x
            // is greater then the resultant
            // gcd till now, then r=x
            if (x <= b && x > r)
                r = x;
 
            // checks if n/x is smaller than
            // the max gcd possible and n/x
            // is greater then the resultant
            // gcd till now, then r=x
            if (n / x <= b && n / x > r)
                r = n / x;
        }
 
        // traverses and prints d, 2d, 3d,
        // ..., (k-1)·d,
        for (int i = 1; i < k; i++)
            cout << r * i << " ";
 
        // computes the last element of
        // the sequence n-s.
        int res = n - (r * (k * (k - 1) / 2));
 
        // prints the last element
        cout << res << endl;
    }
}
 
// driver program to test the above function
int main()
{
    int n = 24;
    int k = 4;
    print_sequence(n, k);
 
    n = 24, k = 5;
    print_sequence(n, k);
 
    n = 6, k = 4;
    print_sequence(n, k);
}


Java
// Java program to find the series with
// largest GCD and sum equals to n
import java.io.*;
 
class GFG {
 
// function to generate and print the sequence
static void print_sequence(int n, int k)
{
    // stores the maximum gcd that can be
    // possible of sequence.
    int b = n / (k * (k + 1) / 2);
 
    // if maximum gcd comes out to be
    // zero then not possible
    if (b == 0) {
        System.out.println("-1");
 
    } else {
 
        // the smallest gcd possible is 1
        int r = 1;
 
        // traverse the array to find out
        // the max gcd possible
        for (int x = 1; x * x <= n; x++) {
 
            // checks if the number is
            // divisible or not
            if (n % x != 0)
                continue;
 
            // checks if x is smaller then
            // the max gcd possible and x
            // is greater then the resultant
            // gcd till now, then r=x
            if (x <= b && x > r)
                r = x;
 
            // checks if n/x is smaller than
            // the max gcd possible and n/x
            // is greater then the resultant
            // gcd till now, then r=x
            if (n / x <= b && n / x > r)
                r = n / x;
        }
 
        // traverses and prints d, 2d, 3d,..., (k-1)
        for (int i = 1; i < k; i++)
            System.out.print(r * i + " ");
 
        // computes the last element of
        // the sequence n-s.
        int res = n - (r * (k * (k - 1) / 2));
 
        // prints the last element
        System.out.println(res);
    }
}
 
// driver program to test the above function
public static void main(String[] args)
{
    int n = 24;
    int k = 4;
    print_sequence(n, k);
 
    n = 24; k = 5;
    print_sequence(n, k);
 
    n = 6; k = 4;
    print_sequence(n, k);
}
}
 
// This code is contributed by Prerna Saini


Python3
# Python3 code to find the series
# with largest GCD and sum equals to n
 
def print_sequence(n, k):
     
    # stores the maximum gcd that
    # can be possible of sequence.
     
    b = int(n / (k * (k + 1) / 2));
     
 
    # if maximum gcd comes out to be
    # zero then not possible
     
    if b == 0:
        print ("-1")
 
    else:
        # the smallest gcd possible is 1
        r = 1;
 
        # traverse the array to find out
        # the max gcd possible
        x = 1
         
        while x ** 2 <= n:
             
            # checks if the number is
            # divisible or not
            if n % x != 0:
             
                # x = x + 1
                continue;
                 
             
            # checks if x is smaller then
            # the max gcd possible and x
            # is greater then the resultant
            # gcd till now, then r=x
            elif x <= b and x > r:
                r = x
                # x = x + 1
 
            # checks if n/x is smaller than
            # the max gcd possible and n/x
            # is greater then the resultant
            # gcd till now, then r=x
            elif n / x <= b and n / x > r :
                r = n / x
                # x = x + 1
                 
            x = x + 1
         
 
    # traverses and prints d, 2d, 3d,
    # ..., (k-1)·d,
        i = 1
        while i < k :
            print (r * i, end = " ")
            i = i + 1
             
        last_term = n - (r * (k * (k - 1) / 2))
        print (last_term)
         
         
             
         
# main driver
print_sequence(24,4)
print_sequence(24,5)
print_sequence(6,4)
 
# This code is contributed by Saloni Gupta


C#
// C# program to find the series with
// largest GCD and sum equals to n
using System;
 
class GFG {
 
// function to generate and
// print the sequence
static void print_sequence(int n, int k)
{
     
    // stores the maximum gcd that can be
    // possible of sequence.
    int b = n / (k * (k + 1) / 2);
 
    // if maximum gcd comes out to be
    // zero then not possible
    if (b == 0)
    {
        Console.Write("-1");
 
    }
    else
    {
 
        // the smallest gcd possible is 1
        int r = 1;
 
        // traverse the array to find out
        // the max gcd possible
        for (int x = 1; x * x <= n; x++)
        {
 
            // checks if the number is
            // divisible or not
            if (n % x != 0)
                continue;
 
            // checks if x is smaller then
            // the max gcd possible and x
            // is greater then the resultant
            // gcd till now, then r=x
            if (x <= b && x > r)
                r = x;
 
            // checks if n/x is smaller than
            // the max gcd possible and n/x
            // is greater then the resultant
            // gcd till now, then r=x
            if (n / x <= b && n / x > r)
                r = n / x;
        }
 
        // traverses and prints d, 2d,
        // 3d,..., (k-1)
        for (int i = 1; i < k; i++)
        Console.Write(r * i + " ");
 
        // computes the last element of
        // the sequence n-s.
        int res = n - (r * (k *
                  (k - 1) / 2));
 
        // prints the last element
        Console.WriteLine(res);
    }
}
 
// Driver Code
public static void Main()
{
    int n = 24;
    int k = 4;
    print_sequence(n, k);
 
    n = 24; k = 5;
    print_sequence(n, k);
 
    n = 6; k = 4;
    print_sequence(n, k);
}
}
 
// This code is contributed by Nitin Mittal.


PHP
 $r)
                $r = $x;
 
            // checks if n/x is smaller than
            // the max gcd possible and n/x
            // is greater then the resultant
            // gcd till now, then r=x
            if ($n / $x <= $b && $n / $x > $r)
                $r = $n / $x;
        }
 
        // traverses and prints d, 2d, 3d,
        // ..., (k-1)·d,
        for ($i = 1; $i < $k; $i++)
            echo($r * $i . " ");
 
        // computes the last element of
        // the sequence n-s.
        $res = $n - ($r * ($k * ($k - 1) / 2));
 
        // prints the last element
        echo($res . "\n");
    }
}
 
// Driver Code
$n = 24;
$k = 4;
print_sequence($n, $k);
 
$n = 24; $k = 5;
print_sequence($n, $k);
 
$n = 6; $k = 4;
print_sequence($n, $k);
 
// This code is contributed by Ajit.
?>


Javascript


输出 :

2 4 6 12
1 2 3 4 14
-1