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的倍数的个数. 思路: ...
随机推荐
- HDU3045 Picnic Cows —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-3045 Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memor ...
- 【Java】DateUtil(1)
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- c#-关于自动属性的思考
参考:c#-关于自动属性的思考 我的理解:自动属性跟 公有字段 一模一样,编程习惯而已.目前是这么认为的. 自动属性:public string Name{ get; set } 公有字段:pub ...
- GDUT 积木积水 2*n 时间复杂度
题意 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元,于是他把这个三维的现实问 ...
- [Java] 继承,隐藏,覆盖,重载,多态,抽象类,接口
1.子类 class SonClass extends ABC{...} 在子类定义后,子类中就可以直接隐式包含父类的成员变量和方法,而不用再写,这就是使用继承的优点. 子类包含父类的成员,不是子类和 ...
- 【扬中集训DAY5T1】 交换矩阵
[题目链接] 点击打开链接 [算法] 链表,对于每个点,存它的上,下,左,右分别是谁 [代码] #include<bits/stdc++.h> using namespace std; # ...
- Park Visit(树的直径)
传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- registerWithTouchDispatcher 注册单点触摸事件
Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...
- Eclipse全项目搜索指定文件&字串
在eclipse中如果希望在大量的项目中寻找指定的文件可不是一件轻松的事,还好eclipse提供了强大的搜索功能. 我们可以通过通配符或正则表达式来设定查寻条件,下面是操作示例: ctrl+h 打开搜 ...
- 任务43:Identity MVC:UI
基于原来做的cookie认证的代码:MvcCookieAuthSample 增加登陆和退出的方法: 增加Login和SignIn这两个Action方法. 在Views下面创建Account文件夹,然后 ...