【链接】 我是链接,点我呀:)

【题意】

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的更多相关文章

  1. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【32.89%】【codeforces 574D】Bear and Blocks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  4. 【codeforces 791C】Bear and Different Names

    [题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...

  5. 【codeforces 791B】Bear and Friendship Condition

    [题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...

  6. 【codeforces 791A】Bear and Big Brother

    [题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...

  7. 【19.05%】【codeforces 680D】Bear and Tower of Cubes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【Codeforces 639B】Bear and Forgotten Tree 3

    [链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...

  9. 【Codeforces 639A】Bear and Displayed Friends

    [链接] 我是链接,点我呀:) [题意] [题解] 时刻维护一下前K大的数字就好. 因为k<=6 然后数字不会减少只会增加. 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的. 写个 ...

随机推荐

  1. [Codeplus 2017] 晨跑

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5105 [算法] 答案为三个数的最小公倍数 [代码] #include<bits ...

  2. Scala 方法接受变参

    def Parametron(strings:String*): Unit ={ strings.foreach(x=>{ println(x) ") println(s"* ...

  3. 在网页上打印,js window.print

    window.print默认会打印出当前页在屏幕中显示的部分,可以实现在线打印

  4. Unity 代码改宏定义

    两个函数 PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup); //所有宏定义 ; 分割 PlayerSettings.SetS ...

  5. codevs3342绿色通道(单调队列优化dp)

    3342 绿色通道  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold   题目描述 Description <思远高考绿色通道>(Green Pass ...

  6. Java 中数组的遍历方式

    数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 语言中提供的数组是用来存储固定大小的同类型元素. 今天我们就来说一下在java中遍历数组都有哪几 ...

  7. distpicker三级联动,动态改变省市信息

    一.引入3个js文件 <script type="text/javascript" src="js/distpicker.data.js">< ...

  8. JavaScript--引用JS外部文件

    通过前面知识学习,我们知道使用<script>标签在HTML文件中添加JavaScript代码,如图: JavaScript代码只能写在HTML文件中吗?当然不是,我们可以把HTML文件和 ...

  9. html表单代码演示

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  10. 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化

    序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #.     _9_    /   \   3     2  ...