-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.cpp
51 lines (45 loc) · 894 Bytes
/
5.cpp
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
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
// } Driver Code Ends
//User function template for C++
class Solution {
public:
// #define ull unsigned long long
/* Function to get the nth ugly number*/
#define ull unsigned long long
ull getNthUglyNo(int n) {
vector<ull> dp(n + 1);
dp[1] = 1;
int p2 = 1, p3 = 1, p5 = 1;
for (int i = 2; i <= n; i++) {
ull f1 = 2 * dp[p2];
ull f2 = 3 * dp[p3];
ull f3 = 5 * dp[p5];
ull min = (f1 < f2) ? ((f1 < f3) ? f1 : f3) : ((f2 < f3) ? f2 : f3);
dp[i] = min;
if (min == f1)
p2++;
if (min == f2)
p3++;
if (min == f3)
p5++;
}
return dp[n];
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
Solution ob;
auto ans = ob.getNthUglyNo(n);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends