📜  从 1 到 a 和 1 到 b 的对数,其总和可被 N 整除

📅  最后修改于: 2021-10-26 02:30:45             🧑  作者: Mango

给定三个整数abN 。找出可以通过从1 到 a 中选择一个整数以及从1 到 b 中选择一个整数来形成的不同对的总数,使得它们的总和可以被 N 整除。

例子:

Input : a = 4, b = 4, N = 4
Output : 4

Input : a = 5, b = 13, N = 3
Output : 22

基本方法:对于可以被N整除的一对,它必须包含一个从 1 到 a 的范围和另一个从 1 到 b 的数字。
因此,对于从 1 到 a 的整数迭代,对于每个整数 (i),存在 b/N 个数字,其与 i 的总和将被 N 整除。此外,如果 (i%N + b%N) >= N 那么存在另外 1 对,其总和可被 N 整除。

例如取 a = 7, b = 6 和 N = 4:

Let's check for i = 3:
  • b/N = 6/4 = 1 => 从 1 到 b 有一个整数,
    其总和与 3 可被 4 整除,即(3, 1)。
  • 还有 i%N + b%N = 3%4 + 6%4 = 3+2 = 5 > 4,
    表示从 1 到 b 还存在一个整数
    其总和与 3 可被 4 整除,即(3, 5)。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // Iterate over 1 to a to find distinct pairs
    for (int i = 1; i <= a; i++) {
        // For each integer from 1 to a
        // b/n integers exists such that pair
        // sum is divisible by n
        ans += b / n;
 
        // If (i%n +b%n ) >= n one more pair is possible
        ans += (i % n + b % n) >= n ? 1 : 0;
    }
 
    // Return answer
    return ans;
}
 
// Driver code
int main()
{
    int a = 5, b = 13, n = 3;
    cout << findCountOfPairs(a, b, n);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
static int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // Iterate over 1 to a to find distinct pairs
    for (int i = 1; i <= a; i++)
    {
        // For each integer from 1 to a
        // b/n integers exists such that pair
        // sum is divisible by n
        ans += b / n;
 
        // If (i%n +b%n ) >= n one more pair is possible
        ans += (i % n + b % n) >= n ? 1 : 0;
    }
 
    // Return answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a = 5, b = 13, n = 3;
    System.out.println(findCountOfPairs(a, b, n));
}
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python implementation of above approach
 
# Function to find the distinct pairs from
# 1-a & 1-b such that their sum is divisible by n.
def findCountOfPairs(a, b, n):
    ans = 0
    for i in range(1, a + 1):
 
        # For each integer from 1 to a
        # b/n integers exists such that pair
        # / sum is divisible by n
        ans += b//n
 
        # If (i%n +b%n ) >= n one more pair is possible
        ans += 1 if (i % n + b % n) >= n else 0
 
    return ans
 
# Driver code
a = 5; b = 13; n = 3
print(findCountOfPairs(a, b, n))
 
# This code is contributed by Shrikant13


C#
// C# program for the above approach
using System;
 
class GFG
{
     
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
static int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // Iterate over 1 to a to find distinct pairs
    for (int i = 1; i <= a; i++)
    {
        // For each integer from 1 to a
        // b/n integers exists such that pair
        // sum is divisible by n
        ans += b / n;
 
        // If (i%n +b%n ) >= n one more pair is possible
        ans += (i % n + b % n) >= n ? 1 : 0;
    }
 
    // Return answer
    return ans;
}
 
// Driver code
static public void Main ()
{
    int a = 5, b = 13, n = 3;
    Console.WriteLine(findCountOfPairs(a, b, n));
}
}
 
// This code has been contributed by ajit.


PHP
= n one more
        // pair is possible
        $ans += (($i % $n ) +
                 ($b % $n)) >= $n ? 1 : 0;
    }
 
    // Return answer
    return $ans;
}
 
// Driver code
$a = 5;
$b = 13;
$n = 3;
echo findCountOfPairs($a, $b, $n);
 
// This code is contributed by akt_mit.
?>


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
int findCountOfPairs(int a, int b, int n)
{
    if (a > b)
    {
        // if first element is bigger then swap
        swap(a, b);
    }
    int temp = 1, count = 0;
    // count is store the number of pair.
    for (int i = n; temp > 0; i += n)
    {
        // we use temp for breking a loop.
        if (a >= i)
        {
            // count when a is greater.
            temp = i - 1;
        }
        else if (b >= i)
        {
            // Count when a is smaller but
            // b is greater
            temp = a;
        }
        else if (i > b)
        {
            // Count when a and b both are smaller
            temp = a - (i - b) + 1;
        }
        if (temp > 0) //breaking condition
        {
            // For storing The pair in count.
            count += temp;
        }
    }
    // return the number of pairs.
    return count;
}
 
