📌  相关文章
📜  计算满足不等式x * x + y * y <n的不同非负整数对(x,y)

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

给定一个正数n,对满足不等式x * x + y * y 例子:

Input:  n = 5
Output: 6
The pairs are (0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (0, 2)

Input: n = 6
Output: 8
The pairs are (0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (0, 2),
              (1, 2), (2, 1)

一个简单的解决方案是运行两个循环。外循环适用于x的所有可能值(从0到√n)。内部循环从x的当前值中选取y的所有可能值(由外部循环选择)。以下是简单解决方案的实现。

C++
#include 
using namespace std;
 
// This function counts number of pairs (x, y) that satisfy
// the inequality x*x + y*y < n.
int countSolutions(int n)
{
   int res = 0;
   for (int x = 0; x*x < n; x++)
      for (int y = 0; x*x + y*y < n; y++)
         res++;
   return res;
}
 
// Driver program to test above function
int main()
{
    cout << "Total Number of distinct Non-Negative pairs is "
         << countSolutions(6) << endl;
    return 0;
}


Java
// Java code to Count Distinct
// Non-Negative Integer Pairs
// (x, y) that Satisfy the
// inequality x*x + y*y < n
import java.io.*;
 
class GFG
{
    // This function counts number
    // of pairs (x, y) that satisfy
    // the inequality x*x + y*y < n.
    static int countSolutions(int n)
    {
        int res = 0;
        for (int x = 0; x * x < n; x++)
            for (int y = 0; x * x + y * y < n; y++)
                res++;
                 
        return res;
    }
 
    // Driver program
    public static void main(String args[])
    {
        System.out.println ( "Total Number of distinct Non-Negative pairs is "
                                                        +countSolutions(6));
         
    }
}
 
// This article is contributed by vt_m.


Python3
# Python3 implementation of above approach
 
# This function counts number of pairs
# (x, y) that satisfy
# the inequality x*x + y*y < n.
def countSolutions(n):
 
    res = 0
    x = 0
    while(x * x < n):
        y = 0
        while(x * x + y * y < n):
            res = res + 1
            y = y + 1
        x = x + 1
 
    return res
 
# Driver program to test above function
if __name__=='__main__':
    print("Total Number of distinct Non-Negative pairs is ",
         countSolutions(6))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# code to Count Distinct
// Non-Negative Integer Pairs
// (x, y) that Satisfy the
// inequality x*x + y*y < n
using System;
 
class GFG {
     
    // This function counts number
    // of pairs (x, y) that satisfy
    // the inequality x*x + y*y < n.
    static int countSolutions(int n)
    {
        int res = 0;
         
        for (int x = 0; x*x < n; x++)
            for (int y = 0; x*x + y*y < n; y++)
                res++;
                 
        return res;
    }
 
    // Driver program
    public static void Main()
    {
        Console.WriteLine( "Total Number of "
          + "distinct Non-Negative pairs is "
                        + countSolutions(6));
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
// An efficient C program to find different (x, y) pairs that
// satisfy x*x + y*y < n.
#include 
using namespace std;
 
// This function counts number of pairs (x, y) that satisfy
// the inequality x*x + y*y < n.
int countSolutions(int n)
{
   int x = 0, yCount, res = 0;
 
   // Find the count of different y values for x = 0.
   for (yCount = 0; yCount*yCount < n; yCount++) ;
 
   // One by one increase value of x, and find yCount for
   // current x.  If yCount becomes 0, then we have reached
   // maximum possible value of x.
   while (yCount != 0)
   {
       // Add yCount (count of different possible values of y
       // for current x) to result
       res  +=  yCount;
 
       // Increment x
       x++;
 
       // Update yCount for current x. Keep reducing yCount while
       // the inequality is not satisfied.
       while (yCount != 0 && (x*x + (yCount-1)*(yCount-1) >= n))
         yCount--;
   }
 
   return res;
}
 
// Driver program to test above function
int main()
{
    cout << "Total Number of distinct Non-Negative pairs is "
         << countSolutions(6) << endl;
    return 0;
}


Java
// An efficient Java program to
// find different (x, y) pairs
// that satisfy x*x + y*y < n.
import java.io.*;
 
class GFG
{
    // This function counts number
    //of pairs (x, y) that satisfy
    // the inequality x*x + y*y < n.
    static int countSolutions(int n)
    {
        int x = 0, yCount, res = 0;
         
        // Find the count of different
        // y values for x = 0.
        for (yCount = 0; yCount * yCount < n; yCount++) ;
         
        // One by one increase value of x,
        // and find yCount forcurrent x. If
        // yCount becomes 0, then we have reached
        // maximum possible value of x.
        while (yCount != 0)
        {
            // Add yCount (count of different possible
            // values of y for current x) to result
            res += yCount;
             
            // Increment x
            x++;
             
            // Update yCount for current x. Keep reducing
            // yCount while the inequality is not satisfied.
            while (yCount != 0 && (x * x + (yCount - 1)
                                  * (yCount - 1) >= n))
            yCount--;
             
        }
        return res;
         
    }
     
    // Driver program
    public static void main(String args[])
    {
        System.out.println ( "Total Number of distinct Non-Negative pairs is "
                             +countSolutions(6)) ;
         
    }
}
 
// This article is contributed by vt_m.


Python3
# An efficient python program to
# find different (x, y) pairs
# that satisfy x*x + y*y < n.
 
# This function counts number of
# pairs (x, y) that satisfy the
# inequality x*x + y*y < n.
def countSolutions(n):
     
    x = 0
    res = 0
    yCount = 0
 
    # Find the count of different
    # y values for x = 0.
    while(yCount * yCount < n):
        yCount = yCount + 1
         
    # One by one increase value of
    # x, and find yCount for current
    # x. If yCount becomes 0, then
    # we have reached maximum
    # possible value of x.
    while (yCount != 0):
        # Add yCount (count of
        # different possible values
        # of y for current x) to
        # result
        res = res + yCount
 
        # Increment x
        x = x + 1
 
        # Update yCount for current
        # x. Keep reducing yCount
        # while the inequality is
        # not satisfied.
        while (yCount != 0 and (x * x
                     + (yCount - 1) *
                     (yCount - 1) >= n)):
            yCount = yCount - 1
         
    return res
 
# Driver program to test
# above function
print ("Total Number of distinct ",
           "Non-Negative pairs is "
               , countSolutions(6))
 
# This code is contributed by Sam007.


C#
// An efficient C# program to
// find different (x, y) pairs
// that satisfy x*x + y*y < n.
using System;
 
class GFG {
     
    // This function counts number
    //of pairs (x, y) that satisfy
    // the inequality x*x + y*y < n.
    static int countSolutions(int n)
    {
        int x = 0, yCount, res = 0;
         
        // Find the count of different
        // y values for x = 0.
        for (yCount = 0; yCount * yCount < n;
                                   yCount++) ;
         
        // One by one increase value of x,
        // and find yCount forcurrent x. If
        // yCount becomes 0, then we have
        // reached maximum possible value
        // of x.
        while (yCount != 0)
        {
             
            // Add yCount (count of different
            // possible values of y for
            // current x) to result
            res += yCount;
             
            // Increment x
            x++;
             
            // Update yCount for current x.
            // Keep reducing yCount while the
            // inequality is not satisfied.
            while (yCount != 0 && (x * x +
                            (yCount - 1) *
                         (yCount - 1) >= n))
            yCount--;
        }
         
        return res;
    }
     
    // Driver program
    public static void Main()
    {
        Console.WriteLine( "Total Number of "
          + "distinct Non-Negative pairs is "
                       + countSolutions(6)) ;
    }
}
 
// This code is contributed by Sam007.


PHP
= $n))
            $yCount--;
    }
     
    return $res;
}
 
