给定两个整数X和Y ,任务是找到整数K ,使得gcd(X,Y) 等于 gcd(X + K,Y) ,其中0 < K
例子:
Input: X = 3, Y = 15
Output: 4
Explanation: All possible values of K are {0, 3, 6, 9} for which GCD(X, Y) = GCD(X + K, Y).
Input: X = 2, Y = 12
Output: 2
Explanation: All possible values of K are {0, 8}.
天真的方法:解决问题的最简单方法是在[0,Y – 1]范围内进行迭代,并针对i的每个值,检查GCD(X + i,Y)是否等于GCD(X,Y)或不是。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate
// GCD of two integers
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to count possible
// values of K
int calculateK(int x, int y)
{
int count = 0;
int gcd_xy = gcd(x, y);
for (int i = 0; i < y; i++) {
// If required condition
// is satisfied
if (gcd(x + i, y) == gcd_xy)
// Increase count
count++;
}
return count;
}
// Driver Code
int main()
{
// Given X and y
int x = 3, y = 15;
cout << calculateK(x, y) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to calculate
// GCD of two integers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to count possible
// values of K
static int calculateK(int x, int y)
{
int count = 0;
int gcd_xy = gcd(x, y);
for (int i = 0; i < y; i++)
{
// If required condition
// is satisfied
if (gcd(x + i, y) == gcd_xy)
// Increase count
count++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
// Given X and y
int x = 3, y = 15;
System.out.print(calculateK(x, y));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to calculate
# GCD of two integers
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a % b)
# Function to count possible
# values of K
def calculateK(x, y):
count = 0
gcd_xy = gcd(x, y)
for i in range(y):
# If required condition
# is satisfied
if (gcd(x + i, y) == gcd_xy):
# Increase count
count += 1
return count
# Driver Code
if __name__ == '__main__':
# Given X and y
x = 3
y = 15
print (calculateK(x, y))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate
// GCD of two integers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to count possible
// values of K
static int calculateK(int x, int y)
{
int count = 0;
int gcd_xy = gcd(x, y);
for(int i = 0; i < y; i++)
{
// If required condition
// is satisfied
if (gcd(x + i, y) == gcd_xy)
// Increase count
count++;
}
return count;
}
// Driver code
public static void Main(String[] args)
{
// Given X and y
int x = 3, y = 15;
Console.Write(calculateK(x, y));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the gcd of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the number of Ks
int calculateK(int x, int y)
{
// Find gcd
int g = gcd(x, y);
int n = y / g;
int res = n;
// Calculating value of totient
// function for n
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res -= (res / i);
while (n % i == 0)
n /= i;
}
}
if (n != 1)
res -= (res / n);
return res;
}
// Driver Code
int main()
{
// Given X and Y
int x = 3, y = 15;
cout << calculateK(x, y) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the number of Ks
static int calculateK(int x, int y)
{
// Find gcd
int g = gcd(x, y);
int n = y / g;
int res = n;
// Calculating value of totient
// function for n
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
res -= (res / i);
while (n % i == 0)
n /= i;
}
}
if (n != 1)
res -= (res / n);
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given X and Y
int x = 3, y = 15;
System.out.print(calculateK(x, y) +"\n");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
# Function to find the gcd of a and b
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a % b)
# Function to find the number of Ks
def calculateK(x, y):
# Find gcd
g = gcd(x, y)
n = y // g
res = n
# Calculating value of totient
# function for n
i = 2
while i * i <= n:
if (n % i == 0):
res -= (res // i)
while (n % i == 0):
n //= i
i += 1
if (n != 1):
res -= (res // n)
return res
# Driver Code
if __name__ == "__main__":
# Given X and Y
x = 3
y = 15
print(calculateK(x, y))
# This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the number of Ks
static int calculateK(int x, int y)
{
// Find gcd
int g = gcd(x, y);
int n = y / g;
int res = n;
// Calculating value of totient
// function for n
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
res -= (res / i);
while (n % i == 0)
n /= i;
}
}
if (n != 1)
res -= (res / n);
return res;
}
// Driver Code
public static void Main(String[] args)
{
// Given X and Y
int x = 3, y = 15;
Console.Write(calculateK(x, y) + "\n");
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
4
时间复杂度: O(YlogY)
辅助空间: O(1)
高效的方法:这个想法是使用Euler的totient函数的概念。请按照以下步骤解决问题:
- 计算X和Y的gcd并将其存储在变量g中。
- 用Y / g初始化变量n 。
- 现在,找到n的totient函数,这将是必需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the gcd of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the number of Ks
int calculateK(int x, int y)
{
// Find gcd
int g = gcd(x, y);
int n = y / g;
int res = n;
// Calculating value of totient
// function for n
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res -= (res / i);
while (n % i == 0)
n /= i;
}
}
if (n != 1)
res -= (res / n);
return res;
}
// Driver Code
int main()
{
// Given X and Y
int x = 3, y = 15;
cout << calculateK(x, y) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the number of Ks
static int calculateK(int x, int y)
{
// Find gcd
int g = gcd(x, y);
int n = y / g;
int res = n;
// Calculating value of totient
// function for n
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
res -= (res / i);
while (n % i == 0)
n /= i;
}
}
if (n != 1)
res -= (res / n);
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given X and Y
int x = 3, y = 15;
System.out.print(calculateK(x, y) +"\n");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
# Function to find the gcd of a and b
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a % b)
# Function to find the number of Ks
def calculateK(x, y):
# Find gcd
g = gcd(x, y)
n = y // g
res = n
# Calculating value of totient
# function for n
i = 2
while i * i <= n:
if (n % i == 0):
res -= (res // i)
while (n % i == 0):
n //= i
i += 1
if (n != 1):
res -= (res // n)
return res
# Driver Code
if __name__ == "__main__":
# Given X and Y
x = 3
y = 15
print(calculateK(x, y))
# This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the number of Ks
static int calculateK(int x, int y)
{
// Find gcd
int g = gcd(x, y);
int n = y / g;
int res = n;
// Calculating value of totient
// function for n
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
res -= (res / i);
while (n % i == 0)
n /= i;
}
}
if (n != 1)
res -= (res / n);
return res;
}
// Driver Code
public static void Main(String[] args)
{
// Given X and Y
int x = 3, y = 15;
Console.Write(calculateK(x, y) + "\n");
}
}
// This code is contributed by gauravrajput1
Java脚本
输出:
4
时间复杂度: O(log(min(X,Y))+√N),其中N为Y / gcd(X,Y)。
辅助空间: O(1)