题目链接:http://codeforces.com/problemset/problem/385/C

题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和

解题思路:首先暴力打出一张素数表,最大的素数小于等于n个数中的最大值即可。在打表的过程就统计从2开始到当前素数的总的整除次数(简直简单粗暴),最后对于询问区间,找出该区间内的最小素数与最大素数在素数表中的位置,结果即为s[r]-s[l-1]

代码如下:

 #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 10000005
int now,t;
int c[maxn],f[maxn],s[maxn];
bool prime[maxn]; void solve(int x)
{
t=;
int i,j;
for(i=;i<=x;i++)
{
if(!prime[i])
{
prime[i]=true;
f[now]=i;
t+=c[i];
// s[i]+=c[i];
for(j=i+i;j<=x;j+=i)
{
prime[j]=true;
t+=c[j];
}
s[now]=t;
now++;
}
}
} int lfind(int x)//找大于等于x的最小值
{
int l=,r=now-,mid;
while(l<=r)
{
mid=(l+r)/;
if(f[mid]>=x)
{
r=mid-;
}
else
l=mid+;
}
return l;
} int rfind(int x)//找小于等于x的最大值
{
int l=,r=now-,mid;
while(l<=r)
{
mid=(l+r)/;
if(f[mid]<=x)
l=mid+;
else
r=mid-;
}
return r; }
int main()
{
int n,x;
scanf("%d",&n);
int Max=;
now=;
memset(prime,false,sizeof(prime));
memset(c,,sizeof(c));
memset(f,,sizeof(f));
memset(s,,sizeof(s));
for(int i=;i<n;i++)
{
scanf("%d",&x);
if(x>Max)
Max=x;
c[x]++;
}
solve(Max);
int m,l,r;
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&l,&r);
int t1=lfind(l);
int t2=rfind(r);
// cout<<t1<<' '<<t2<<endl;
// cout<<f[t1]<<' '<<f[t2]<<endl;
if(t1==)
{
printf("%d\n",s[t2]);
}
else
printf("%d\n",s[t2]-s[t1-]);
} return ;
}

codeforces 385C Bear and Prime Numbers 预处理DP的更多相关文章

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

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  2. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  3. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

  4. CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)

    Recently, the bear started studying data structures and faced the following problem. You are given a ...

  5. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  6. CF385C Bear and Prime Numbers 数学

    题意翻译 给你一串数列a.对于一个质数p,定义函数f(p)=a数列中能被p整除的数的个数.给出m组询问l,r,询问[l,r]区间内所有素数p的f(p)之和. 题目描述 Recently, the be ...

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

    A - Bear and Prime 100 思路:任何一个合数都可以写成2个以上质数的乘积.在2-100中,除了4,9,25,49外都可以写成两个以上不同质数的乘积. 所以打一个质数加这四个数的表: ...

  8. 【Codeforces 385C】Bear and Prime Numbers

    [链接] 我是链接,点我呀:) [题意] f[i]表示在x[]中有多少个数字是i的倍数 让你求出sum(f[i]) li<=i<=ri 且i是质数 [题解] 做筛法求素数的时候顺便把素数i ...

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

    /* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...

随机推荐

  1. 关于word-break,word-wrap换行

    目前项目中有一些流程日志需要动态显示到页面上,实现方法是ajax动态获取附加到<span></span>标签上,然后设置word-break:break-all样式使其自动换行 ...

  2. swift 赋值判断

    . var name:String? = "Wing" var geting = "Hello!" if let thisname = name{ geting ...

  3. dp题目

    从别的地方看来,最近一直在啃DP,有个目标,更有动力了. 1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包; ...

  4. php 连接mysql数据库并显示数据 实例 转载

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. 简单学C——第五天

    结构体 首先明确,结构体是一种构造的数据类型,是一种由多个数据类型如 int,char,double,数组或者结构体......组成的类型,现在告诉大家如何定义一个结构体.在定义int整型变量时,大家 ...

  6. django中文件(头像)上传

    一些常用的小功能,记录下来 先说一种上传功能. 模板表单 <form enctype="multipart/form-data" name="form1" ...

  7. Newtonsoft.Json工具类

    这个类用于序列化和反序列化类. 效果是当前最好的.微软都推荐使用.在建立MVC的里面已经引用了这个dll. 上面一篇文章要用到 SerializeHelper工具类 public class Seri ...

  8. 完美方案——iOS的WebView自适应内容高度

    /////////////////////////////初始化,self.view是父控件///////////////////////////////// _webView = [[UIWebVi ...

  9. LightOj_1104 Birthday Paradox

    题目链接 题意: 若一年有n天, 问至少需要多少个人才能满足其中两个人生日相同的概率大于等于0.5? 思路: 经典问题:生日悖论 换成其互斥事件:m个人, 每个人生日都不相同的概率 ≤ 0.5 时最小 ...

  10. struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题

    <interceptor-ref name="fileUpload"> <param name="allowedTypes">image ...