📌  相关文章
📜  x和y的乘积总和,使得楼板(n / x)= y

📅  最后修改于: 2021-05-04 14:30:50             🧑  作者: Mango

给定正整数n 。任务是找到xy的乘积之和,使得⌊n/x⌋= y (整数除法)。

例子:

Input : n = 5
Output : 21
Following are the possible pairs of (x, y):
(1, 5), (2, 2), (3, 1), (4, 1), (5, 1).
So, 1*5 + 2*2 + 3*1 + 4*1 + 5*1 
   = 5 + 4 + 3 + 4 + 5 
   = 21.

Input : n = 10
Output : 87

方法1(蛮力):
将x从1迭代到n以找到y。然后在每次迭代中将x * y添加到答案中。

以下是此方法的实现:

C++
// C++ program to find sum of product of x and y
// such that n/x = y (Integer Division)
#include
using namespace std;
  
// Return the sum of product x*y.
int sumofproduct(int n)
{
    int ans = 0;
  
    // Iterating x from 1 to n
    for (int x = 1; x <= n; x++)
    {
        // Finding y = n/x.
        int y = n/x;
  
        // Adding product of x and y to answer.
        ans += (y * x);
    }
  
    return ans;
}
  
// Driven Program
int main()
{
    int n = 10;
    cout << sumofproduct(n) << endl;
    return 0;
}


Java
// Java program to find sum of 
// product of x and y such that
// n/x = y (Integer Division) 
import java.io.*;
  
class GFG {
      
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int ans = 0;
  
    // Iterating x from 1 to n
    for (int x = 1; x <= n; x++)
    {
        // Finding y = n/x.
        int y = n / x;
  
        // Adding product of x and 
        // y to answer.
        ans += (y * x);
    }
  
