+ 3

Help me to fix the problem??

n = int(input()) m = int(input()) for i in range(m): for j in range(i+1,m): if 1<=i<=n and 1<=j<=m and (i+j)%5==0: print(i,j) Task: You are given two integers ‘N’ and ‘M’. A pair (x, y) is a divisible pair if it satisfies the following conditions: a) 1 <= x <= ‘N’ b) 1 <= y <= ‘M’ c) x + y is divisible by 5. Your task is to return the count of all divisible pairs that can be formed from given ‘N’ and ‘M’. If N = 3 and M = 5, then { x = 1, y = 4 }, { x = 2, y = 3 }, { x = 3, y = 2 } are the pairs that satisfy the given conditions.

12th Aug 2021, 6:18 AM
Shahir
Shahir - avatar
3 Antworten
+ 4
This seems to be working as expected with given sample <N> = 3 and <M> = 5 n = int(input()) m = int(input()) div_pairs = 0 for x in range( 1, n + 1 ): for y in range( 1, m + 1 ): if ( x + y ) % 5 == 0: #print( ( x, y ) ) div_pairs += 1 print( div_pairs ) #Your task is to return the count of all divisible pairs that can be formed from given ‘N’ and ‘M’.
12th Aug 2021, 7:31 AM
Ipang
+ 3
Thanks it works
12th Aug 2021, 8:43 AM
Shahir
Shahir - avatar
+ 2
I spent some time experimenting with this task, heres the resulting formula in O(1), no loops needed def count_pairs(n,m): n_mod = n%5 m_mod = m%5 return n*(m//5) + (n//5)*m_mod + max(0,n_mod+m_mod-4)
12th Aug 2021, 10:03 AM
Giorgos