Given two numbers N and K. A sequence A1, A2, ….AN of length N can be created by placing numbers from 1 to K at each position making a total of KN sequences. The task is to find the sum of GCD of all the sequences formed.
Note: The answer can be very large, so take modulo with 109 + 7.
Input: N = 3, K = 2
The gcd of all the subsequences are:
gcd(1, 1, 1) = 1
gcd(1, 1, 2) = 1
gcd(1, 2, 1) = 1
gcd(1, 2, 2) = 1
gcd(2, 1, 1) = 1
gcd(2, 1, 2) = 1
gcd(2, 2, 1) = 1
gcd(2, 2, 2) = 2
Sum of GCD is 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 = 9.
Input: N = 3, K = 200
Naive Approach: The idea is to generate all the possible subsequence of length N recursively. The summation of GCD of all the sequences formed is the required result.
Below is the implementation of the above approah:
Time Complexity: O(NK)
- Since the numbers of the sequence can be from 1 to K, the gcd value of the sequence will be in the range 1 to K.
- Let count[i] represent the number of sequences with gcd = i. For i = 1, we have no constraints on which elements can belong to the sequence, so at each of the N places we have K possibilities to place elements making the total sequences to be KN. But the resulting sequences may have higher GCD, So subtract the over counted values:
count = KN - count - count - count - .... count[K]
- Similarly for i = 2, since every number must be a multiple of 2, we have K/2 possibilities at each place making the total to be (K/2)N. And Subtract all the over counted values by subtracting sequence count with GCD of all multiples of 2.
count = (K/2)N - count - count - count - ... all multiples of 2
- Similarly follow the above steps for each gcd value till K.
- The summation of each GCD value(say g) with count[g] is the sum of GCD of all the sequence formed.
Below is the implementation of the above approach:
Time Complexity: O( K*log(N) + K*log(log(K)) )
- Count number of sub-sequences with GCD 1
- Print all sequences of given length
- Number of N length sequences whose product is M
- Count of sub-sequences which satisfy the given condition
- Count total number of even sum sequences
- Sum of minimum element of all sub-sequences of a sorted array
- Count the Arithmetic sequences in the Array of size at least 3
- Count number of increasing sub-sequences : O(NlogN)
- Print all non-increasing sequences of sum equal to a given number x
- Count all sub-sequences having product <= K - Recursive approach
- Sequences of given length where every element is more than or equal to twice of previous
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Print all longest common sub-sequences in lexicographical order
- Find all even length binary sequences with same sum of first and second half bits
- Count even length binary sequences with same sum of first and second half bits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.