给定一个数组Arr和一个整数K。任务是找到大小为K的子序列数,以使序列的LCM和HCF相同。
例子:
Input: Arr = {1, 2, 2, 3, 3}, K = 2
Output: 2
Subsequences are – {2, 2} and {3, 3}
Input: Arr = {1, 1, 1, 1, 2, 2}, K = 3
Output: 4
方法:
当所有数字相同时,一组数字的LCM和HCF (GCD)相等。
可以通过以下方式证明。让我们以两个数字为例。
设两个数字为x和y
HCF(x,y) = LCM(x,y) = k(说)
由于HCF(x,y)= k ,
x = kn , y = km ,对于某些自然数m , n
我们知道, HCF×LCM =两个数字的乘积
因此, k 2 = km×kn
因此, mn = 1
因此, m = n = 1 ,因为m,n是自然数
因此,
x = kn = k ,而y = km = k
意味着x = y = k ,即数字必须相等。
这个概念可以扩展到一组数字。因此,证明了相同的数字具有相等的GCD和LCM。
之后,我们需要借助映射来找到阵列中每个元素的频率。然后,将使用组合理论公式来查找具有给定频率的特定元素的特定长度K的子序列数。该概念将应用于具有给定频率的所有元素,所有这些元素的总和将成为答案。
下面是上述方法的实现:
C++
// C++ implementation
#include
using namespace std;
// Returns factorial of n
long long fact(int n)
{
long long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Returns nCr for the
// given values of r and n
long long nCr(int n, int r)
{
return fact(n) / (1LL * fact(r)
* fact(n - r));
}
long long number_of_subsequences(int arr[],
int k,
int n)
{
long long s = 0;
// Map to store the frequencies
// of each elements
map m;
// Loop to store the
// frequencies of elements
// in the map
for (int i = 0; i < n; i++) {
m[arr[i]]++;
}
for (auto j : m) {
// Using nCR formula to
// calculate the number
// of subsequences of a
// given length
s = s + 1LL * nCr(j.second, k);
}
return s;
}
// Driver Code
int main()
{
int arr[] = { 1, 1, 1, 1, 2, 2, 2 };
int k = 2;
int n = sizeof(arr) / sizeof(arr[0]);
// Function calling
cout << number_of_subsequences(arr, k, n);
return 0;
}
Java
// Java implementation for above approach
import java.util.*;
class GFG
{
// Returns factorial of n
static long fact(int n)
{
long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Returns nCr for the
// given values of r and n
static long nCr(int n, int r)
{
return fact(n) / (1 * fact(r) *
fact(n - r));
}
static long number_of_subsequences(int arr[],
int k, int n)
{
long s = 0;
// Map to store the frequencies
// of each elements
HashMap mp = new HashMap();
// Loop to store the
// frequencies of elements
// in the map
for (int i = 0; i < n; i++)
{
if(mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
for (Map.Entry j : mp.entrySet())
{
// Using nCR formula to
// calculate the number
// of subsequences of a
// given length
s = s + 1 * nCr(j.getValue(), k);
}
return s;
}
// Driver Code
static public void main ( String []arg)
{
int arr[] = { 1, 1, 1, 1, 2, 2, 2 };
int k = 2;
int n = arr.length;
// Function calling
System.out.println(number_of_subsequences(arr, k, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of above approach
# Returns factorial of n
def fact(n):
res = 1
for i in range(2, n + 1):
res = res * i
return res
# Returns nCr for the
# given values of r and n
def nCr(n, r):
return fact(n) // (fact(r) * fact(n - r))
def number_of_subsequences(arr, k, n):
s = 0
# Map to store the frequencies
# of each elements
m = dict()
# Loop to store the
# frequencies of elements
# in the map
for i in arr:
m[i] = m.get(i, 0) + 1
for j in m:
# Using nCR formula to
# calculate the number
# of subsequences of a
# given length
s = s + nCr(m[j], k)
return s
# Driver Code
arr = [1, 1, 1, 1, 2, 2, 2]
k = 2
n = len(arr)
# Function calling
print(number_of_subsequences(arr, k, n))
# This code is contributed by Mohit Kumar
C#
// C# implementation for above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Returns factorial of n
static long fact(int n)
{
long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Returns nCr for the
// given values of r and n
static long nCr(int n, int r)
{
return fact(n) / (1 * fact(r) *
fact(n - r));
}
static long number_of_subsequences(int []arr,
int k, int n)
{
long s = 0;
// Map to store the frequencies
// of each elements
Dictionary mp = new Dictionary();
// Loop to store the
// frequencies of elements
// in the map
for (int i = 0 ; i < n; i++)
{
if(mp.ContainsKey(arr[i]))
{
var val = mp[arr[i]];
mp.Remove(arr[i]);
mp.Add(arr[i], val + 1);
}
else
{
mp.Add(arr[i], 1);
}
}
foreach(KeyValuePair j in mp)
{
// Using nCR formula to
// calculate the number
// of subsequences of a
// given length
s = s + 1 * nCr(j.Value, k);
}
return s;
}
// Driver Code
static public void Main ( String []arg)
{
int []arr = { 1, 1, 1, 1, 2, 2, 2 };
int k = 2;
int n = arr.Length;
// Function calling
Console.Write(number_of_subsequences(arr, k, n));
}
}
// This code is contributed by 29AjayKumar
输出:
9