📌  相关文章
📜  使得((n%i)%j)%n最大化的(i,j)对的计数

📅  最后修改于: 2021-04-29 12:47:34             🧑  作者: Mango

给定整数n ,任务是计算对(i,j)的对数,以使((n%i)%j)%n最大化,其中1≤i,j≤n

例子:

简单方法:为了获得最大的剩余值,n具有由(N / 2)+ 1进行划分。存储max = n%((n / 2)+ 1) ,现在检查ij的所有可能值。如果(((n%i)%j)%n = max,则更新计数= count +1 。最后打印计数

C++
// CPP implementation of the approach
#include
using namespace std;
  
// Function to return the count of required pairs
int countPairs(int n)
{
    // Number which will give the max value
    // for ((n % i) % j) % n
    int num = ((n / 2) + 1);
      
    // To store the maximum possible value of
    // ((n % i) % j) % n
    int max = n % num;
  
    // To store the count of possible pairs
    int count = 0;
  
    // Check all possible pairs
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= n; j++)
        {
  
            // Calculating the value of ((n % i) % j) % n
            int val = ((n % i) % j) % n;
  
            // If value is equal to maximum
            if (val == max)
                count++;
        }
    }
  
    // Return the number of possible pairs
    return count;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << (countPairs(n));
}
  
// This code is contributed by 
// Surendra_Gangwar


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the count of required pairs
    public static int countPairs(int n)
    {
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // To store the count of possible pairs
        int count = 0;
  
        // Check all possible pairs
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
  
                // Calculating the value of ((n % i) % j) % n
                int val = ((n % i) % j) % n;
  
                // If value is equal to maximum
                if (val == max)
                    count++;
            }
        }
  
        // Return the number of possible pairs
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(countPairs(n));
    }
}


Python3
# Python3 implementation of the approach
  
