Codeforces 385C Bear and Prime Numbers(数论)
2014-01-30 12:30
369 查看
题目链接:Codeforces 385C Bear and Prime Numbers
题目大意:给出一个长度为n的序列,然后有m次询问,每次询问给出a, b,然后计算[a,b]中所有素数的F(x)之和,F(x)为计算序列中有几个数为x的倍数。
解题思路:数论题,因为内存空间限制为512M,所以可以开的下10^7的数组,然后用筛选法求素数的同时计算个数。
#include <stdio.h>
#include <string.h>
const int N = 10000005;
int n, m, v
, g
, s
;
void init() {
memset(v, 0, sizeof(v));
memset(g, 0, sizeof(g));
memset(s, 0, sizeof(s));
int a;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
g[a]++;
}
for (int i = 2; i < N; i++) {
if (v[i]) continue;
for (int j = i; j < N; j += i) {
if (g[j]) s[i] += g[j];
v[j] = 1;
}
}
for (int i = 1; i < N; i++) s[i] += s[i-1];
}
void solve() {
int a, b;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
if (a >= N) a = N;
if (b >= N) b = N - 1;
printf("%d\n", s[b] - s[a-1]);
}
}
int main() {
init();
solve();
return 0;
}
题目大意:给出一个长度为n的序列,然后有m次询问,每次询问给出a, b,然后计算[a,b]中所有素数的F(x)之和,F(x)为计算序列中有几个数为x的倍数。
解题思路:数论题,因为内存空间限制为512M,所以可以开的下10^7的数组,然后用筛选法求素数的同时计算个数。
#include <stdio.h>
#include <string.h>
const int N = 10000005;
int n, m, v
, g
, s
;
void init() {
memset(v, 0, sizeof(v));
memset(g, 0, sizeof(g));
memset(s, 0, sizeof(s));
int a;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
g[a]++;
}
for (int i = 2; i < N; i++) {
if (v[i]) continue;
for (int j = i; j < N; j += i) {
if (g[j]) s[i] += g[j];
v[j] = 1;
}
}
for (int i = 1; i < N; i++) s[i] += s[i-1];
}
void solve() {
int a, b;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
if (a >= N) a = N;
if (b >= N) b = N - 1;
printf("%d\n", s[b] - s[a-1]);
}
}
int main() {
init();
solve();
return 0;
}
相关文章推荐
- Codeforces 385C Bear and Prime Numbers(数论)
- 【数论】CF 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers【巧妙用埃筛】
- 【数论】CF 385C Bear and Prime Numbers
- codeforces 385C Bear and Prime Numbers 预处理DP
- CodeForces 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers [素数筛法]
- Codeforces 385C Bear and Prime Numbers 筛法
- Codeforces 385C Bear and Prime Numbers
- CodeForces - 385C Bear and Prime Numbers——素数筛法
- Codeforces 385C Bear and Prime Numbers(素数打表,二分)
- CodeForces 385C Bear and Prime Numbers 素数打表
- codeforces-385C Bear and Prime Numbers(素数筛选法+维护前缀和)
- CodeForces 385C Bear and Prime Numbers
- codeforces 385C C. Bear and Prime Numbers(线性筛素数)
- CF:Problem 385C - Bear and Prime Numbers 预处理DP
- Codeforces 385 C Bear and Prime Numbers
- Codeforces 385 C. Bear and Prime Numbers
- codeforces 680C C. Bear and Prime 100(数论)
- codeforces 680C Bear and Prime 100(数论)