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个数中被能整除的次数之和 解 ...
随机推荐
- POJ 2031 Building a Space Station【最小生成树+简单计算几何】
You are a member of the space station engineering team, and are assigned a task in the construction ...
- Bzoj2818 Gcd(莫比乌斯反演)
题面 题意都在题目里面了 题解 你可以把题意看成这个东西 $$ \sum_{i=1}^n\sum_{j=1}^m\mathbf f(gcd(i,j)) $$ 其中$\mathbf f(n)$为$是否是 ...
- 【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)
1528: [POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到 ...
- BZOJ 1150 [CTSC2007]数据备份Backup(贪心+优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1150 [题目大意] 给出n个数,请你挑出k对(每个数不可重复选取),使得他们差的绝对值 ...
- 【高斯消元】CDOJ1783 曜酱的线性代数课堂(一)
高斯消元求逆矩阵板子. #include<cstdio> #include<cmath> #include<algorithm> #include<cstri ...
- 【线性基】hdu3949 XOR
给你n个数,问你将它们取任意多个异或起来以后,所能得到的第K小值? 求出线性基来以后,化成简化线性基,然后把K二进制拆分,第i位是1就取上第i小的简化线性基即可.注意:倘若原本的n个数两两线性无关,也 ...
- [POI2008]Triangles
题目大意: 平面直角坐标系上有n个点,问以这n个点为顶点的不同的三角形的面积和是多少? 思路: 很容易想到一个O(n^3)的暴力,枚举三个点,用海龙公式求一下面积和即可,这样做是40分. 标算的复杂度 ...
- Hiho----有向图欧拉回路
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho破解了一道又一道难题,终于来到了最后一关.只要打开眼前的宝箱就可以通关这个游戏了. 宝箱被一种奇怪的机关锁住 ...
- dump备份mysql表
Auth: Jin Date: 20140403 Content: #!/bin/bash - ### auth: Jin ### ### Desc: 备份输入库里的所有表,清理指定天前的备份文件 # ...
- C# 高德地图调用帮助类 GaodeHelper
/// <summary> /// 高德地图调用帮助类 /// 更多详情请参考 高德api /// </summary> public class GaodeHelper { ...