codeforces 385C Bear and Prime Numbers 预处理DP
题目链接: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的更多相关文章
- 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 A - Bear and Prime 100(交互题)
A - Bear and Prime 100 思路:任何一个合数都可以写成2个以上质数的乘积.在2-100中,除了4,9,25,49外都可以写成两个以上不同质数的乘积. 所以打一个质数加这四个数的表: ...
- 【Codeforces 385C】Bear and Prime Numbers
[链接] 我是链接,点我呀:) [题意] f[i]表示在x[]中有多少个数字是i的倍数 让你求出sum(f[i]) li<=i<=ri 且i是质数 [题解] 做筛法求素数的时候顺便把素数i ...
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
/* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...
随机推荐
- Object To Enum
public static T ObjectToEnum<T>(object o) { try { return (T)Enum.Parse(typeof(T), o.ToString() ...
- 枚举N行N列的自然数列
数据库环境:SQL SERVER 2005 现有一个需求,要枚举1-50个自然数,分10行5列展示.如图,
- LINQ 101——分区、Join、聚合
一.Partitioning 分区 Take 例1:取前3个数 static void Linq1() { , , , , , , , , , }; ); Console.WriteLine(&quo ...
- ZOJ 1057 Undercut(简单模拟)
Undercut 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=57 题目大意:a card game,two pla ...
- VMware虚拟机中如何安装VMWare-Tools详解
VMware虚拟机中如何安装VMWare-Tools详解 好处:可以支持图形界面,可以支持共享文件功能等 VMware虚拟机中如何配置显 VMware作为一款虚拟机利器,很多人都利用它来实现Linux ...
- angularjs应用骨架(4)
继续上一篇 继续了解angular其他内容. 与服务器交互 真正的应用需要和真实的服务器进行交互移动应用和新兴的Chrome桌面应用可能是例外.但是对于此外的所有应用来说,无论是想把数据持久化到云端还 ...
- 基于管道通知的百万并发长连接server模型
0.前言 最近突然想了解怎样设计一个支持百万连接的后台server架构. 要设计一个支持百万连接的后台server,我们首先要知道会有哪些因素限制后台server的高并发连接,这里想到的因素有以下几点 ...
- Fedora 21 设置开机启动脚本
sudo touch /etc/rc.d/rc.localsudo vim /etc/rc.d/rc.local 在/etc/rc.d/rc.local文件中写入, 然后使用:wq命令 保存并退出. ...
- Android2.2 API —— ImageView
注意 请查看本文后期更新完整版: http://www.cnblogs.com/over140/archive/2011/06/08/2075054.html 来源: 农民伯伯: http://www ...
- 基于page的简单页面推送技术
我们可以先看下简单效果,打开2个页面可以看到推送效果 服务端我们只需要下面一个方法 using System; using System.Collections.Generic; using Syst ...