整数对 (x , y) 的计数,使得 x 和 y 的平方之间的差是完美平方
给定一个整数 N。任务是找到小于 N 和大于 1 的整数对 (x, y) 的数量,使得x 2 - y是平方数或 0。
例子:
Input: N = 3
Output: 2
Explanation:
The only possible valid pairs are (1, 1), (2, 3). Therefore, the count of such pairs is 2.
Input: N = 2
Output: 1
朴素方法:解决给定问题的最简单方法是在[1, N]范围内生成所有可能的整数对(x, y) ,然后检查(x 2 – y)的值是否为完美平方或不。如果发现是true ,则计算这一对。检查完所有可能后,打印获得的总数。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:上述方法也可以通过以下观察进行优化:
x2-y is a square of a number, let’s say square of z.
x2 – y = z2
x2 – z2 = y
( x + z ) * ( x – z ) = y
Now, let x + z = p and x – z = q
p * q = y
因此,问题被简化为计算p, q而不是x, y的对。
现在,因为 y 只能在1到N的范围内
因此, p*q也将在 1 到 N 的范围内。由于p>=q (因为x+z >= xz ), q将在1 到 √N的范围内,p 将在范围内为1 到 N/q 。
Also p+q = 2*x, so x = (p+q)/2.
Now, as x can have a max value of N, therefore
(p+q)/2 <= N
p <= 2*N-q
因此, p = min ( 2*N – q, N/q) 的最大值。
现在,在知道p & q 的范围之后,尝试 p & q的所有可能值。并且在固定之后,所有可能的对都是(l, q)其中l在从q到p 的最大值的范围内。
因此,形成的对总数为(p – q + 1) ,例如cnt 。
现在,我们知道p=x+z和q=xz,所以p和q要么是偶数要么是奇数。并且基于这个结论,如果q是偶数,则有效对的总数为cnt/2(在删除所有p 为偶数的对之后) ,如果它是奇数,则有效对的总数为(cnt/2 + 1) .
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find number of pairs
// (x, y) such that x^2 - y is a
// square number
int countPairs(int N)
{
// Stores the count of total pairs
int res = 0;
// Iterate q value 1 to sqrt(N)
for (int q = 1; q * q <= N; q++) {
// Maximum possible value of p is
// min(2 * N - q, N / q)
int maxP = min(2 * N - q, N / q);
// P must be greater than or
// equal to q
if (maxP < q)
continue;
// Total number of pairs are
int cnt = maxP - q + 1;
// Adding all valid pairs to res
res += (cnt / 2 + (cnt & 1));
}
// Return total no of pairs (x, y)
return res;
}
// Driver Code
int main()
{
int N = 3;
cout << countPairs(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find number of pairs
// (x, y) such that x^2 - y is a
// square number
static int countPairs(int N)
{
// Stores the count of total pairs
int res = 0;
// Iterate q value 1 to Math.sqrt(N)
for (int q = 1; q * q <= N; q++) {
// Maximum possible value of p is
// Math.min(2 * N - q, N / q)
int maxP = Math.min(2 * N - q, N / q);
// P must be greater than or
// equal to q
if (maxP < q)
continue;
// Total number of pairs are
int cnt = maxP - q + 1;
// Adding all valid pairs to res
res += (cnt / 2 + (cnt & 1));
}
// Return total no of pairs (x, y)
return res;
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
System.out.print(countPairs(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# python program for the above approach
import math
# Function to find number of pairs
# (x, y) such that x^2 - y is a
# square number
def countPairs(N):
# Stores the count of total pairs
res = 0
# Iterate q value 1 to sqrt(N)
for q in range(1, int(math.sqrt(N)) + 1):
# Maximum possible value of p is
# min(2 * N - q, N / q)
maxP = min(2 * N - q, N // q)
# P must be greater than or
# equal to q
if (maxP < q):
continue
# Total number of pairs are
cnt = maxP - q + 1
# Adding all valid pairs to res
res += (cnt // 2 + (cnt & 1))
# Return total no of pairs (x, y)
return res
# Driver Code
if __name__ == "__main__":
N = 3
print(countPairs(N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find number of pairs
// (x, y) such that x^2 - y is a
// square number
static int countPairs(int N)
{
// Stores the count of total pairs
int res = 0;
// Iterate q value 1 to sqrt(N)
for (int q = 1; q * q <= N; q++) {
// Maximum possible value of p is
// min(2 * N - q, N / q)
int maxP = Math.Min(2 * N - q, N / q);
// P must be greater than or
// equal to q
if (maxP < q)
continue;
// Total number of pairs are
int cnt = maxP - q + 1;
// Adding all valid pairs to res
res += (cnt / 2 + (cnt & 1));
}
// Return total no of pairs (x, y)
return res;
}
// Driver Code
public static void Main()
{
int N = 3;
Console.WriteLine(countPairs(N));
}
}
// This code is contributed by ukasp.
Javascript
2
时间复杂度: O(N 1/2 )
辅助空间: O(1)