codeforces 955C - Sad powers

portal

 

Q (1 ≤  Q  ≤ 10 5 ) group query, given L, R  (1 ≤  L  ≤  R  ≤ 10 18 ). , how many numbers in the closed interval can be expressed as a number to the power of k (k > 1) )

 

For the case of k=2, we can directly find the root and do the difference. For the case of k>3, since the number of all numbers is small, we can directly enumerate them. In the process, pay attention to the weight and the square number (otherwise, repeat the calculation with Case 1)

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const LL up = 1e18;
10 
11 int Q;
12 LL L, R;
13 
14 vector<LL> vec;
15 
16 void init() {
17     vector<LL> tmp;
18     tmp.push_back(1);
19     LL _up = 1e6;
20     for (LL i = 2; i < _up; i++) {
21         for (LL j = i * i * i; ; j *= i) {
22             tmp.push_back(j);
23             if (j > up / i) break;
24         }
25     }
26     sort(tmp.begin(), tmp.end());
27     tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
28     for (int i = 0; i < tmp.size(); i++) {
29         LL t = tmp[i];
30         LL tt = sqrt(t);
31         if (tt * tt != t) vec.push_back(tmp[i]);
32     }
33 }
34 
35 LL lower_root(LL A) {
36     LL t = sqrt(A);
37     if (t * t == A) t--;
38     return t;
39 }
40 
41 
42 int main() {
43     init();
44     scanf("%d", &Q);
45     while (Q--) {
46         scanf("%lld%lld", &L, &R);
47         LL ans = upper_bound(vec.begin(), vec.end(), R)
48                - lower_bound(vec.begin(), vec.end(), L);
49         ans += lower_root(R + 1) - lower_root(L);
50         printf("%lld\n", ans);
51     }
52 
53     return 0;
54 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523407&siteId=291194637