// Driver program to test above function
echo "Total Number of distinct Non-Negative",
        "pairs is ", countSolutions(6) ,"\n";
 
// This code is contributed by anuj_67.
?>


输出:

Total Number of distinct Non-Negative pairs is 8

上述解决方案的时间复杂度的上限是O(n)。外循环运行√n次。内循环最多运行√n次。
使用有效的解决方案,我们可以找到O(√n)时间的计数。想法是首先找到与x的0值相对应的所有y值的计数。设不同的y值的计数为yCount。我们可以通过运行循环并将yCount * yCount与n进行比较来找到yCount。
有了初始yCount之后,我们可以逐一增加x的值,并通过减小yCount来找到yCount的下一个值。

C++

// An efficient C program to find different (x, y) pairs that
// satisfy x*x + y*y < n.
#include 
using namespace std;
 
// This function counts number of pairs (x, y) that satisfy
// the inequality x*x + y*y < n.
int countSolutions(int n)
{
   int x = 0, yCount, res = 0;
 
   // Find the count of different y values for x = 0.
   for (yCount = 0; yCount*yCount < n; yCount++) ;
 
   // One by one increase value of x, and find yCount for
   // current x.  If yCount becomes 0, then we have reached
   // maximum possible value of x.
   while (yCount != 0)
   {
       // Add yCount (count of different possible values of y
       // for current x) to result
       res  +=  yCount;
 
       // Increment x
       x++;
 
       // Update yCount for current x. Keep reducing yCount while
       // the inequality is not satisfied.
       while (yCount != 0 && (x*x + (yCount-1)*(yCount-1) >= n))
         yCount--;
   }
 
   return res;
}
 
