-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNP-Problem.py
More file actions
72 lines (58 loc) · 1.3 KB
/
NP-Problem.py
File metadata and controls
72 lines (58 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Question: NP Problem
An array A of N integers is said to be a good if these conditions are met.
1. Elements of array are non-decreasing. i.e A1 < A2 < A3 <... < An.
2. Product of all elements is less than or equal to P.
Find how many distinct good array A, exist for a given value of N and P.
Constrains
1 <= T <= 5
1 <= N <= 3
1 <= P <= 2 * 10^10
All input are integers.
Input:
T : Number of test cases.
N : length of array
P : for good array calculation.
Input Format:
First line contain T
Second line contain N and P.
Output Format:
Each separate line for each testcase results.
Sample input:
1
2 4
Sample output:
5
Explanation:
All possible good array are [1,1], [1,2], [1,3], [1,4], and [2,2].
"""
# Solution with explanation.
def solve(target):
# this function solves for n ==2
sum = 0
i = 0
while i < P // i:
sum += P//i - i + 1
i += 1
return ans
T = int(input())
for i in range(T):
N, P = input().split(" ")
N, P = int(N), int(P)
ans = 0
if N == 1:
ans = P
elif N == 2:
ans = solve(P)
elif N == 3:
sum = 0
i = 1
while True:
temp = solve(P//i)
if temp > 0:
sum += temp
i+=1
else:
break
ans = sum
print(ans)