385C - Bear and Prime Numbers

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

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset((a),(b),sizeof(a))
const int INF=0x3f3f3f3f;
const int N=1e6+;
const int M=1e7+;
int vis[M]={};
bool not_prime[M]={false};
int dp[M]={};
void prime()
{
for(int i=;i<M;i++)
{
if(!not_prime[i])
{
dp[i]+=vis[i];
for(int j=i+i;j<M;j+=i)
not_prime[j]=true,dp[i]+=vis[j];
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,x;
cin>>n;
for(int i=;i<n;i++)
{
cin>>x;
vis[x]++;
} prime();
for(int i=;i<M;i++)dp[i]+=dp[i-];
int m,l,r;
cin>>m;
while(m--)
{
cin>>l>>r;
if(l>M)l=M-;
if(r>M)r=M-;
cout<<dp[r]-dp[l-]<<endl;
}
return ;
}

Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)的更多相关文章

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

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

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

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

  3. Codeforces 385C Bear and Prime Numbers

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

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

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

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

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

  6. codeforces 414A A. Mashmokh and Numbers(素数筛)

    题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...

  7. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  8. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  9. CF385C Bear and Prime Numbers 数学

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

随机推荐

  1. PIMPL(一)

    1 参考 <effective C++> 条款31:将文件间的编译关系降至最低 PIMPL Idiom: http://c2.com/cgi/wiki?PimplIdiom 2 什么是PI ...

  2. 表单验证——JqueryValidator、BootstrapValidator

    表单验证两种方式: 1.JqueryValidator <!DOCTYPE html> <html lang="en"> <head> < ...

  3. fontawesome与amazeUI

    fontawesome是很不错的东西呢~~~~ amazeUI也是很不错的东西呢~~~~~ 最近一年里,比较偏爱这两个家伙? 什么?为什么bootstrap?有什么区别? 这个我还真说不太清楚. 用一 ...

  4. python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名

    python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名,如果在类中定义的就是类的私有成员. >>> dir(__builtin ...

  5. python 代码覆盖率 coverage用法

    先装coverage: D:\test_python\e8_test>pip install coverageCollecting coverage  Downloading https://f ...

  6. Linux 远程连接sftp与ftp

    linux sftp远程连接命令 sftp -oPort=60001 root@192.168.0.254 使用-o选项来指定端口号. -oPort=远程端口号 sftp> get /var/w ...

  7. 关于webpack的path和publicPath。

    path:所有输出文件的目标路径; publicPath:输出解析文件的目录,url 相对于 HTML 页面 区别:path是webpack所有文件的输出的路径,必须是绝对路径,比如:output输出 ...

  8. WebService(JAX-WS、XFire、Axis三种)获取客户端ip

    WebService(JAX-WS.XFire.Axis三种)获取客户端ip JAX-WS.XFire.Axis三种webservice的获取客户端IP的简单实现过程: 1,基于JDK6 jax-ws ...

  9. 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 3 章 答案

    判断对错 1.由计算机存储和操作的信息称为数据.2.由于浮点数是非常准确的,所以通常应该使用它们,而不是int.3.像加法和减法这样的操作在mAth库中定义.4.n 项的可能排列的数目等于 n!.5. ...

  10. my normal Header

    #ifndef INCLUDES_MY #define INCLUDES_MY //默认登录名密码 #define DEFAULT_USERNAME "admin" #define ...