ural 2070. Interesting Numbers
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 |
Problem Author: Alexey Danilyuk
Problem Source: Ural Regional School Programming Contest 2015
Problem Source: Ural Regional School Programming Contest 2015
Tags: number theory
Difficulty: 221
题意:问l~r之间
1、x是个质数
2、约数个数是质数
要么满足两者,要么两者都不满足的数的个数
分析:
我们只要扣除只满足一个的数就可以了
首先1特殊处理
考虑x=p1^k1 * p2^k2 *....
当质因子超过1个,则两者都不满足
当质因子只有一个,当k==1时,两者满足
当k>1时,就要扣除
所以只用枚举质数,查看在l-r内的幂就行了
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int N = 1000010; 6 typedef long long LL; 7 LL l, r; 8 LL prime[N]; 9 int tot; 10 bool visit[N]; 11 12 inline void input() 13 { 14 cin >> l >> r; 15 } 16 17 inline void solve() 18 { 19 if(l == r && l == 1) 20 { 21 cout << "1\n"; 22 return; 23 } 24 25 LL ans = 0; 26 if(l == 1) l++, ans++; 27 for(int i = 2; i < N; i++) 28 { 29 if(!visit[i]) prime[++tot] = i; 30 for(int j = 1; j <= tot; j++) 31 { 32 if(prime[j] * i >= N) break; 33 visit[prime[j] * i] = 1; 34 if(!(i % prime[j])) break; 35 } 36 } 37 38 ans += r - l + 1; 39 for(int i = 1; i <= tot; i++) 40 { 41 LL now = 1; 42 int times = 0; 43 while(now < l) now *= prime[i], times++; 44 while(now <= r) 45 { 46 if(times > 1 && !visit[times + 1]) ans--; 47 now *= prime[i], times++; 48 } 49 } 50 51 cout << ans << "\n"; 52 } 53 54 int main() 55 { 56 ios::sync_with_stdio(0); 57 input(); 58 solve(); 59 return 0; 60 }