CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了
Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.
xn的范围在(2 ≤ xi ≤ 107),而 l, r 的范围在 (2 ≤ li ≤ ri ≤ 2·109) ,易得在计算的时候是不会考虑107 以后了
首先写一个素数快速打表,同时也统计一下在 l, r 范围内每个数满足题目条件的总数 (虽然觉得这样的打表方法的确很慢)
然后注意了,因为查询的次数很多,多达5*104次 ,所以在统计的时候可以算出累计和以节省时间
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ; int cnt[MAXN],a[MAXN];
bool check[MAXN]; void init_prime(){
for(int i = ; i < MAXN; ++i){ //素数打表
if(!check[i]){
for(int j = i; j < MAXN; j += i){
check[j] = true;
cnt[i] += a[j];
}
}
}
} int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, numCase = ;
int n, q, x, y;
cin >> n;
for(i = ; i <= n; ++i){
cin >> x;
++a[x];
}
init_prime();
for(i = ; i < MAXN; ++i){
cnt[i] += cnt[i - ]; //作累计和以节省查询时间
}
cin >> t;
while(t--){
cin >> x >> y;
checkmin(x, );
checkmin(y, );
cout << cnt[y] - cnt[x - ] << endl;
}
return ;
}
CodeForces 385C Bear and Prime Numbers 素数打表的更多相关文章
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- 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 (埃氏筛的美妙用法)
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个数中被能整除的次数之和 解 ...
- UVA 10539 - Almost Prime Numbers 素数打表
Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...
- 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 ...
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
随机推荐
- IOS 学习笔记(3) 视图UITabbarController
1.UITabbarViewController标签试图控制器.由于标签页本就起着分类的作用,所以往往呈现的视图内容之间,可以是毫不相关的功能. UITabbarViewController仍然继承自 ...
- 怎么给没链接的flash加超链接
最近开始准备设计一个广告条,本想用阿里妈妈的的banner marker来设计,却遗憾的发现,banner marker已经实行收费模式了. 我不得不启用另一款在线banner生成工具,百度旗下的&q ...
- Protel99se生成gerber文件的方法
这几天工作中遇到制作 gerber 的问题,确实让我郁闷了一 番,为了方便大家,不要再受其苦,特将这一过程写成文档供有这方面需要的同仁们参考. 1 protel99se 打开要生成 gerber 的 ...
- TreeView控件例子
XmL文件代码: <?xml version="1.0" encoding="utf-8" ?> <Area> <Province ...
- 把一个数组向右循环移动k位要求时间复杂度为O(n)
今晚做了下某公司的网络笔试题,好久没刷题了,现在渣得要死,里面有道程序设计题是 把一个数组向右循环移动k位要求时间复杂度为O(n) 给的方法定义为 public void solution(int a ...
- 用elasticsearch索引mongodb数据
参照网页:单机搭建elasticsearch和mongodb的river 三个步骤: 一,搭建单机replicSet二,安装mongodb-river插件三,创建meta,验证使用 第一步,搭建单机m ...
- Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- ThinkPHP - 事务操作
/* * 添加酒店和房型 * */ public function insertAll($arr_hotel=array(),$arr_room=array()){ $model = new Mode ...
- 字符串匹配算法(KMP)
字符串匹配运用很广泛,举个简单例子,我们每天登QQ时输入账号和密码,大家有没有想过账号和密码是怎样匹配的呢?登录需要多长时间和匹配算法的效率有直接的关系. 首先理解一下前缀和后缀的概念: 给出一个问题 ...
- MYSQL 好文章集锦
比较细致的讲解MySQL数据库的数据结构以及实现原理 MySQL索引背后的数据结构及算法原理 MySQL的InnoDB索引原理详解 MySQL索引原理及慢查询优化 持续更新,快乐学习.