1+ /*
2+ * @Author: LetMeFly
3+ * @Date: 2024-11-22 20:51:14
4+ * @LastEditors: LetMeFly.xyz
5+ * @LastEditTime: 2024-11-22 21:22:17
6+ */
7+ #ifdef _WIN32
8+ #include " _[1,2]toVector.h"
9+ #endif
10+
11+ #ifdef FirstTry // 在搞什么
12+ class Solution {
13+ public:
14+ int nonSpecialCount (int l, int r) {
15+ int ans = 0 ;
16+ for (int i = l; i <= r; i++) {
17+ int x = sqrt (i), y = sqrt (x);
18+ if (x * x == i && y * y != x) {
19+ ans++;
20+ }
21+ }
22+ return ans;
23+ }
24+ };
25+ #else // FirstTry
26+ #ifdef SecondTry // WA,3是质数但不是平方数
27+ class Solution {
28+ public:
29+ int nonSpecialCount (int l, int r) {
30+ int ans = r - l + 1 ;
31+ int from = sqrt (l), to = sqrt (r);
32+ if (from * from != l) {
33+ from++;
34+ }
35+ if (to * to != r) {
36+ to--;
37+ }
38+ for (int i = from; i <= to; i++) {
39+ int t = sqrt (i);
40+ ans -= t * t == i;
41+ // printf("i = %d, sqrt(%d) * sqrt(%d) = %d\n", i, i, i, sqrt(i) * sqrt(i)); //*****
42+ printf (" i = %d, t = %d, %d * %d = %d\n " , i, t, t, t, t * t); // *****
43+ }
44+ return ans;
45+ }
46+ };
47+ /*
48+ i = 2, t = 1, 1 * 1 = 1
49+ i = 3, t = 1, 1 * 1 = 1
50+ i = 4, t = 2, 2 * 2 = 4
51+ */
52+ #else // SecondTry
53+ #ifdef ThirdTry // WHAT!!! 有点不在状态今天
54+ class Solution {
55+ private:
56+ int isPrime (int n) {
57+ // if (n == 1) {
58+ // return false;
59+ // }
60+ for (int i = 2 ; i <= sqrt (n); i++) {
61+ if (n % i == 0 ) {
62+ return false ;
63+ }
64+ }
65+ return true ;
66+ }
67+ public:
68+ int nonSpecialCount (int l, int r) {
69+ int ans = r - l + 1 ;
70+ int from = sqrt (l), to = sqrt (r);
71+ if (from * from != l) {
72+ from++;
73+ }
74+ for (int i = from; i < to; i++) {
75+ ans -= isPrime (i);
76+ // printf("isPrime(%d) = %d\n", i, isPrime(i));
77+ }
78+ return ans;
79+ }
80+ };
81+ #else // ThirdTry
82+ // FourthTry // Copy
83+ class Solution {
84+ public:
85+ int nonSpecialCount (int l, int r) {
86+ int n = sqrt (r);
87+ vector<int > v (n + 1 );
88+ int res = r - l + 1 ;
89+ for (int i = 2 ; i <= n; i++) {
90+ if (v[i] == 0 ) {
91+ if (i * i >= l && i * i <= r) {
92+ res--;
93+ }
94+ for (int j = i * 2 ; j <= n; j += i) {
95+ v[j] = 1 ;
96+ }
97+ }
98+ }
99+ return res;
100+ }
101+ };
102+ #endif // ThirdTry
103+ #endif // SecondTry
104+ #endif // FirstTry
0 commit comments