    return ans;
}
  
    // Driver Code
    static public void main(String[] args)
    {
        int n = 10;
        System.out.println(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


Python3
# Python3 program to find sum of
# product of x and y such that 
# n/x = y (Integer Division)
  
# Return the sum of product x*y
def sumofproduct(n):
    ans = 0
  
    # Iterating x from 1 to n
    for x in range(1, n + 1):
          
        # Finding y = n/x.
        y = int(n / x)
  
        # Adding product of x and y to answer.
        ans += (y * x)
  
    return ans
  
# Driven Program
n = 10
print (sumofproduct(n))
  
#This code is Shreyanshi Arun


C#
// C# program to find sum of 
// product of x and y such that
// n/x = y (Integer Division) 
using System;
  
class GFG {
      
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int ans = 0;
  
    // Iterating x from 1 to n
    for (int x = 1; x <= n; x++)
    {
        // Finding y = n/x.
        int y = n / x;
  
        // Adding product of x and 
        // y to answer.
        ans += (y * x);
    }
  
    return ans;
}
  
    // Driver Code
    static public void Main(String[] args)
    {
        int n = 10;
        Console.WriteLine(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


PHP


C++
// C++ program to find sum of product of x and y
// such that n/x = y (Integer Division)
#include
using namespace std;
  
// Return the sum of natural number in a range.
int sumOfRange(int a, int b)
{
    // n*(n+1)/2.
    int i = (a * (a+1)) >> 1;
    int j = (b * (b+1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = sqrt(n);
    for (int i=1; i<=root; i++)
    {
        // Finding the upper limit.
        int up = n/i;
  
        // Finding the lower limit.
        int low = max(n/(i+1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n/i));
    }
  
    return sum;
}
  
// Driven Program
int main()
{
    int n = 10;
    cout << sumofproduct(n) << endl;
    return 0;
}


Java
// Java program to find sum of 
// product of x and y such that
// n / x = y (Integer Division)
import java.io.*;
  
class GFG {
      
// Return the sum of natural number in a range.
static int sumOfRange(int a, int b)
{
    // n * (n + 1) / 2.
    int i = (a * (a + 1)) >> 1;
    int j = (b * (b + 1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = (int)Math.sqrt(n);
    for (int i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        int up = n / i;
  
        // Finding the lower limit.
        int low = Math.max(n / (i + 1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n / i));
    }
  
    return sum;
}
  
    // Driver Code
    static public void main(String[] args)
    {
        int n = 10;
        System.out.println(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


Python3
# Python3 program to find sum 
# of product of x and y such 
# that n/x = y (Integer Division)
import math
  
# Return the sum of natural 
# number in a range.
def sumOfRange(a, b):
    # n*(n+1)/2.
    i = (a * (a + 1)) >> 1;
    j = (b * (b + 1)) >> 1;
    return (i - j);
  
# Return the sum of product x*y.
def sumofproduct(n):
    sum = 0;
  
    # Iterating i from 1 to sqrt(n)
    root = int(math.sqrt(n));
    for i in range(1, root + 1):
        # Finding the upper limit.
        up = int(n / i);
  
        # Finding the lower limit.
        low = max(int(n / (i + 1)), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * int(n / i));
  
    return sum;
  
# Driven Code
n = 10;
print(sumofproduct(n));
      
# This code is contributed by mits


C#
// C# program to find sum of 
// product of x and y such that
// n / x = y (Integer Division)
using System;
  
class GFG {
      
// Return the sum of natural number in a range.
static int sumOfRange(int a, int b)
{
    // n * (n + 1) / 2.
    int i = (a * (a + 1)) >> 1;
    int j = (b * (b + 1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = (int)Math.Sqrt(n);
    for (int i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        int up = n / i;
  
        // Finding the lower limit.
        int low = Math.Max(n / (i + 1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n / i));
    }
  
    return sum;
}
  
    // Driver Code
    static public void Main(String[] args)
    {
        int n = 10;
        Console.WriteLine(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


PHP
> 1;
    $j = ($b * ($b + 1)) >> 1;
    return ($i - $j);
}
  
// Return the sum of product x*y.
function sumofproduct($n)
{
    $sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    $root = sqrt($n);
    for ($i = 1; $i <= $root; $i++)
    {
        // Finding the upper limit.
        $up = (int)($n / $i);
  
        // Finding the lower limit.
        $low = max((int)($n / ($i + 1)), $root);
  
        $sum += ($i * sumOfRange($up, $low));
        $sum += ($i * (int)($n / $i));
    }
  
    return $sum;
}
  
// Driven Code
$n = 10;
echo sumofproduct($n) . "\n";
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)
?>


输出 :

87

时间复杂度: O(n)

方法2(有效方法):
让我们求解n = 10,所以
x = 1,y = 10
x = 2,y = 5
x = 3,y = 3
x = 4,y = 2
x = 5,y = 2
x = 6,y = 1
x = 7,y = 1
x = 8,y = 1
x = 9,y = 1
x = 10,y = 1

因此,我们的答案将是1 * 10 + 2 * 5 + 3 * 3 + 4 * 2 + 5 * 2 + 6 * 1 + 7 * 1 + 8 * 1 + 9 * 1 + 10 * 1。

现在,观察y的某些值是否重复。另外,请注意它们在x的某个连续值范围内重复,例如y = 1在x = 6至10时重复。

因此,不要像方法1那样为x的所有值(1到n)找到y的值,而是尝试找到x的较低和较高的值,对于y来说,y的可能值类似于y = 1尝试找到x = 6的较低值和x = 10的较高值。现在,观察较低的值将是(n /(y + 1))+1,而较高的值将是(n / y)。求出x范围的总和,然后乘以y,然后加到答案中。

如何找到y的可能值?
观察到,当y小于或等于x时,y的所有值都从1到sqrt(n)。因此,对于y = 1到sqrt(n),找到每个y的x的上下限。对于n = 10,
y = 1,lo = 6,hi = 10,ans + =(6 + 7 + 8 + 9 + 10)* 1
y = 2,lo = 4,hi = 5,ans + =(4 + 5)* 2
y = 3,lo = 3,hi = 3,ans + =(3)* 3

对于要添加的其他值(对于y = 10和n = 10中的5),请注意可以在上述步骤中找到它们,对于每个y,在答案中添加y *(n / y)。
对于n = 10,
y = 1,ans + = 1 *(10/1)
y = 2,ans + = 2 *(10/2)。

以下是此方法的实现:

C++

// C++ program to find sum of product of x and y
// such that n/x = y (Integer Division)
#include
using namespace std;
  
// Return the sum of natural number in a range.
int sumOfRange(int a, int b)
{
    // n*(n+1)/2.
    int i = (a * (a+1)) >> 1;
    int j = (b * (b+1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = sqrt(n);
    for (int i=1; i<=root; i++)
    {
        // Finding the upper limit.
        int up = n/i;
  
        // Finding the lower limit.
        int low = max(n/(i+1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n/i));
    }
  
    return sum;
}
  
// Driven Program
int main()
{
    int n = 10;
    cout << sumofproduct(n) << endl;
    return 0;
}

Java

// Java program to find sum of 
// product of x and y such that
// n / x = y (Integer Division)
import java.io.*;
  
class GFG {
      
// Return the sum of natural number in a range.
static int sumOfRange(int a, int b)
{
    // n * (n + 1) / 2.
    int i = (a * (a + 1)) >> 1;
    int j = (b * (b + 1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = (int)Math.sqrt(n);
    for (int i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        int up = n / i;
  
        // Finding the lower limit.
        int low = Math.max(n / (i + 1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n / i));
    }
  
    return sum;
}
  
    // Driver Code
    static public void main(String[] args)
    {
        int n = 10;
        System.out.println(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.

Python3

# Python3 program to find sum 
# of product of x and y such 
# that n/x = y (Integer Division)
import math
  
# Return the sum of natural 
# number in a range.
def sumOfRange(a, b):
    # n*(n+1)/2.
    i = (a * (a + 1)) >> 1;
    j = (b * (b + 1)) >> 1;
    return (i - j);
  
# Return the sum of product x*y.
def sumofproduct(n):
    sum = 0;
  
    # Iterating i from 1 to sqrt(n)
    root = int(math.sqrt(n));
    for i in range(1, root + 1):
        # Finding the upper limit.
        up = int(n / i);
  
        # Finding the lower limit.
        low = max(int(n / (i + 1)), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * int(n / i));
  
    return sum;
  
# Driven Code
n = 10;
print(sumofproduct(n));
      
# This code is contributed by mits

C#

// C# program to find sum of 
// product of x and y such that
// n / x = y (Integer Division)
using System;
  
class GFG {
      
// Return the sum of natural number in a range.
static int sumOfRange(int a, int b)
{
    // n * (n + 1) / 2.
    int i = (a * (a + 1)) >> 1;
    int j = (b * (b + 1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = (int)Math.Sqrt(n);
    for (int i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        int up = n / i;
  
        // Finding the lower limit.
        int low = Math.Max(n / (i + 1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n / i));
    }
  
    return sum;
}
  
    // Driver Code
    static public void Main(String[] args)
    {
        int n = 10;
        Console.WriteLine(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.

的PHP

> 1;
    $j = ($b * ($b + 1)) >> 1;
    return ($i - $j);
}
  
// Return the sum of product x*y.
function sumofproduct($n)
{
    $sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    $root = sqrt($n);
    for ($i = 1; $i <= $root; $i++)
    {
        // Finding the upper limit.
        $up = (int)($n / $i);
  
        // Finding the lower limit.
        $low = max((int)($n / ($i + 1)), $root);
  
        $sum += ($i * sumOfRange($up, $low));
        $sum += ($i * (int)($n / $i));
    }
  
    return $sum;
}
  
// Driven Code
$n = 10;
echo sumofproduct($n) . "\n";
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)
?>

输出:

87

时间复杂度: O((&Sqrt; n)

来源:
https://www.quora.com/What-is-the-the-fastest-way-to-solve-the-problem-that-states-given-a-number-N-find-the-sum-of-all-产品x * y这样的Nxy整数除法在这里N将是N-10