【Codeforces 385C】Bear and Prime Numbers
【链接】 我是链接,点我呀:)
【题意】
f[i]表示在x[]中有多少个数字是i的倍数
让你求出sum(f[i]) li
【题解】
做筛法求素数的时候顺便把素数i在x[]中的倍数的个数求出来就好
前缀和 输出即可
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)1e7;
static class Task{
public void solve(InputReader in,PrintWriter out) {
int n;
int []cnt = new int [N+10];
int []shai = new int [N+10];
long []f = new long [N+10];
n = in.nextInt();
for (int i = 1;i <= n;i++) {
int x;
x = in.nextInt();
cnt[x]++;
}
for (int i = 2;i <= N;i++)
if (shai[i]==0) {
f[i] = cnt[i];
for (int j = 2*i;j <= N;j+=i) {
f[i] = f[i] + cnt[j];
shai[j] = 1;
}
}
for (int i = 1;i <= N;i++)
f[i]=f[i-1]+f[i];
n = in.nextInt();
for (int i = 1;i <= n;i++) {
int l,r;
l = in.nextInt();r = in.nextInt();
l = Math.min(l, N);r = Math.min(r,N);
out.println(f[r]-f[l-1]);
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 385C】Bear and Prime Numbers的更多相关文章
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 【codeforces 791C】Bear and Different Names
[题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- 【codeforces 791A】Bear and Big Brother
[题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...
- 【19.05%】【codeforces 680D】Bear and Tower of Cubes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- 【Codeforces 639A】Bear and Displayed Friends
[链接] 我是链接,点我呀:) [题意] [题解] 时刻维护一下前K大的数字就好. 因为k<=6 然后数字不会减少只会增加. 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的. 写个 ...
随机推荐
- tiny4412 裸机程序 八、重定位到DRAM及LCD实验【转】
本文转载自:http://blog.csdn.net/eshing/article/details/37407423 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一 ...
- FormatString格式大众人全
FormatString格式大众人全 Posted on 2010-08-12 16:14 moss_tan_jun 阅读(457) 评论(0) 编辑 收藏 格式化日期和数字的字符串经常要用到这个, ...
- 获取Access数据里所有表的名称和表的字段
-------------//获取Access数据库表名 public void GetTableName() { string connSt ...
- 简单动态规划——最长公共子序列&&最长回文子序列&&最长上升||下降子序列
最长公共子序列,顾名思义当然是求两个字符串的最长公共子序列啦,当然,这只是一道非常菜的动规,所以直接附上代码: #include<iostream> #include<cstdio& ...
- 【高德地图API】注册密钥
能正常使用高德地图API所有功能的前提是添加了密钥.高德API官网地址:http://api.amap.com/ 注册步骤:1.注册账号 2.填写开发者信息 3.注册密钥 1.注册账号(略过) 2.填 ...
- 接口管理功能全面增强!EOLINKER EPC 5.0.9版本更新:支持LDAP用户系统、加入更多项目统计图表、强化测试/自动化测试功能等
EOLINKER EPC(Enterprise Private Cloud 企业私有云产品)已于近期发布5.0.9版本:界面全面改版.支持LDAP用户系统.加入更多项目统计图表.强化测试/自动化测试功 ...
- Tempter of the Bone------剪枝
看了好多别人的 代码,最终还是 感觉 这种代码的风格适合我 下面附上代码 /* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 , 如果是 答题思路错误的话 借鉴别人的 代码 再写 */ ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- vs项目结构解析
当我们用VS开发一个项目的时候,首先应该清楚用VS这个IDE生成的一些文件和文件夹是什么意思,起什么作用,什么场合下使用. 因为我使用的是VS2015,就以这个为例来进行一些说明: 首先要做的是更改你 ...
- [ Nowcoder Contest 165 #D ] 合法括号序列
\(\\\) \(Description\) 键盘上有三个键,敲击效果分别是: 在输出序列尾部添加一个左括号 在输出序列尾部添加一个右括号 删除输出序列尾部的第一个元素,若输出序列为空,则什么都不发生 ...