题意:给你一个n,输入n个数,然后输入m,接下来有m个询问,每一个询问为[l,r],然后输出在区间内[l,r]内f(p)的和,p为[l,r]的素数,f(p)的含义为在n个数中是p的倍数的个数。

思路:先打出10000000内的素数,然后统计每一个素数在n个数中的倍数的个数记录在num[i]中,在每次询问的是找出l,r在素数表的位置,然后计算就可以。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 10000100
using namespace std; int n,m,cnt;
int x[maxn];
bool vis[maxn];
int vis1[maxn];
int f[maxn];
int l[maxn],r[maxn];
int num[maxn];
int sum[maxn]; void Getprime()
{
cnt=;
vis[]=vis[]=true;
memset(vis,false,sizeof(vis));
for(int i=; i<=maxn; i++)
{
if(!vis[i])
{
f[cnt++]=i;
for(int j=*i; j<=maxn; j+=i)
{
vis[j]=true;
}
}
}
} int main()
{
Getprime();
while(scanf("%d",&n)!=EOF)
{
memset(vis1,,sizeof(vis1));
int max1=;
for(int i=; i<n; i++)
{
scanf("%d",&x[i]);
vis1[x[i]]++;
max1=max(max1,x[i]);
}
for(int i=; i<cnt; i++)
{
for(int j=f[i]; j<=max1; j+=f[i])
{
if(vis1[j])
{
num[f[i]]+=vis1[j];
}
}
}
memset(sum,,sizeof(sum));
for(int i=; i<cnt; i++)
{
if(i==)
{
sum[i]=num[f[i]];
}
else
{
sum[i]=sum[i-]+num[f[i]];
} }
scanf("%d",&m);
for(int i=; i<=m; i++)
{
scanf("%d%d",&l[i],&r[i]);
if(l[i]>)
{
printf("0\n");
continue;
}
int ll=lower_bound(f,f+cnt,l[i])-f;
int rr=lower_bound(f,f+cnt,r[i])-f;
if(f[cnt-]<=r[i])
{
rr=cnt-;
}
if(f[ll]>r[i])
{
printf("0\n");
continue;
}
if(f[rr]>r[i])
{
rr=rr-;
}
if(l[i]==r[i])
{
printf("%d\n",num[l[i]]);
}
else
printf("%d\n",sum[rr]-sum[ll-]);
}
}
return ;
}

cf C Bear and Prime Numbers的更多相关文章

  1. Codeforces 385C Bear and Prime Numbers

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

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

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

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

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

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

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

  5. CF385C Bear and Prime Numbers 数学

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

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

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

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

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

  8. codeforces 385C Bear and Prime Numbers 预处理DP

    题目链接:http://codeforces.com/problemset/problem/385/C 题目大意:给定n个数与m个询问区间,问每个询问区间中的所有素数在这n个数中被能整除的次数之和 解 ...

  9. CF385C Bear and Prime Numbers

    思路: 需要对埃氏筛法的时间复杂度有正确的认识(O(nlog(log(n)))),我都以为肯定超时了,结果能过. 实现: #include <bits/stdc++.h> using na ...

随机推荐

  1. etrace 跟踪程序函数动态执行流程

    https://github.com/elcritch/etrace 窗口1: 监控窗口,执行监控程序,显示监控结果 [root@monitor example]# pwd /root/etrace- ...

  2. A different twist on pre-compiling JSPs--reference

    I’ve blogged about this topic earlier and expressed my frustrations as to how web containers don’t p ...

  3. iOS--iOS7摄像头识别二维码功能

    iOS–iOS7摄像头识别二维码功能 属性介绍: AVFoundation 框架基于以下几个类实现图像捕捉 ,通过这些类可以访问来自相机设备的原始数据并控制它的组件. AVCaptureDevice ...

  4. notification.setLatestEventInfo(context, title, message, pendingIntent); undefined

    notification.setLatestEventInfo(context, title, message, pendingIntent);    在target为23时删除了该方法,我们应该使用 ...

  5. Android(java)学习笔记202:Handler消息机制的原理和实现

     联合学习 Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系   1. 首先我们通过一个实例案例来引出一个异常: (1)布局文件activity_m ...

  6. Linux 目录与文件的基本操作

    1 目录与文件 1.1 文件 硬盘中的数据在操作系统中的体现为文件. 1.2 目录 目录的概念不是文件集合.目录和文件一样,目录也是文件.目录是找到文件的“踏板”.目录的本质是路径映射. 1.3 Li ...

  7. MVC中javascript直接调用Model

    最近做一个统计页面, Model从后台已经获取了数据集合,想直接在前台展示,而这个展示是需要用js生成图表的. 控制器部分代码: public ActionResult Index() { var m ...

  8. 导入sql时报日期类型错误

    导入的脚本中有的日期类型数据是:0000-00-00 00:..这种格式的. 需要把这种格式修改一下.有的mysql版本不支持这种0000.设置成当前时间即可

  9. mysql出现的错误

    (一)ERROR 1005 (HY000): Can't create table '.\day19\user_role.frm' (errno: 121) 今天遇到的这个问题是因为创建了五张表,其中 ...

  10. 文字排版--粗体(font-weight)

    我们还可以使用css样式来改变文字的样式:粗体.斜体.下划线.删除线,可以使用下面代码实现设置文字以粗体样式显示出来. p span{font-weight:bold;} 在这里大家可以看到,如果想为 ...