题意翻译

给你一串数列a.对于一个质数p,定义函数f(p)=a数列中能被p整除的数的个数.给出m组询问l,r,询问[l,r]区间内所有素数p的f(p)之和.

题目描述

Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1,x2,...,xn x_{1},x_{2},...,x_{n} x1​,x2​,...,xn​ of length n n n and m m m queries, each of them is characterized by two integers li,ri l_{i},r_{i} li​,ri​ . Let's introduce f(p) f(p) f(p) to represent the number of such indexes k k k , that xk x_{k} xk​ is divisible by p p p . The answer to the query li,ri l_{i},r_{i} li​,ri​ is the sum: , where S(li,ri) S(l_{i},r_{i}) S(li​,ri​) is a set of prime numbers from segment [li,ri] [l_{i},r_{i}] [li​,ri​] (both borders are included in the segment).

Help the bear cope with the problem.

输入输出格式

输入格式:

The first line contains integer n n n (1<=n<=106) (1<=n<=10^{6}) (1<=n<=106) . The second line contains n n n integers x1,x2,...,xn x_{1},x_{2},...,x_{n} x1​,x2​,...,xn​ (2<=xi<=107) (2<=x_{i}<=10^{7}) (2<=xi​<=107) . The numbers are not necessarily distinct.

The third line contains integer m m m (1<=m<=50000) (1<=m<=50000) (1<=m<=50000) . Each of the following m m m lines contains a pair of space-separated integers, li l_{i} li​ and ri r_{i} ri​ (2<=li<=ri<=2⋅109) (2<=l_{i}<=r_{i}<=2·10^{9}) (2<=li​<=ri​<=2⋅109) — the numbers that characterize the current query.

输出格式:

Print m m m integers — the answers to the queries on the order the queries appear in the input.

输入输出样例

输入样例#1:
复制

6
5 5 7 10 14 15
3
2 11
3 12
4 4
输出样例#1: 复制

9
7
0
输入样例#2: 复制

7
2 3 5 7 11 4 8
2
8 10
2 123
输出样例#2: 复制

0
7 首先可以线性筛筛出 1e7 里面所有的素数;
对于每一个x[i] ,我们记录其次数,然后类似于埃筛的做法,对于每个素数,用 sum [ i ] 来累计有该素数因子的数的个数;
最后用前缀和维护;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 10000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n;
int x[maxn];
int m;
int cnt[maxn];
int prime[maxn];
int tot = 0;
int sum[maxn];
bool vis[maxn]; void init() {
vis[1] = 1;
for (int i = 2; i < maxn; i++) {
if (!vis[i])prime[++tot] = i;
for (int j = 1; prime[j] * i < maxn; j++) {
vis[prime[j] * i] = 1;
if (i%prime[j] == 0)break;
}
}
} int main()
{
//ios::sync_with_stdio(0);
rdint(n);
for (int i = 1; i <= n; i++)rdint(x[i]), cnt[x[i]]++;
init();
for (int i = 1; i <= tot; i++) {
for (int j = 1; j*prime[i] < maxn; j++) {
sum[i] += cnt[j*prime[i]];
}
}
for (int i = 1; i <= tot; i++)sum[i] += sum[i - 1];
rdint(m);
while (m--) {
int l, r; rdint(l); rdint(r);
int pos1 = upper_bound(prime + 1, prime + 1 + tot, r) - prime - 1;
int pos2 = lower_bound(prime + 1, prime + 1 + tot, l) - prime - 1;
// cout << pos1 << ' ' << pos2 << endl;
cout << sum[pos1] - sum[pos2] << endl;
}
return 0;
}

CF385C Bear and Prime Numbers 数学的更多相关文章

  1. CF385C Bear and Prime Numbers

    思路: 需要对埃氏筛法的时间复杂度有正确的认识(O(nlog(log(n)))),我都以为肯定超时了,结果能过. 实现: #include <bits/stdc++.h> using na ...

  2. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

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

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

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

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

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

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

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

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

  7. 680C. Bear and Prime 100 数学

    C. Bear and Prime 100 time limit per test:1 second memory limit per test:256 megabytes input:standar ...

  8. codeforces 356 div2 C.Bear and Prime 100 数学

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers

    /* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...

随机推荐

  1. Celery-4.1 用户指南: Configuration and defaults (配置和默认值)

    这篇文档描述了可用的配置选项. 如果你使用默认的加载器,你必须创建 celeryconfig.py 模块并且保证它在python路径中. 配置文件示例 以下是配置示例,你可以从这个开始.它包括运行一个 ...

  2. Python类(七)-类的特殊成员方法

    __doc__ 用来表示类的描述信息 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(object) ...

  3. vb和dos批处理创建或生成快捷方式

    https://www.cnblogs.com/gszhl/archive/2009/04/23/1441753.html vb和dos批处理创建或生成快捷方式   首先说我现在用的一种,最有效的也是 ...

  4. C#连接MSSQL

    本文将介绍如何用C#连接MSSQL,C#连接SQL十分简单.我们一步一步来操作. 1.打开Microsoft SQL Server Management Studio创建一个数据库,这里我创建一个数据 ...

  5. latex 小模板

    \documentclass[11pt,a4paper,english]{article}\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}\use ...

  6. 环境变量,include搜索路径,lib库搜索路径

    环境变量 系统环境变量 我们知道,我们经常要设置一些环境变量,系统环境变量我们非常容易理解.其实我们在windows中经常容易接触.其实环境变量是一个非常广泛的一个概念,它与web应用程序中的web. ...

  7. JAVA中的垃圾回收机制以及其在android开发中的作用

    http://blog.csdn.net/xieqibao/article/details/6707519 这篇文章概述了JAVA中运行时数据的结构,以及垃圾回收机制的作用.在后半部分,描述了如何检测 ...

  8. 如何使用google等一系列搜索引擎?

    对于我们经常使用的搜索引擎大家都都不陌生,但是,如何高效的利用呢?大家都知道空格是搜索多个关键词,那么有没有其他的快捷键呢?答案是肯定的,以下内容转自知乎 1.双引号 把搜索词放在双引号中,代表完全匹 ...

  9. idea 修改Recent projects

    idea用了一段时间了,打开的项目多了,导致Open Recent列表中的项目也非常多,在找一个项目时很不方便. 后来查询,在~/Library/Preferences/IntelliJIdea目录/ ...

  10. [转载]HTTP的无状态是什么意思?

    文章地址:https://www.cnblogs.com/bellkosmos/p/5237146.html#commentform 作者:赛艇队长 引子: 最近在好好了解http,发现对介绍http ...