CF385C Bear and Prime Numbers
思路:
需要对埃氏筛法的时间复杂度有正确的认识(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的更多相关文章
- CF385C Bear and Prime Numbers 数学
题意翻译 给你一串数列a.对于一个质数p,定义函数f(p)=a数列中能被p整除的数的个数.给出m组询问l,r,询问[l,r]区间内所有素数p的f(p)之和. 题目描述 Recently, the be ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)
385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- 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 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
/* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...
- codeforces 385C Bear and Prime Numbers 预处理DP
题目链接:http://codeforces.com/problemset/problem/385/C 题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和 解 ...
- cf C Bear and Prime Numbers
题意:给你一个n,输入n个数,然后输入m,接下来有m个询问,每一个询问为[l,r],然后输出在区间内[l,r]内f(p)的和,p为[l,r]的素数,f(p)的含义为在n个数中是p的倍数的个数. 思路: ...
随机推荐
- antV G6流程图在Vue中的使用
最近我司项目中需要加入流程图制作功能,于是乎百度各种找可视化绘制拓扑图的轮子,大部分都是国外的,看文档太吃力,不过好在最终让我发现了AntV G6流程图图表库,最新版为2.0,不过编辑器在2.0版本还 ...
- HDU4292 Food —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/HDU-4292 Food Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- YTU 1439: 2.4.5 Fractions to Decimals 分数化小数
1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB 提交: 194 解决: 13 题目描述 写一个程序,输入一个形如N/ ...
- 获取cookie值
function get_cookie(Name) { var search = Name + "=" var returnvalue = ""; if (do ...
- POJ3020 二分图匹配——最小路径覆盖
Description The Global Aerial Research Centre has been allotted the task of building the fifth gener ...
- 禁用选择文本功能user-select
有时候,我们为了用户体验,需要禁用选择文本功能. 这需要用到一个CSS属性:user-select,user-select的文档点这里 user-select有两个值:none:用户不能选择文本tex ...
- 高效使用ppt素材
一.素材大致分为: 立体素材:以TG素材为代表的那种高光立体素材 平面素材:以咨询公司麦肯锡.罗兰贝格公司为代表的平面设计的素材 二.使用原则: 原则一:平面左边,立体右边 这个原则告诉你几件事: 如 ...
- 使用VirtualBox虚拟机搭建局域网(续)
最近家中Windows 10更新后抽风,最后不得不重装系统,原本配置好的开发环境全部要重装,也包括局域网搭建,但由于是家庭网络并非公司局域网,故旧文<使用VirtualBox虚拟机搭建局域网&g ...
- bzoj 3053: The Closest M Points【KD-tree】
多维KDtree板子 左右儿子的估价用mn~mx当区间,假设区间里的数都存在:k维轮着做割点 #include<iostream> #include<cstdio> #incl ...
- springcloud(一) 服务拆分
一般我们的项目如果需要从单应用服务升级到微服务,必须要将原来的服务做拆分,我这边的拆分也是基于将之前spb-demo的springboot单应用做拆分,拆分出三个应用,spb-brian-query- ...