题目链接:Codeforces 385C Bear and Prime Numbers

这题告诉我仅仅有询问没有更新通常是不用线段树的。或者说还有比线段树更简单的方法。

用一个sum数组记录前n项和,这个sum数组在打素数表时候就能够求出来,注意一点求素数的内层循环要改成i。不能再写成i + i或者i * i了。原因想想就明确了。

这学期最后一场比赛也结束了,结果不非常惬意但也还好。

总的来说这学收获还是蛮多的。

近期可能就不再做ACM了吧,可能要复习CET6了吧,可能要复习期末考试的内容了吧。可能要考研了吧。。

#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int MAX_N = 1000000 * 10 + 1000; bool primes[MAX_N];
long long sum[MAX_N];
int arr[MAX_N], cnt[MAX_N]; //primes[i] = 0表示i是素数,为1表示i不是素数
void get_primes()
{
memset(primes,0,sizeof primes);
primes[0]=primes[1]=1;
for(int i=2;i<MAX_N;i++)
{
sum[i] = sum[i - 1];
if(!primes[i])
{
for(int j=i;j<MAX_N;j+=i)
{
sum[i] += cnt[j];
primes[j]=1;
}
}
}
} int main()
{
int n, _max = 0;
scanf("%d", &n);
memset(sum, 0, sizeof(sum));
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= n; i++)
{
scanf("%d", &arr[i]);
cnt[arr[i]]++;
_max = max(_max, arr[i]);
}
get_primes();
int q, a, b;
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
scanf("%d%d", &a, &b);
if(a > _max && b > _max)
puts("0");
else
{
if(a > _max)
a = _max;
if(b > _max)
b = _max;
printf("%I64d\n", sum[b] - sum[a - 1]);
}
}
return 0;
}

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

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

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

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

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

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

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

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

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

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

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

  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. eclipse - copy类的全名

    由于多次操作,感觉eclipse应该提供这个功能,网上搜一下,发现需要安装插件. 下载地址为 http://www.jave.de/eclipse/copyfully/copyfully_1.2.0. ...

  2. 用POP动画引擎实现衰减动画(POPDecayAnimation)

    效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @end @i ...

  3. TreeView 数据绑定及选中命令处理

    昨天接近下班,一个群里面的网友,问treeView绑定后  选中命令怎么来处理,怎么没有效果,而且用MVVM的方式来写:快下班了本来想远程帮他看下,结果就说写个Demo给他看:再加上选中传参: 下面分 ...

  4. JQuery 解析xml

    JQuery 可以通过 $.get() 或 $.post() 方法来加载 xml.     JQuery 解析 XML 与解析 DOM 一样, 可以使用 find(), children() 等函数来 ...

  5. 托盘图标、气泡以及任务栏崩溃后的自动添加——Shell_NotifyIcon

    托盘图标使用函数 Shell_NotifyIcon 创建.修改和删除,参数主要使用 NOTIFYICONDATA 结构. 任务栏启动时会给所有顶层窗口发送 TaskbarCreated 消息,由于不同 ...

  6. php判断是移动端还是pc

    //判断是否是手机 function is_mobile() { $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_pc = (strpos( ...

  7. linux 添加 $path

    # vim /etc/profile在文档最后,添加:export PATH="/usr/local/src/bin:$PATH"保存,退出,然后运行:#source /etc/p ...

  8. jquery升级到新版本报错[jQuery] Cannot read property ‘msie’ of undefined错误的解决方法(转)

    最近把一个项目的jQuery升级到最新版,发现有些页面报错Cannot read property 'msie' of undefined.上jQuery网站上搜了一下,原因是$.browser这个a ...

  9. [LeetCode 114] - 将树平面化为链表(Flatten Binary Tree to Linked List)

    问题 给出一个二叉树,将其原地平面化为链表. 例如,给出: 1   /  \  2    5 / \     \ 3  4     6 平面化后的树看起来应该是这样: 1 \  2    \      ...

  10. QT使用BC技术(网页与桌面结合)开发程序,好多相关链接(寒山居士)

    http://blog.csdn.net/Esonpo/article/category/1366376