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 ...
随机推荐
- Java: Best Way to read a file
经常在各种平台的online test里面不熟悉STDIN, STOUT,下面举个例子: Input Format There are three lines of input: The first ...
- SpringMVC学习笔记二第一个小的程序
首先:我们要用springmvc来写一个helloworld的例子: 首先我们需要导入所需要的架包: /demo1/WebRoot/WEB-INF/lib/commons-logging-1.1.1. ...
- Django初级手册6-静态文件
用Django加载外部文件 在Django中iamges,JS或者CSS通称为static文件 定制APP的外观 一般放在应用目录下的static/polls/目录下,下为polls/static/p ...
- windows8.1的启动目录的路径
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\ 或者使用斜杠表示,就像这样: C:/ProgramData/Microsof ...
- css+div table
div+css table表格样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- mysql备份恢复详解
前言 为什么需要备份数据? 数据的备份类型 MySQL备份数据的方式 备份需要考虑的问题 设计合适的备份策略 实战演练 使用cp进行备份 使用mysqldump+复制BINARY LOG备份 使用lv ...
- Linux服务器---安装jdk
安装jdk jdk是运行或者开发java的必须工具,很多软件都会依赖jdk,因此必须学会安装jdk 1.查看当前系统的jdk情况 [root@localhost wj]# rpm -qa | grep ...
- mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法
mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...
- pyDay7
内容来自廖雪峰的官方网站 1.如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 2.只要是可迭代对象,无论有无下标, ...
- SNMP学习笔记之SNMP的安装及Python的调用
0x00 概述 本文是介绍SNMP在Windows和Linux(Ubuntu)下的安装,以及通过Python调用其接口的文章. 0x01 开发环境 Python 3.5.1 Windows 10 64 ...