Ural 2070 Interesting Numbers
来源:互联网 发布:json数组格式怎样写 编辑:程序博客网 时间:2024/04/30 15:06
Description
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 (
Output
In the only line output one integer — the number of satisfying integers from segment [L, R].
题意
Nikolay 认为一个数是有趣的,若这个数是素数;Asya 认为一个数是有趣的,当一个数的约数个数为 k ,且 k 是一个素数。
问在区间
分析
首先,当一个数是素数的时候,这个数的约数个数为 2 (1 和自身),是素数;即两人必然同时认为一个素数是有趣的。
之后,考虑合数的情况。一个数约数的个数可以通过公式快速求得(套质因数分解的板):
很容易看到,当一个合数的因子数
>=2
时,这个数的约数个数必然是一个合数,则两人同时认为这个数不有趣。因此,只需要判断一个素数的任意幂次,当
故预处理出
HINT:由于最大右区间为
代码
#include<bits/stdc++.h>using namespace std;const long long inf = 1e12;const int N = 1000001;bool isprime[N];int primes[N/3], tot;void getPrime() { tot = 0; memset(isprime, 0, sizeof(isprime)); for(int i=2, j;i<N;i++) { if(!isprime[i]) primes[tot++] = i; for(j=0;j<tot && i*primes[j] < N;++j) { isprime[i*primes[j]] = true; if(i % primes[j] == 0) break; } }}int main(){ long long j, l, r; getPrime(); vector<long long> vec; for(int i=0, x;i<tot;i++) for(j=(long long)primes[i]*primes[i], x=2;j<=inf;j*=primes[i], ++x) if(!isprime[x+1]) vec.push_back(j); sort(vec.begin(), vec.end()); scanf("%I64d %I64d",&l,&r); int lidx = lower_bound(vec.begin(), vec.end(), l) - vec.begin(); int ridx = upper_bound(vec.begin(), vec.end(), r) - vec.begin(); printf("%I64d\n", r-l+1-(ridx-lidx));}
- ural 2070 - Interesting Numbers
- URAL 2070 Interesting Numbers
- Ural 2070 Interesting Numbers
- URAL 2070 Interesting Numbers(数学)
- URAL 2070 Interesting Numbers(数学)
- URAL 2070 Interesting Numbers (素数枚举)
- 素数标记 Interesting Numbers URAL - 2070
- URAL 2070 Interesting Numbers(数论)
- ural 2070 Interesting Numbers(数论基础)
- Interesting Numbers URAL
- URAL 2070. Interesting Numbers(素数打表 数学题)
- URAL 1385 Interesting Number
- URAL 1385 Interesting Number
- Scarily interesting! (URAL
- HDU 3195 Interesting Numbers
- UVA2070-Interesting Numbers
- ural2070-Interesting Numbers
- URAL 1385 Interesting Number 解题报告
- Java List的ArrayList实现
- MAC瘦身,XCODE手动清理
- MediaMuxer和MediaExtractor
- 日志-syslog系统日志
- wait,notify,notifyAll详细介绍
- Ural 2070 Interesting Numbers
- 常用的邮箱服务器(SMTP、POP3)地址、端口
- 自己编写的数据仓库梳理工具,不知道还需要什么功能,求指教
- JVM内存区域划分Eden Space、Survivor Space、Tenured Gen,Perm Gen解释
- 大数据学习线路
- JVM类加载器
- python QQ空间新说说邮件提醒功能实现
- Springmvc前端JSON转换器MappingJackson2HttpMessageConverter设置对值为null的处理
- 操作系统02进程管理Process_Description_and_Control