Codeforces 385 C Bear and Prime Numbers
做题感悟:这题属于想法题,比赛时直接做的 D 题。可是处理坐标处理的头晕眼花的结果到最后也没AC。
解题思路:
由于查询的时候仅仅考虑素数,so~我们仅仅考虑素数就能够,这就须要筛素数。我们能够在筛素数的同一时候把某个素数出现的倍数加上。输入的时候仅仅要记录某个数的个数就能够了。
代码:
#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT long long int
const int INF = 0x3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const int mod = 1000000007 ;
const int MY = 100 + 5 ;
const int MX = 10000000 + 5 ;
int Max ,n ,m ;
bool isprime[MX] ;
int sum[MX] ,num[MX] ;
void init() // 筛法同一时候记录个数
{
memset(isprime ,false ,sizeof(isprime)) ;
memset(sum ,0 ,sizeof(sum)) ;
for(int i = 2 ;i <= Max ; ++i)
{
sum[i] += sum[i-1] ;
if(!isprime[i])
{
sum[i] += num[i] ;
for(int j = i + i ;j <= Max ; j += i)
{
sum[i] += num[j] ;
isprime[j] = true ;
}
}
}
}
int main()
{
int x ;
while(~scanf("%d" ,&n))
{
memset(num ,0 ,sizeof(num)) ;
Max = 0 ;
for(int i = 0 ;i < n ; ++i)
{
scanf("%d" ,&x) ;
num[x]++ ; // 记录个数
Max = max(Max ,x) ;
}
init() ;
scanf("%d" ,&m) ;
int le ,rt ;
for(int i = 0 ;i < m ; ++i)
{
scanf("%d%d" ,&le ,&rt) ;
if(rt > Max) rt = Max ;
if(le > Max) cout<<"0"<<endl ;
else cout<<sum[rt]-sum[le-1]<<endl ;
}
}
return 0 ;
}
Codeforces 385 C Bear and Prime Numbers的更多相关文章
- 【Codeforces 385C】Bear and Prime Numbers
[链接] 我是链接,点我呀:) [题意] f[i]表示在x[]中有多少个数字是i的倍数 让你求出sum(f[i]) li<=i<=ri 且i是质数 [题解] 做筛法求素数的时候顺便把素数i ...
- 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 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- CF385C Bear and Prime Numbers 数学
题意翻译 给你一串数列a.对于一个质数p,定义函数f(p)=a数列中能被p整除的数的个数.给出m组询问l,r,询问[l,r]区间内所有素数p的f(p)之和. 题目描述 Recently, the be ...
- codeforces 680C C. Bear and Prime 100(数论)
题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces 385C Bear and Prime Numbers 预处理DP
题目链接:http://codeforces.com/problemset/problem/385/C 题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和 解 ...
随机推荐
- ZOJ 3498 Javabeans
脑筋急转弯. 如果是偶数个,那么第一步可以是$n/2+1$位置开始到$n$都减去$n/2$,后半段就和前半段一样了. 如果是奇数个,那么第一步可以是$(n+1)/2$位置开始到$n$都减去$(n+1) ...
- ZOJ 3495 Lego Bricks
计算几何,暴力. 题目中有一句话:$The$ $mass$ $of$ $each$ $brick$ $is$ $equally$ $distributed$ $and$ $it$ $will$ $be ...
- 计蒜客 UCloud 的安全秘钥(困难)(哈希)
UCloud 的安全秘钥(困难) 编辑代码 9.53% 1200ms 262144K 每个 UCloud 用户会构造一个由数字序列组成的秘钥,用于对服务器进行各种操作.作为一家安全可信的云计算平台,秘 ...
- Electron与jQuery中$符号冲突的三种解决方法
在Electron工程中引用jQuery时,经常会出现以下错误: Uncaught ReferenceError: $ is not defined 解决的具体方法如下: ①.在测试的过程中(测试过1 ...
- EasyUI学习总结(六)——EasyUI布局(转载)
本文转载自:http://www.cnblogs.com/xdp-gacl/p/4088198.html 一.EasyUI布局介绍 easyUI布局容器包括东.西.南.北.中五个区域,其中中心面板是必 ...
- [BZOJ4709][JSOI2011]柠檬(斜率优化DP)
显然选出的每一段首尾都是相同的,于是直接斜率优化,给每个颜色的数开一个单调栈即可. #include<cstdio> #include<vector> #include< ...
- 「2018山东一轮集训」Game
%%神仙题 首先转化一波模型:可以把原问题看成,初始每个位置有0/1个石子,1操作看成从一个位置拿走一个石子,2操作看成从l[i]拿走一个石子,并在[ l[i]+1 , r[i] ]的每个位置放上一个 ...
- BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏(树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3391 [题目大意] 给定一棵树,求分支size均不大于一半点数的点 [题解] 递归的同 ...
- double型(双精度型)的打印(printf) 和scanf
double型,printf()用%f输出,而scanf用%lf来接受输入. 格式 printf scanf %c int char * %d, %i int int * %o, %u, %x u ...
- android 关于Make sure the plugin is properly configured问题的解决办法
这个问题引发的原因最初的报错是: [2013-10-14 10:01:58 - XXX] The connection to adb is down, and a severe error has o ...