codeforces 385C Bear and Prime Numbers

题意:给你一个数列,和m个询问,求 数组种 l -r 中所有质数的的倍数的个数和。

解题思路:变形筛法。注意细节

解题代码:

 1 // File Name: 385c.cpp
 2 // Author: darkdream
 3 // Created Time: 2015年03月07日 星期六 18时24分53秒
 4
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 using namespace std;
26 const int maxn = 1000000*10 + 1000;
27 int      ans[maxn];
28 int       hs[maxn];
29 int      ahs[maxn];
30 int n ;
31 void solve()
32 {
33    //int tmp = sqrt(maxn);
34    for(int i = 2;i < maxn;i ++)
35    {
36        if(hs[i] == 0)
37        {
38           for(int j = i ;j < maxn;j += i)
39            {
40
41               ans[i] += ahs[j] ;
42               hs[j] = 1;
43           }
44        }
45    }
46    for(int i = 2;i <= maxn;i ++)
47    {
48      ans[i] += ans[i-1];
49    }
50 }
51 int main(){
52   memset(ans,0,sizeof(ans));
53   memset(hs,0,sizeof(hs));
54   memset(ahs,0,sizeof(ahs));
55   scanf("%d",&n);
56   int _max = 0;
57   for(int i = 1;i <= n;i ++)
58   {
59      int tmp ;
60      scanf("%d",&tmp);
61      ahs[tmp] ++;
62      _max = max(tmp,_max);
63   }
64   solve();
65   int m ;
66   scanf("%d",&m);
67   for(int i = 1;i <= m;i ++)
68   {
69     int l , r;
70     scanf("%d %d",&l,&r);
71     if(l > _max)
72     {
73       printf("0\n");
74       continue;
75     }
76     r = min(r,_max);
77     printf("%d\n",ans[r] - ans[l-1]);
78   }
79 return 0;
80 }

时间: 2025-01-02 17:47:45

codeforces 385C Bear and Prime Numbers的相关文章

Codeforces 385C Bear and Prime Numbers(素数预处理)

Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出现个数时使用了map,以至于后面做前缀和的累加时,每次都要对map进行查询,以至于TLE.而自己一直没有发现,以为是欧拉筛对于这道题还不够优,于是上网搜题解,发现别人的做法几乎一样,但是却能跑过,挣扎了许久才想起是map的原因.map的内部实现是一颗红黑树,每次查询的复杂度为O(logN),在本来时

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

Codeforces 385C Bear and Prime Numbers [素数筛法]

Code: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<queue> #include<deque> #include<stack> #include<map&g

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

Codeforces A - Bear and Prime 100(交互题)

A - Bear and Prime 100 思路:任何一个合数都可以写成2个以上质数的乘积.在2-100中,除了4,9,25,49外都可以写成两个以上不同质数的乘积. 所以打一个质数加这四个数的表:{2,3,4,5,7,9,11,13,17,19,23,25,29,31,37,41,43,47,49},询问19次,如果能被整出两次以上,说明是合数,否则是质数. #include<bits/stdc++.h> using namespace std; #define ll long long

Codeforces 385 C Bear and Prime Numbers

题目链接~~> 做题感悟:这题属于想法题,比赛时直接做的 D 题,但是处理坐标处理的头晕眼花的结果到最后也没AC. 解题思路: 因为查询的时候只考虑素数,so~我们只考虑素数就可以,这就需要筛素数,我们可以在筛素数的同时把某个素数出现的倍数加上,输入的时候只要记录某个数的个数就可以了. 代码: #include<iostream> #include<sstream> #include<map> #include<cmath> #include<f

Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(暴力)

题目链接:点击打开链接 题意:给n个数, m次询问, 每次询问时一个区间[l, r]. 求这个区间中的所有素数,能被n个数中的几个数整除的累加和. 思路:没想到什么好办法, 直接筛出素数以后直接暴力的,没想到跑的这么快.. 最费时间的大概就是那个二重循环, 但是可能因为素数比较少, 所以实际的时间复杂度并不高. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostr

codeforces 679A Bear and Prime 100 交互

第一次交互题,记录一下吧 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std; typed

暑假练习赛 006 B Bear and Prime 100

Bear and Prime 100Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 680C uDebug Description Input Output Sample Input Sample Output Hint Description