思路:

需要对埃氏筛法的时间复杂度有正确的认识(O(nlog(log(n)))),我都以为肯定超时了,结果能过。

实现:

 #include <bits/stdc++.h>
using namespace std; bool is_prime[];
vector<int> prime;
int num[], ans[];
void sieve(int n)
{
for (int i = ; i <= n; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i <= n; i++)
{
if (is_prime[i])
{
prime.push_back(i);
for (int j = * i; j <= n; j += i) is_prime[j] = false;
}
}
}
int main()
{
sieve();
int n, m, x, a, b;
while (scanf("%d", &n) != EOF)
{
memset(num, , sizeof num);
memset(ans, , sizeof ans);
int l = prime.size();
for (int i = ; i < n; i++) { scanf("%d", &x); num[x]++; }
for (int i = ; i < l; i++) // 这段代码和素数筛法是一样的
{
int now = prime[i];
for (int j = now; j <= ; j += now)
{
if (num[j]) ans[now] += num[j];
}
}
for (int i = ; i <= ; i++) ans[i] += ans[i - ];
scanf("%d", &m);
for (int i = ; i < m; i++)
{
scanf("%d %d", &a, &b);
if (a > b) { puts(""); continue; }
a = min(a, ); b = min(b, );
printf("%d\n", ans[b] - ans[a - ]);
}
}
return ;
}

CF385C Bear and Prime Numbers的更多相关文章

  1. CF385C Bear and Prime Numbers 数学

    题意翻译 给你一串数列a.对于一个质数p,定义函数f(p)=a数列中能被p整除的数的个数.给出m组询问l,r,询问[l,r]区间内所有素数p的f(p)之和. 题目描述 Recently, the be ...

  2. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  3. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

  4. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  5. CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)

    Recently, the bear started studying data structures and faced the following problem. You are given a ...

  6. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  7. Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers

    /* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...

  8. codeforces 385C Bear and Prime Numbers 预处理DP

    题目链接:http://codeforces.com/problemset/problem/385/C 题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和 解 ...

  9. cf C Bear and Prime Numbers

    题意:给你一个n,输入n个数,然后输入m,接下来有m个询问,每一个询问为[l,r],然后输出在区间内[l,r]内f(p)的和,p为[l,r]的素数,f(p)的含义为在n个数中是p的倍数的个数. 思路: ...

随机推荐

  1. 开发工具、Object类(java基础知识十一)

    1.常见开发工具介绍 * A:操作系统自带的记事本软件 * B:高级记事本软件 * C:集成开发环境 IDE     * (Integrated Development Environment) *  ...

  2. html5--6-63 布局

    html5--6-63 布局 实例 学习要点 掌握传统布局与CSS3新增布局方式的实现和应用 掌握CSS3新增属性box-sizing 了解CSS3新增的多列布局 常用布局方式 固定布局与流体布局的优 ...

  3. ubuntu docker的安装和使用

    Docker CE for Ubuntu Docker CE for Ubuntu is the best way to install the Docker platform on Ubuntu L ...

  4. 识别String类型变量的问题

    碰到了android无法识别string的问题 Cursor cursor = db.query(true, "user", new String[]{"id" ...

  5. SPOJ:Bits. Exponents and Gcd(组合数+GCD)

    Rastas's has been given a number n. Being weak at mathematics, she has to consider all the numbers f ...

  6. AFNetworking源码的学习

    忽略编译警告 AFNetworking源码中常常会出现忽略警告的代码,如下: 1 2 3 4 #pragma clang diagnostic push #pragma clang diagnosti ...

  7. bzoj4547

    矩阵乘法 看成了合并果子... 就是斐波那契数列,只是有负数的时候,先把负数变成正的,然后矩乘 矩乘还是用单位举矩阵记录快速幂的矩阵比较保险 #include<cstdio> #inclu ...

  8. Vue scrollBehavior 滚动行为

    使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意: 这个功能只 ...

  9. k8s认证及ServiceAccount-十五

    一.ServiceAccount (1)简介 https://www.kubernetes.org.cn/service-account Service account是为了方便Pod里面的进程调用K ...

  10. NC文件的处理【netcdf】

    NC是气象领域数据的标准格式之一. 能够更好的存储格点数据. 下面为测试NC文件的读写. git:https://git.oschina.net/ipnunu/nctest pom.xml <p ...