Count of permutation such that GCD of all elements multiplied with position is not 1
Given an integer array having N elements ranging from 1 to N and each element appearing exactly once. The task is to find the number of possible permutations such that the GCD of all elements multiplied with their position is greater than 1.
Note: As the answer can be very large, return the answer modulo 109 + 7
Examples:
Input: N = 2, arr[] = {1, 2}
Output: 1
Explanation: The only valid permutation will be is [2, 1] because GCD(1*2, 2*1) = 2.
Input: N = 4, arr[] = {4, 1, 3, 2}
Output: 4
Explanation:
The valid permutations will be
[4, 3, 2, 1] with GCD(1*4, 2*3, 3*2, 4*1) = 2.
[2, 3, 4, 1] with GCD(1*2, 2*3, 3*4, 4*1) = 2.
[2, 1, 4, 3] with GCD(1*2, 2*1, 3*4, 4*3) = 2.
[4, 1, 2, 3] with GCD(1*4, 2*1, 3*2, 4*3) = 2.
Approach: The idea to solve the problem is as follows:
Try to make the product of position and the number even, then in that situation GCD will be at least 2.
So if N is odd then there will 1 more odd element than possible even positions. So no permutation is possible.
Otherwise
- the N/2 even elements can be arranged in (N/2)! ways.
- For each of this arrangement N/2 odd elements can be arranged in (N/2)! ways.
So total number of possible ways are ((N/2)!)2.
Follow the below steps to solve this problem:
- If N is odd then return 0.
- Initialize one variable to store the answer (say ans = 1).
- Traverse from i = 1 to N/2.
- Make ans equal to ans * i * i % MOD.
- Find the mod of ans.
- Return ans.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int ValidPerm( int n, int a[])
{
if (n & 1) {
return 0;
}
long long ans = 1;
for ( int i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
return ans;
}
int main()
{
int N = 4;
int arr[N] = { 1, 3, 2, 4 };
cout << ValidPerm(N, arr);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int MOD = 1000000007 ;
static int ValidPerm( int n, int a[])
{
if ((n & 1 ) == 1 ) {
return 0 ;
}
long ans = 1 ;
for ( int i = 1 ; i <= n / 2 ; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
return ( int )ans;
}
public static void main(String args[])
{
int N = 4 ;
int arr[] = { 1 , 3 , 2 , 4 };
System.out.println(ValidPerm(N, arr));
}
}
|
Python3
MOD = 1000000007
def ValidPerm(n, a):
if (n & 1 ):
return 0
ans = 1
for i in range ( 1 ,(n / / 2 ) + 1 ):
ans * = i * i % MOD
ans % = MOD
return ans
N = 4
arr = [ 1 , 3 , 2 , 4 ]
print (ValidPerm(N, arr));
|
C#
using System;
public class GFG {
static int MOD = 1000000007;
static int ValidPerm( int n, int []a)
{
if ((n & 1) == 1) {
return 0;
}
long ans = 1;
for ( int i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
return ( int )ans;
}
public static void Main()
{
int N = 4;
int []arr = { 1, 3, 2, 4 };
Console.WriteLine(ValidPerm(N, arr));
}
}
|
Javascript
<script>
const MOD = 1000000007;
const ValidPerm = (n, a) => {
if (n & 1) {
return 0;
}
let ans = 1;
for (let i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
return ans;
}
let N = 4;
let arr = [1, 3, 2, 4];
document.write(ValidPerm(N, arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
16 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...