+ 1

Maths behind itertools combinations with replacement

I need to find the total number of arrays using n and k inputs.. n=int(input()) k=int(input() N denotes the maximum number range of array elements K denotes the length of array I need to find all possible arrays using the inputs N and K a[1],a[2],a[3],... a[k] where every number a[i] is in range from 1 to N and moreover a[i+1] should be divisible by a[i] where 1< i <= K Sample case : n=2 k=2 output : [1,1], [1,2], [2,2] [2,1] is invalid because a[i+1] is not divisible by a[i] so total number of arrays are 3 ------------------------------------------------------------------------ without caring the condition [a+1] % a[i] = 0 I use itertools.combinations_with_replacement in my code and using formula I found out the total number of possible arrays. formula : k+(n-1)! ---------------- k! (n-1)! I used formula because when we use bug nunbers as input N and K then time complexity increases.. I need formula for condition also a[i+1] % a[i]

14th Jul 2021, 11:49 AM
Ratnapal Shende
Ratnapal Shende - avatar
12 odpowiedzi
+ 1
# try this: def combinations_count(N,K): last = range(1,N+1) for _ in range(K-1): last = ( v for a in last for v in range(a,N+1,a) ) return sum(1 for _ in last) n = int(input()) k = int(input()) if K<1 or N<1: print(0) elif l==1 or n==1: print(n) else: print(combinations_count(n,k))
14th Jul 2021, 3:03 PM
visph
visph - avatar
+ 1
visph Thank you so much! 🎉☺️🎊 please explain math behind it sir I am weak in maths..
14th Jul 2021, 6:10 PM
Ratnapal Shende
Ratnapal Shende - avatar
14th Jul 2021, 11:51 AM
Ratnapal Shende
Ratnapal Shende - avatar
0
Ratnapal Shende wrote: "[1,2] is invalid because a[i+1] is not divisible by a[i]"... but a[i+1] == 2, a[i] == 1 and 2 is divisible by 1... did you mean the converse? a[i] is not divisible by a[i+1]? and so condition must be a[i] % a[i+1] == 0?
14th Jul 2021, 12:05 PM
visph
visph - avatar
0
visph i mean [1,2] # valid here 2 should be divisible by 1 [2,4] #valid 4 should be divisible by 2 [9,3] # invalid because 3 is not divisible by 9 when k is 3 --> [2,4,8] # valid because 8 is divisible by 4 and 4 is divisible by 2 [3,5,10] # invalid 10 is divisible by 5 but 5 is not divisible by 3 so this array is invalid.. like this I need a formula which tells the number of all the possible arrays..
14th Jul 2021, 12:17 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
you should edit your sample case in question description as it state that [1,2] is invalid ^^ anyway,,not sure at all that there's a formula as you expect... if time duration is a problem, I guess you must find a smarter algorithm to compute the solution, but not necessarly by finding a formula ;P however, my math skills are not bad, but not enough to give you the expected formula if only it exists :(
14th Jul 2021, 12:23 PM
visph
visph - avatar
0
maybe you could avoid using itertools combinations_with_replacement function and instead generate yourself them (without storing and only counting) only for values that match your condition...
14th Jul 2021, 12:29 PM
visph
visph - avatar
0
visph case edited ! please give me any solution for that please.. Atleast, If i give input n=100 k=3 My system should say the possible numbers of arrays without getting stuck ... 😛 edit: OK I will try...
14th Jul 2021, 12:32 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
Ratnapal Shende for: [a0,a1,..,an] a0 < a1 < ... < an (a[i] < a[i+1]) and a[i+1] = x*a[i] (wich imply previous if x >= 1) with x as int number
14th Jul 2021, 12:35 PM
visph
visph - avatar
0
visph i am not getting what to do... my brain is not working 😢 I read code of combination in docs but I am unable to implement according to my need. please help me sir !
14th Jul 2021, 1:12 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
if a[i] == x then possible values of a[i+1] are: [x, x*2, x*3, ... x*n] for x<=N and x*n <= N (max number range of array elements)
14th Jul 2021, 1:20 PM
visph
visph - avatar
- 1
no much math behind... instead of generating all combinations based on rule previously explained, I generate (and keep), only last a[i] as generator... with last list (generator) I get length of elements by adding 1 for each element (iterate only once to get count) ;)
14th Jul 2021, 7:26 PM
visph
visph - avatar