Count of pairs upto N such whose LCM is not equal to their product for Q queries
Last Updated :
17 Nov, 2021
Given a number N, the task is to find the number of pairs (a, b) in the range [1, N] such that their LCM is not equal to their product, i.e. LCM(a, b) != (a*b) and (b > a). There can be multiple queries to answer.
Examples:
Input: Q[] = {5}
Output: 1
Explanation:
The pair from 1 to 5 is (2, 4)
Input: Q[] = {5, 7}
Output: 1, 4
Explanation:
The pair from 1 to 5 is (2, 4)
The pairs from 1 to 7 are (2, 4), (2, 6), (3, 6), (4, 6)
Approach: The idea is to use Euler’s Totient Function.
1. Find the total number of pairs that can be formed using numbers from 1 to N. The number of pairs formed is equal to (N * (N – 1)) / 2.
2. For each integer i ? N, using Euler’s Totient Function find all such pairs which are co-prime with i and store them in the array.
Example:
arr[10] = 10 * (1-1/2) * (1-1/5)
= 4
3.
4. Now build the prefix sum table which stores the sum of all phi(i) for all i between 1 to N. Using this, we can answer each query in O(1) time.
5. Finally, the answer for any i ? N is given by the difference between the total number of pairs formed and pref[i].
Below is the implementation of the given approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int phi[N];
int pref[N];
void precompute()
{
phi[1] = 0;
for ( int i = 2; i < N; i++)
phi[i] = i;
for ( int p = 2; p < N; p++) {
if (phi[p] == p) {
phi[p] = p - 1;
for ( int i = 2 * p; i < N; i += p) {
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
void prefix()
{
for ( int i = 1; i < N; i++)
pref[i] = pref[i - 1] + phi[i];
}
void find_pairs( int n)
{
int total = (n * (n - 1)) / 2;
int ans = total - pref[n];
cout << "Number of pairs from 1 to "
<< n << " are " << ans << endl;
}
int main()
{
precompute();
prefix();
int q[] = { 5, 7 };
int n = sizeof (q) / sizeof (q[0]);
for ( int i = 0; i < n; i++) {
find_pairs(q[i]);
}
return 0;
}
|
Java
class GFG{
static final int N = 100005 ;
static int []phi = new int [N];
static int []pref = new int [N];
static void precompute()
{
phi[ 1 ] = 0 ;
for ( int i = 2 ; i < N; i++)
phi[i] = i;
for ( int p = 2 ; p < N; p++) {
if (phi[p] == p) {
phi[p] = p - 1 ;
for ( int i = 2 * p; i < N; i += p) {
phi[i] = (phi[i] / p) * (p - 1 );
}
}
}
}
static void prefix()
{
for ( int i = 1 ; i < N; i++)
pref[i] = pref[i - 1 ] + phi[i];
}
static void find_pairs( int n)
{
int total = (n * (n - 1 )) / 2 ;
int ans = total - pref[n];
System.out.print( "Number of pairs from 1 to "
+ n + " are " + ans + "\n" );
}
public static void main(String[] args)
{
precompute();
prefix();
int q[] = { 5 , 7 };
int n = q.length;
for ( int i = 0 ; i < n; i++) {
find_pairs(q[i]);
}
}
}
|
Python3
N = 100005
phi = [ 0 for i in range (N)]
pref = [ 0 for i in range (N)]
def precompute():
phi[ 1 ] = 0
for i in range ( 2 , N, 1 ):
phi[i] = i
for p in range ( 2 ,N):
if (phi[p] = = p):
phi[p] = p - 1
for i in range ( 2 * p, N, p):
phi[i] = (phi[i] / / p) * (p - 1 )
def prefix():
for i in range ( 1 , N, 1 ):
pref[i] = pref[i - 1 ] + phi[i]
def find_pairs(n):
total = (n * (n - 1 )) / / 2
ans = total - pref[n]
print ( "Number of pairs from 1 to" ,n, "are" ,ans)
if __name__ = = '__main__' :
precompute()
prefix()
q = [ 5 , 7 ]
n = len (q)
for i in range (n):
find_pairs(q[i])
|
C#
using System;
class GFG{
static readonly int N = 100005;
static int []phi = new int [N];
static int []pref = new int [N];
static void precompute()
{
phi[1] = 0;
for ( int i = 2; i < N; i++)
phi[i] = i;
for ( int p = 2; p < N; p++)
{
if (phi[p] == p)
{
phi[p] = p - 1;
for ( int i = 2 * p; i < N; i += p)
{
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
static void prefix()
{
for ( int i = 1; i < N; i++)
pref[i] = pref[i - 1] + phi[i];
}
static void find_pairs( int n)
{
int total = (n * (n - 1)) / 2;
int ans = total - pref[n];
Console.Write( "Number of pairs from 1 to " +
n + " are " + ans + "\n" );
}
public static void Main(String[] args)
{
precompute();
prefix();
int []q = {5, 7};
int n = q.Length;
for ( int i = 0; i < n; i++)
{
find_pairs(q[i]);
}
}
}
|
Javascript
<script>
var N = 100005;
var phi = Array(N).fill(0);
var pref = Array(N).fill(0);
function precompute() {
phi[1] = 0;
for (i = 2; i < N; i++)
phi[i] = i;
for (p = 2; p < N; p++) {
if (phi[p] == p) {
phi[p] = p - 1;
for (i = 2 * p; i < N; i += p) {
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
function prefix() {
for (i = 1; i < N; i++)
pref[i] = pref[i - 1] + phi[i];
}
function find_pairs(n) {
var total = (n * (n - 1)) / 2;
var ans = total - pref[n];
document.write( "Number of pairs from 1 to " + n + " are " + ans + "<br/>" );
}
precompute();
prefix();
var q = [ 5, 7 ];
var n = q.length;
for (i = 0; i < n; i++) {
find_pairs(q[i]);
}
</script>
|
Output:
Number of pairs from 1 to 5 are 1
Number of pairs from 1 to 7 are 4
Time Complexity: O(n2)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...