2070. Interesting Numbers
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Nikolay
and Asya investigate integers together in their spare time. Nikolay
thinks an integer is interesting if it is a prime number. However, Asya
thinks an integer is interesting if the amount of its positive divisors
is a prime number (e.g., number 1 has one divisor and number 10 has four
divisors).
Nikolay
and Asya are happy when their tastes about some integer are common. On
the other hand, they are really upset when their tastes differ. They
call an integer satisfying if they both consider or do not consider this
integer to be interesting. Nikolay and Asya are going to investigate
numbers from segment [L; R] this weekend. So they ask you to calculate the number of satisfying integers from this segment.
Input
In the only line there are two integers L and R (2 ≤ L ≤ R ≤ 1012).
Output
In the only line output one integer — the number of satisfying integers from segment [L; R].
Samples
input | output |
---|---|
3 7
|
4
|
2 2
|
1
|
77 1010
|
924
|
题目大意:Nikolay 认为质数有趣; Asya认为一个数的因子个数是质数,那么这个数也是有趣的。
你统计区间[a, b]内满足Nikolay 和 Asya 都认为有趣或者两个人都认为没趣的数的个数
思路:(1)每个素数有两个因子肯定满足两个人的兴趣
(2)那么这个数不是质数并且它的因子个数不是质数才行
所以减去这个数只满足该数的因子个数为质数而且该数是合数
对于“正规”的合数, 有多个质因子, 如6的质因子是2,3, 会产生两对(1,6),(2, 3),因子个数为2*n不满足条件
这是为啥?例如 n= p1a1*p2a2, n的因子个数为(1+a1)*(1+a2)显然不是质数。
所以我们要探索那些 pk, p为质数,k为整数, 它的因子为(1, p1,p2, p3,,,,pk)共有(k+1)个因子
1 #include <iostream> 2 #include <sstream> 3 #include <fstream> 4 #include <string> 5 #include <vector> 6 #include <deque> 7 #include <queue> 8 #include <stack> 9 #include <set> 10 #include <map> 11 #include <algorithm> 12 #include <functional> 13 #include <utility> 14 #include <bitset> 15 #include <cmath> 16 #include <cstdlib> 17 #include <ctime> 18 #include <cstdio> 19 #include <cstring> 20 #define FOR(i, a, b) for(int i = (a); i <= (b); i++) 21 #define RE(i, n) FOR(i, 1, n) 22 #define FORP(i, a, b) for(int i = (a); i >= (b); i--) 23 #define REP(i, n) for(int i = 0; i <(n); ++i) 24 #define SZ(x) ((int)(x).size ) 25 #define ALL(x) (x).begin(), (x.end()) 26 #define MSET(a, x) memset(a, x, sizeof(a)) 27 using namespace std; 28 29 int N, T; 30 const int MAXN = 1000006; 31 bool a[MAXN]; 32 long long cnt, prime[MAXN]; 33 void init(){ 34 cnt = 0; 35 for(int i = 2; i < MAXN; i++) a[i] = true; 36 for(int i = 2; i < MAXN; i++){ 37 if(a[i]){ 38 prime[++cnt] = i; 39 } 40 for(int j = 1; j <= cnt; j++){ 41 if(i*prime[j] >= MAXN) break; 42 a[i*prime[j]] = false; 43 if(i%prime[j] == 0) break; 44 } 45 } 46 } 47 long long int slove(long long int x){ 48 int res = 0; 49 for(int i = 1; i <= cnt; i++){ 50 long long tmp = prime[i]*prime[i]; 51 //printf("%I64d ", tmp); 52 int k = 2; 53 for(;tmp <= x; tmp = tmp*prime[i],k++){ 54 if(tmp <= x && a[k+1]){ 55 res++; 56 //printf("%I64d ", tmp); 57 } 58 if(tmp > x) break; 59 } 60 } 61 return x - res; 62 } 63 int main() { 64 //freopen("in.txt", "r", stdin); 65 init(); 66 long long int n, m; 67 scanf("%I64d%I64d", &n, &m); 68 printf("%I64d", slove(m) - slove(n-1)); 69 return 0; 70 }