[InterviewBit] Powerful Divisors

Powerful Divisors

  • Time :
  • Space :
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
int dp[1010101];
int helper(int n) {
if(n == 1) return 1;
if(dp[n]) return dp[n];
int& res = dp[n];
int N = n;
bool fl = true;
for(int i = 2; i <= sqrt(N) and fl; i++) {
if(N % i) continue;
int c = 1;
while((N % i) == 0) {
N /= i;
c++;
}
fl = __builtin_popcount(c) == 1;
}
return res += helper(n-1) + fl;
}

vector<int> Solution::powerfulDivisors(vector<int> &A) {
vector<int> res;
for(auto& a : A) {
res.push_back(helper(a));
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/09/PS/interviewbit/powerful-divisors/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.