// Driver code
int main()
{
    int a = 5, b = 13, n = 3;
    cout << findCountOfPairs(a, b, n);
 
    return 0;
}
// contribute by Vivek Javiya


Java
// Java implementation of
// the above approach
class GFG{
     
// Function to find the
// distinct pairs from
// 1-a & 1-b such that
// their sum is divisible by n.
public static int findCountOfPairs(int a,
                                   int b,
                                   int n)
{
  if (a > b)
  {
    // if first element is
    // bigger then swap
    int temp = a;
    a = b;
    b = temp;
  }
   
  int temp = 1, count = 0;
   
  // count is store the
  // number of pair.
  for (int i = n; temp > 0; i += n)
  {
    // we use temp for
    // breaking a loop.
    if (a >= i)
    {
      // count when a
      // is greater.
      temp = i - 1;
    }
    else if (b >= i)
    {
      // Count when a is
      // smaller but
      // b is greater
      temp = a;
    }
    else if (i > b)
    {
      // Count when a and b
      // both are smaller
      temp = a - (i - b) + 1;
    }
     
    //breaking condition
    if (temp > 0)
    {
      // For storing The
      // pair in count.
      count += temp;
    }
  }
   
  // return the number
  // of pairs.
  return count;
}
   
// Driver code
public static void main(String[] args)
{
  int a = 5, b = 13, n = 3;
  System.out.print(findCountOfPairs(a,
                                    b, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python implementation of above approach
 
# Function to find the distinct pairs from
# 1-a & 1-b such that their sum is divisible by n.
def findCountOfPairs(a, b, n):
    if(a > b):
       
        # if first element is bigger then swap
        a, b = b, a
 
    temp = 1
    count = 0
     
    # count is store the number of pair.
    i = n
    while(temp > 0):
       
        # we use temp for breking a loop.
        if(a >= i):
           
            # count when a is greater.
            temp = i - 1
        elif(b >= i):
           
            # Count when a is smaller but
            # b is greater
            temp = a
        elif(i > b):
           
            # Count when a and b both are smaller
            temp = a - (i - b) + 1
 
        if(temp > 0):
           
          # breaking condition
            # For storing The pair in count.
            count += temp
        i += n
 
    # return the number of pairs.
    return count
 
# Driver code
a = 5
b = 13
n = 3
 
print(findCountOfPairs(a, b, n))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# implementation of
// the above approach
using System;
 
class GFG
{
     
    // Function to find the
    // distinct pairs from
    // 1-a & 1-b such that
    // their sum is divisible by n.
    static int findCountOfPairs(int a, int b, int n)
    {
      if (a > b)
      {
         
        // if first element is
        // bigger then swap
        int temp1 = a;
        a = b;
        b = temp1;
      }
        
      int temp = 1, count = 0;
        
      // count is store the
      // number of pair.
      for (int i = n; temp > 0; i += n)
      {
         
        // we use temp for
        // breaking a loop.
        if (a >= i)
        {
           
          // count when a
          // is greater.
          temp = i - 1;
        }
        else if (b >= i)
        {
           
          // Count when a is
          // smaller but
          // b is greater
          temp = a;
        }
        else if (i > b)
        {
           
          // Count when a and b
          // both are smaller
          temp = a - (i - b) + 1;
        }
          
        // breaking condition
        if (temp > 0)
        {
           
          // For storing The
          // pair in count.
          count += temp;
        }
      }
        
      // return the number
      // of pairs.
      return count;
    }
   
  // Driver code
  static void Main()
  {
      int a = 5, b = 13, n = 3;
      Console.WriteLine(findCountOfPairs(a, b, n));
  }
}
 
// This code is contributed by divyesh072019


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * (a / n) * (b / n);
 
    // pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += (a / n) * (b % n);
 
    // pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * (b / n);
 
    // pairs from n*(a/n) to a and  n*(b/n) to b
    ans += ((a % n) + (b % n)) / n;
 
    // Return answer
    return ans;
}
 
// Driver code
int main()
{
    int a = 5, b = 13, n = 3;
    cout << findCountOfPairs(a, b, n);
 
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
 
class GFG
{
     
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
static int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * (a / n) * (b / n);
 
    // pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += (a / n) * (b % n);
 
    // pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * (b / n);
 
    // pairs from n*(a/n) to a and n*(b/n) to b
    ans += ((a % n) + (b % n)) / n;
 
    // Return answer
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
    int a = 5, b = 13, n = 3;
    System.out.println (findCountOfPairs(a, b, n));
}
}
 
// This code is contributed by ajit..


Python3
# Python 3 implementation of above approach
 
# Function to find the distinct pairs from
# 1-a & 1-b such that their sum is divisible by n.
def findCountOfPairs(a, b, n):
    ans = 0
 
    # pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * int(a / n) * int(b / n)
 
    # pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += int(a / n) * (b % n)
 
    # pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * int(b / n)
 
    # pairs from n*(a/n) to a and n*(b/n) to b
    ans += int(((a % n) + (b % n)) / n);
 
    # Return answer
    return ans
 
# Driver code
if __name__ == '__main__':
    a = 5
    b = 13
    n = 3
    print(findCountOfPairs(a, b, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of above approach
using System;
 
class GFG
{
         
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
 
static int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * (a / n) * (b / n);
 
    // pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += (a / n) * (b % n);
 
    // pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * (b / n);
 
    // pairs from n*(a/n) to a and n*(b/n) to b
    ans += ((a % n) + (b % n)) / n;
 
    // Return answer
    return ans;
}
 
// Driver code
    static public void Main (){
    int a = 5, b = 13, n = 3;
    Console.WriteLine(findCountOfPairs(a, b, n));
}
}
 
// This code is contributed by @Tushil


PHP


Javascript


输出:
22

时间复杂度:O(N)

第二种方法:-这种方法有点棘手。在这里,我们找到 N 的倍数有多少对。

第一:-保持(a

第二:-从 N 的最低倍数开始 for 循环并遍历倍数。

  • 现在最小元素 ( a ) 大于或等于当前倍数,然后我们将 ((Current_Multiple) – 1) 对添加到 ans。
  • 现在 a 更小但 b 大于或等于我们向 ans 添加 a 的当前倍数。
  • 现在,如果 a 和 b 都小于我们计算剩余的对添加 a – (current_multiple – b ) + 1。
  • 打破循环。

下面是上述逻辑的实现。

C++

// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
int findCountOfPairs(int a, int b, int n)
{
    if (a > b)
    {
        // if first element is bigger then swap
        swap(a, b);
    }
    int temp = 1, count = 0;
    // count is store the number of pair.
    for (int i = n; temp > 0; i += n)
    {
        // we use temp for breking a loop.
        if (a >= i)
        {
            // count when a is greater.
            temp = i - 1;
        }
        else if (b >= i)
        {
            // Count when a is smaller but
            // b is greater
            temp = a;
        }
        else if (i > b)
        {
            // Count when a and b both are smaller
            temp = a - (i - b) + 1;
        }
        if (temp > 0) //breaking condition
        {
            // For storing The pair in count.
            count += temp;
        }
    }
    // return the number of pairs.
    return count;
}
 
// Driver code
int main()
{
    int a = 5, b = 13, n = 3;
    cout << findCountOfPairs(a, b, n);
 
    return 0;
}
// contribute by Vivek Javiya

Java

// Java implementation of
// the above approach
class GFG{
     
// Function to find the
// distinct pairs from
// 1-a & 1-b such that
// their sum is divisible by n.
public static int findCountOfPairs(int a,
                                   int b,
                                   int n)
{
  if (a > b)
  {
    // if first element is
    // bigger then swap
    int temp = a;
    a = b;
    b = temp;
  }
   
  int temp = 1, count = 0;
   
  // count is store the
  // number of pair.
  for (int i = n; temp > 0; i += n)
  {
    // we use temp for
    // breaking a loop.
    if (a >= i)
    {
      // count when a
      // is greater.
      temp = i - 1;
    }
    else if (b >= i)
    {
      // Count when a is
      // smaller but
      // b is greater
      temp = a;
    }
    else if (i > b)
    {
      // Count when a and b
      // both are smaller
      temp = a - (i - b) + 1;
    }
     
    //breaking condition
    if (temp > 0)
    {
      // For storing The
      // pair in count.
      count += temp;
    }
  }
   
  // return the number
  // of pairs.
  return count;
}
   
// Driver code
public static void main(String[] args)
{
  int a = 5, b = 13, n = 3;
  System.out.print(findCountOfPairs(a,
                                    b, n));
}
}
 
// This code is contributed by divyeshrabadiya07

蟒蛇3

# Python implementation of above approach
 
# Function to find the distinct pairs from
# 1-a & 1-b such that their sum is divisible by n.
def findCountOfPairs(a, b, n):
    if(a > b):
       
        # if first element is bigger then swap
        a, b = b, a
 
    temp = 1
    count = 0
     
    # count is store the number of pair.
    i = n
    while(temp > 0):
       
        # we use temp for breking a loop.
        if(a >= i):
           
            # count when a is greater.
            temp = i - 1
        elif(b >= i):
           
            # Count when a is smaller but
            # b is greater
            temp = a
        elif(i > b):
           
            # Count when a and b both are smaller
            temp = a - (i - b) + 1
 
        if(temp > 0):
           
          # breaking condition
            # For storing The pair in count.
            count += temp
        i += n
 
    # return the number of pairs.
    return count
 
# Driver code
a = 5
b = 13
n = 3
 
print(findCountOfPairs(a, b, n))
 
# This code is contributed by avanitrachhadiya2155

C#

// C# implementation of
// the above approach
using System;
 
class GFG
{
     
    // Function to find the
    // distinct pairs from
    // 1-a & 1-b such that
    // their sum is divisible by n.
    static int findCountOfPairs(int a, int b, int n)
    {
      if (a > b)
      {
         
        // if first element is
        // bigger then swap
        int temp1 = a;
        a = b;
        b = temp1;
      }
        
      int temp = 1, count = 0;
        
      // count is store the
      // number of pair.
      for (int i = n; temp > 0; i += n)
      {
         
        // we use temp for
        // breaking a loop.
        if (a >= i)
        {
           
          // count when a
          // is greater.
          temp = i - 1;
        }
        else if (b >= i)
        {
           
          // Count when a is
          // smaller but
          // b is greater
          temp = a;
        }
        else if (i > b)
        {
           
          // Count when a and b
          // both are smaller
          temp = a - (i - b) + 1;
        }
          
        // breaking condition
        if (temp > 0)
        {
           
          // For storing The
          // pair in count.
          count += temp;
        }
      }
        
      // return the number
      // of pairs.
      return count;
    }
   
  // Driver code
  static void Main()
  {
      int a = 5, b = 13, n = 3;
      Console.WriteLine(findCountOfPairs(a, b, n));
  }
}
 
// This code is contributed by divyesh072019

Javascript


输出 :

22

有效的方法:为了有效地解决问题,将其分为四部分并解决为:

  • 从 1 到 N*(a/N) 范围内的每个整数将恰好有从 1 到 N*(b/N) 的 b/N 个整数,其总和可被 N 整除。
  • 存在范围从 1 到 N*(a/N) 的 a/N 整数,它们可以与范围从 N*(b/N) 到 b 的 b%N 整数成对。
  • 存在从 N*(a/N) 到 a 的 a%N 个整数,它们可以与从 1 到 N*(b/N) 的 b/N 整数成对。
  • 存在 (a%N + b%N)/N 个从 N*(a/N) 到 a 和从 N*(b/N) 到 b 的整数,它们可以组成对,其总和可以被 N 整除。

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * (a / n) * (b / n);
 
    // pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += (a / n) * (b % n);
 
    // pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * (b / n);
 
    // pairs from n*(a/n) to a and  n*(b/n) to b
    ans += ((a % n) + (b % n)) / n;
 
    // Return answer
    return ans;
}
 
// Driver code
int main()
{
    int a = 5, b = 13, n = 3;
    cout << findCountOfPairs(a, b, n);
 
    return 0;
}

Java

// Java implementation of above approach
import java.io.*;
 
class GFG
{
     
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
static int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * (a / n) * (b / n);
 
    // pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += (a / n) * (b % n);
 
    // pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * (b / n);
 
    // pairs from n*(a/n) to a and n*(b/n) to b
    ans += ((a % n) + (b % n)) / n;
 
    // Return answer
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
    int a = 5, b = 13, n = 3;
    System.out.println (findCountOfPairs(a, b, n));
}
}
 
// This code is contributed by ajit..

蟒蛇3

# Python 3 implementation of above approach
 
# Function to find the distinct pairs from
# 1-a & 1-b such that their sum is divisible by n.
def findCountOfPairs(a, b, n):
    ans = 0
 
    # pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * int(a / n) * int(b / n)
 
    # pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += int(a / n) * (b % n)
 
    # pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * int(b / n)
 
    # pairs from n*(a/n) to a and n*(b/n) to b
    ans += int(((a % n) + (b % n)) / n);
 
    # Return answer
    return ans
 
# Driver code
if __name__ == '__main__':
    a = 5
    b = 13
    n = 3
    print(findCountOfPairs(a, b, n))
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of above approach
using System;
 
class GFG
{
         
// Function to find the distinct pairs from
// 1-a & 1-b such that their sum is divisible by n.
 
static int findCountOfPairs(int a, int b, int n)
{
    int ans = 0;
 
    // pairs from 1 to n*(a/n) and 1 to n*(b/n)
    ans += n * (a / n) * (b / n);
 
    // pairs from 1 to n*(a/n) and n*(b/n) to b
    ans += (a / n) * (b % n);
 
    // pairs from n*(a/n) to a and 1 to n*(b/n)
    ans += (a % n) * (b / n);
 
    // pairs from n*(a/n) to a and n*(b/n) to b
    ans += ((a % n) + (b % n)) / n;
 
    // Return answer
    return ans;
}
 
// Driver code
    static public void Main (){
    int a = 5, b = 13, n = 3;
    Console.WriteLine(findCountOfPairs(a, b, n));
}
}
 
// This code is contributed by @Tushil

PHP


Javascript


输出:
22

时间复杂度: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程