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

Codeforces 385C Bear and Prime Numbers

其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1)。

但是,我在统计数组中元素出现个数时使用了map,以至于后面做前缀和的累加时,每次都要对map进行查询,以至于TLE。而自己一直没有发现,以为是欧拉筛对于这道题还不够优,于是上网搜题解,发现别人的做法几乎一样,但是却能跑过,挣扎了许久才想起是map的原因。map的内部实现是一颗红黑树,每次查询的复杂度为O(logN),在本来时间就不富裕的情况下,导致了超时。改用数组来统计后,顺利AC。做题时,在空间允许的情况下,还是尽量用数组来做整数间的映射吧,因为这道题纠结了许久,于是做个记录。

附上AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<algorithm>
#include<climits>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef map<int, int> M;
typedef vector<int> V;
typedef queue<int> Q;
const int maxn=10000000+5;
int cnt[maxn];
bool is[maxn];
int prime[maxn/2];
ll sum[maxn];
void init(int mx)
{
    int i,j,count=0;
    for (i=2;i<=mx;++i)
    {
        if (!is[i])
        {
            prime[count++]=i;
        }
        for (j=0;j<count&&i*prime[j]<=mx;++j)
        {
            is[i*prime[j]]=true;
            if (i%prime[j]==0)
                break;
        }
    }
    for (i=2;i<=mx;++i)
    {
        if (!is[i])
        {
            for (j=1;j*i<=mx;++j)
            {
                sum[i]+=cnt[i*j];
            }
            sum[i]+=sum[i-1];
        }
        else
            sum[i]=sum[i-1];
    }
    return;
}
int main()
{
    int n,t,m,k,i,j;
    cin>>n;
    for (i=0;i<n;++i)
    {
        scanf("%d",&t);
        cnt[t]++;
    }
    init(maxn);
    cin>>m;
    while (m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        l=min(maxn,l);
        r=min(maxn,r);
        printf("%d\n",sum[r]-sum[l-1]);
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/orangee/p/8977964.html

时间: 2024-10-12 19:23:48

Codeforces 385C Bear and Prime Numbers(素数预处理)的相关文章

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 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 #in

UVA 10539 Almost Prime Numbers( 素数因子)

Problem AAlmost Prime NumbersTime Limit: 1 second Almost prime numbers are the non-prime numbers which are divisible by only a single prime number. In this problem your job is to write a program which finds out the number of almost prime numbers with

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

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