【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. 保证这个数组是有序的. 写个 ...
随机推荐
- bzoj1143: [CTSC2008]祭祀river && bzoj27182718: [Violet 4]毕业旅行
其实我至今不懂为啥强联通缩点判入度会错... 然后这个求的和之前那道组合数学一样,就是最长反链=最小链覆盖=最大独立集. #include<cstdio> #include<iost ...
- Ip获取请求ip
public class IPAdress { public static bool isIP(string str1) { || str1.Length > ) return false; s ...
- TeeChart绘图控件 - 之三 - 提高绘图的效率 .
TeeChart是个很强大的控件,其绘图能力之强,其他控件难以比拟,但是有个问题就是他的绘图速度,其实TeeChart绘图速度还是很快的,只是大家一直都没正确运用其功能所以导致绘图速度慢的假象. 下面 ...
- ADTS结构
ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式.转载请注明来自:http://www.binkery.com/ ADTS内容及结构一般情况下 ...
- ARM VM安装Linux Diagnostic 2.3扩展
目前创建的Azure Linux虚拟机默认安装的是LAD 3.0,如果客户有特殊需求,可以通过如下方法安装LAD 2.3 1.在Azure Portal卸载LAD 3.0 2.使用Azure Powe ...
- JavaScript--String 字符串对象属性
访问字符串对象的属性length: stringObject.length; 返回该字符串的长度. var mystr="Hello World!"; var myl=mystr. ...
- 【BZOJ2149】拆迁队(斜率优化DP+CDQ分治)
题目: 一个斜率优化+CDQ好题 BZOJ2149 分析: 先吐槽一下题意:保留房子反而要给赔偿金是什么鬼哦-- 第一问是一个经典问题.直接求原序列的最长上升子序列是错误的.比如\(\{1,2,2,3 ...
- ACM_小Z的A+B
小Z的A+B Time Limit: 2000/1000ms (Java/Others) Problem Description: 小Z最喜欢A+B了,没事就研究研究,比如什么大整数A+B(就是100 ...
- .Net application,Session,Cache简单比较
Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对 ...
- UNIX环境高级编程--8. 进程控制
进程控制进程标识: 每一个进程都有一个非负整型表示的唯一进程ID.虽然唯一,但是ID可以复用.当一个进程结束后,其进程ID会被延迟复用. ID=0的进程通常是调度进程,常被称作交换进程(s ...