// Driver program to test above function
int main()
{
    cout << "Total Number of distinct Non-Negative pairs is "
         << countSolutions(6) << endl;
    return 0;
}

Java

// An efficient Java program to
// find different (x, y) pairs
// that satisfy x*x + y*y < n.
import java.io.*;
 
class GFG
{
    // This function counts number
    //of pairs (x, y) that satisfy
    // the inequality x*x + y*y < n.
    static int countSolutions(int n)
    {
        int x = 0, yCount, res = 0;
         
        // Find the count of different
        // y values for x = 0.
        for (yCount = 0; yCount * yCount < n; yCount++) ;
         
        // One by one increase value of x,
        // and find yCount forcurrent x. If
        // yCount becomes 0, then we have reached
        // maximum possible value of x.
        while (yCount != 0)
        {
            // Add yCount (count of different possible
            // values of y for current x) to result
            res += yCount;
             
            // Increment x
            x++;
             
            // Update yCount for current x. Keep reducing
            // yCount while the inequality is not satisfied.
            while (yCount != 0 && (x * x + (yCount - 1)
                                  * (yCount - 1) >= n))
            yCount--;
             
        }
        return res;
         
    }
     
    // Driver program
    public static void main(String args[])
    {
        System.out.println ( "Total Number of distinct Non-Negative pairs is "
                             +countSolutions(6)) ;
         
    }
}
 
// This article is contributed by vt_m.

Python3

# An efficient python program to
# find different (x, y) pairs
# that satisfy x*x + y*y < n.
 
# This function counts number of
# pairs (x, y) that satisfy the
# inequality x*x + y*y < n.
def countSolutions(n):
     
    x = 0
    res = 0
    yCount = 0
 
    # Find the count of different
    # y values for x = 0.
    while(yCount * yCount < n):
        yCount = yCount + 1
         
    # One by one increase value of
    # x, and find yCount for current
    # x. If yCount becomes 0, then
    # we have reached maximum
    # possible value of x.
    while (yCount != 0):
        # Add yCount (count of
        # different possible values
        # of y for current x) to
        # result
        res = res + yCount
 
        # Increment x
        x = x + 1
 
        # Update yCount for current
        # x. Keep reducing yCount
        # while the inequality is
        # not satisfied.
        while (yCount != 0 and (x * x
                     + (yCount - 1) *
                     (yCount - 1) >= n)):
            yCount = yCount - 1
         
    return res
 
# Driver program to test
# above function
print ("Total Number of distinct ",
           "Non-Negative pairs is "
               , countSolutions(6))
 
# This code is contributed by Sam007.

C#

// An efficient C# program to
// find different (x, y) pairs
// that satisfy x*x + y*y < n.
using System;
 
class GFG {
     
    // This function counts number
    //of pairs (x, y) that satisfy
    // the inequality x*x + y*y < n.
    static int countSolutions(int n)
    {
        int x = 0, yCount, res = 0;
         
        // Find the count of different
        // y values for x = 0.
        for (yCount = 0; yCount * yCount < n;
                                   yCount++) ;
         
        // One by one increase value of x,
        // and find yCount forcurrent x. If
        // yCount becomes 0, then we have
        // reached maximum possible value
        // of x.
        while (yCount != 0)
        {
             
            // Add yCount (count of different
            // possible values of y for
            // current x) to result
            res += yCount;
             
            // Increment x
            x++;
             
            // Update yCount for current x.
            // Keep reducing yCount while the
            // inequality is not satisfied.
            while (yCount != 0 && (x * x +
                            (yCount - 1) *
                         (yCount - 1) >= n))
            yCount--;
        }
         
        return res;
    }
     
    // Driver program
    public static void Main()
    {
        Console.WriteLine( "Total Number of "
          + "distinct Non-Negative pairs is "
                       + countSolutions(6)) ;
    }
}
 
// This code is contributed by Sam007.

的PHP

= $n))
            $yCount--;
    }
     
    return $res;
}
 
// Driver program to test above function
echo "Total Number of distinct Non-Negative",
        "pairs is ", countSolutions(6) ,"\n";
 
// This code is contributed by anuj_67.
?>

输出:

Total Number of distinct Non-Negative pairs is 8