Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)
思路:记录数组中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)的更多相关文章
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)
Recently, the bear started studying data structures and faced the following problem. You are given a ...
- codeforces 385C Bear and Prime Numbers 预处理DP
题目链接:http://codeforces.com/problemset/problem/385/C 题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和 解 ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- Prime Path素数筛与BFS动态规划
埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- CF385C Bear and Prime Numbers 数学
题意翻译 给你一串数列a.对于一个质数p,定义函数f(p)=a数列中能被p整除的数的个数.给出m组询问l,r,询问[l,r]区间内所有素数p的f(p)之和. 题目描述 Recently, the be ...
随机推荐
- Perl中的正则表达式(五)
正则表达式(Regular Expression),在Perl里边通常也叫做模式(Pattern),用来表示匹配(或不匹配)某个字符串的特征模板. 使用简单模式:若模式匹配的对象是$_的内容,只要把模 ...
- Python: 序列list:保持元素顺序同时消除重复值
问题:怎样在Python的一个序列上面保持元素顺序的同时消除重复的值?answer:如果序列上的值都是hashable 类型,那么可以很简单的利用集合或者生成器来解决这个问题. eg1: def de ...
- 在Java中使用Sqlite数据库
一.安装 下载最新的 Sqlite Jdbc 驱动程序jar文件,并添加到Java工程的class路径下: 二.使用 以 sqlite Jdbc 驱动版本为 sqlitejdbc-v56.jar 为例 ...
- linux服务器---代理认证
代理认证 proxy代理服务被广泛的使用,为了安全起见,可以在服务器上增加一层安全认证机制.这里使用htpasswd建立认证账号和密码 1.创建认证账号和密码 [root@localhost wj]# ...
- (一)github之基础概念篇
1.github: 一项为开发者提供git仓库的托管服务, 开发者间共享代码的场所.github上公开的软件源代码全都由git进行管理. 2.git: 开发者将源代码存入名为git仓库的资料库中,而g ...
- Git在windows下上传文件至github流程
github是开发者分享的一个平台,这里不多说,想要上传文件至github需要有一个开发者账号,还需要在windows下安装好了git. 做好准备工作之后,接下来操作 一:登录github,创建项目 ...
- np.tile 和np.newaxis
output array([[ 0.24747071, -0.43886742], [-0.03916734, -0.70580089], [ 0.00462337, -0.5143158 ...
- windows服务与自启动程序的区别(转载)
转载:http://blog.csdn.net/anddy926/article/details/8464142 在客户端服务器项目实践中,作为服务端必须保持程序的24小时不间断运行,需要做一个监控, ...
- 我们能从 jQuery 的一个正则表达式中学到什么?
注意,该篇文章当前为粗糙的 v0.9 版本,会在稍后润色更新. 让我们来看一道思考题,根据 rejectExp,分析其正则执行过程中如何进行过滤?包含哪些执行步骤? rejectExp 变量的值如下: ...
- Python3基础 bool类型变量赋值
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...