-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path970.powerful-integers.cpp
76 lines (74 loc) · 1.47 KB
/
970.powerful-integers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* @lc app=leetcode id=970 lang=cpp
*
* [970] Powerful Integers
*
* https://leetcode.com/problems/powerful-integers/description/
*
* algorithms
* Easy (39.53%)
* Total Accepted: 13.9K
* Total Submissions: 35.3K
* Testcase Example: '2\n3\n10'
*
* Given two positive integers x and y, an integer is powerful if it is equal
* to x^i + y^j for some integers i >= 0 and j >= 0.
*
* Return a list of all powerful integers that have value less than or equal to
* bound.
*
* You may return the answer in any order. In your answer, each value should
* occur at most once.
*
*
*
*
* Example 1:
*
*
* Input: x = 2, y = 3, bound = 10
* Output: [2,3,4,5,7,9,10]
* Explanation:
* 2 = 2^0 + 3^0
* 3 = 2^1 + 3^0
* 4 = 2^0 + 3^1
* 5 = 2^1 + 3^1
* 7 = 2^2 + 3^1
* 9 = 2^3 + 3^0
* 10 = 2^0 + 3^2
*
*
*
* Example 2:
*
*
* Input: x = 3, y = 5, bound = 15
* Output: [2,4,6,8,10,14]
*
*
*
*
*
*
* Note:
*
*
* 1 <= x <= 100
* 1 <= y <= 100
* 0 <= bound <= 10^6
*
*/
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
vector<int> ans;
if(bound == 0) return ans;
unordered_set<int> ss;
for(int i = 0; (long)pow(x, i) <= (long)bound && !(x==1 && i>0); i++)
for(int j = 0; (long)pow(x, i)+pow(y, j)<=(long)bound && !(y==1 && j>0); j++)
ss.insert((int)pow(x, i)+pow(y, j));
for(auto s: ss)
ans.push_back(s);
return ans;
}
};