# Function to return the count of 
# required pairs
def countPairs(n):
  
    # Number which will give the Max 
    # value for ((n % i) % j) % n
    num = ((n // 2) + 1)
      
    # To store the Maximum possible value 
    # of ((n % i) % j) % n
    Max = n % num
  
    # To store the count of possible pairs
    count = 0
  
    # Check all possible pairs
    for i in range(1, n + 1):
      
        for j in range(1, n + 1):
  
            # Calculating the value of
            # ((n % i) % j) % n
            val = ((n % i) % j) % n
  
            # If value is equal to Maximum
            if (val == Max):
                count += 1
          
    # Return the number of possible pairs
    return count
  
# Driver code
n = 5
print(countPairs(n))
  
# This code is contributed
# by Mohit Kumar


C#
// C# implementation of the above approach 
using System;
  
class GFG
{ 
  
// Function to return the count of required pairs 
static int countPairs(int n) 
{ 
    // Number which will give the max 
    // value for ((n % i) % j) % n 
    int num = ((n / 2) + 1) ;
  
    // To store the maximum possible value 
    // of ((n % i) % j) % n 
    int max = n % num; 
  
    // To store the count of possible pairs 
    int count = 0; 
  
    // Check all possible pairs 
    for (int i = 1; i <= n; i++) 
    { 
        for (int j = 1; j <= n; j++) 
        { 
  
            // Calculating the value of 
            // ((n % i) % j) % n 
            int val = ((n % i) % j) % n; 
  
            // If value is equal to maximum 
            if (val == max) 
                count++; 
        } 
    } 
  
    // Return the number of possible pairs 
    return count; 
} 
  
// Driver code 
public static void Main() 
{ 
    int n = 5; 
    Console.WriteLine(countPairs(n)); 
} 
}
  
// This code is contributed by Ryuga


PHP


C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the count of
// required pairs
int countPairs(int n)
{
  
    // Special case
    if (n == 2)
        return 4;
  
    // Number which will give the max value
    // for ((n % i) % j) % n
    int num = ((n / 2) + 1);
  
    // To store the maximum possible value 
    // of ((n % i) % j) % n
    int max = n % num;
  
    // Count of possible pairs
    int count = n - max;
  
    return count;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << countPairs(n);
}
  
// This code is contributed by Code_Mech.


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the count of required pairs
    public static int countPairs(int n)
    {
  
        // Special case
        if (n == 2)
            return 4;
  
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // Count of possible pairs
        int count = n - max;
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(countPairs(n));
    }
}


Python3
# Python3 implementation of the approach
  
# Function to return the count of required pairs
def countPairs(n):
      
    # Special case
    if (n == 2):
        return 4
  
    # Number which will give the max value
    # for ((n % i) % j) % n
    num = ((n // 2) + 1);
  
    # To store the maximum possible value 
    # of ((n % i) % j) % n
    max = n % num;
  
    # Count of possible pairs
    count = n - max;
  
    return count
  
# Driver code
if __name__ =="__main__" :
  
    n = 5;
print(countPairs(n));
  
# This code is contributed by Code_Mech


C#
// C# implementation of above approach
using System;
  
class GFG
{
      
    // Function to return the count of required pairs
    static int countPairs(int n)
    {
  
        // Special case
        if (n == 2)
            return 4;
  
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // Count of possible pairs
        int count = n - max;
  
        return count;
    }
  
    // Driver code
    static public void Main ()
    {
            int n = 5;
        Console.WriteLine(countPairs(n));
    }
}
  
// This code is contributed by Tushil..


PHP


输出:
3

时间复杂度: O(n 2 )

高效方法:获取余数的最大值,即max = n%num ,其中num =((n / 2)+1) 。现在,必须选择num以获得最大值,并且j可以选择为[max,n]范围内的任何值因为我们不需要减少计算出的最大值,并且选择j> max不会影响先前获得的值。因此,总对数为n – max
这种方法不适用于n = 2 。这是因为,对于n = 2 ,最大余数将为0,n – max将给出2,但我们知道答案是4 。在这种情况下,所有可能的对都是(1,1)(1,2)(2,1)(2,2)

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the count of
// required pairs
int countPairs(int n)
{
  
    // Special case
    if (n == 2)
        return 4;
  
    // Number which will give the max value
    // for ((n % i) % j) % n
    int num = ((n / 2) + 1);
  
    // To store the maximum possible value 
    // of ((n % i) % j) % n
    int max = n % num;
  
    // Count of possible pairs
    int count = n - max;
  
    return count;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << countPairs(n);
}
  
// This code is contributed by Code_Mech.

Java

// Java implementation of the approach
class GFG {
  
    // Function to return the count of required pairs
    public static int countPairs(int n)
    {
  
        // Special case
        if (n == 2)
            return 4;
  
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // Count of possible pairs
        int count = n - max;
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(countPairs(n));
    }
}

Python3

# Python3 implementation of the approach
  
# Function to return the count of required pairs
def countPairs(n):
      
    # Special case
    if (n == 2):
        return 4
  
    # Number which will give the max value
    # for ((n % i) % j) % n
    num = ((n // 2) + 1);
  
    # To store the maximum possible value 
    # of ((n % i) % j) % n
    max = n % num;
  
    # Count of possible pairs
    count = n - max;
  
    return count
  
# Driver code
if __name__ =="__main__" :
  
    n = 5;
print(countPairs(n));
  
# This code is contributed by Code_Mech

C#

// C# implementation of above approach
using System;
  
class GFG
{
      
    // Function to return the count of required pairs
    static int countPairs(int n)
    {
  
        // Special case
        if (n == 2)
            return 4;
  
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // Count of possible pairs
        int count = n - max;
  
        return count;
    }
  
    // Driver code
    static public void Main ()
    {
            int n = 5;
        Console.WriteLine(countPairs(n));
    }
}
  
// This code is contributed by Tushil.. 

的PHP


输出:
3

时间复杂度: O(1)