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

C. Bear and Prime Numbers
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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

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

Help the bear cope with the problem.

Input

The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct.

The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.

Output

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

Sample test(s)
input
6
5 5 7 10 14 15
3
2 11
3 12
4 4
output
9
7
0
input
7
2 3 5 7 11 4 8
2
8 10
2 123
output
0
7
Note

Consider the first sample. Overall, the first sample has 3 queries.

  1. The first query l = 2r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
  2. The second query comes l = 3r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
  3. The third query comes l = 4r = 4. As this interval has no prime numbers, then the sum equals 0.

英语跟不上就是不行啊。。。
红体字读了n多遍一直没理解什么意思,最后醍醐灌顶、大彻大悟
题目的意思是给出n
给出x1...xn
再给出m
给出m行l[i],r[i]
对应这m行,每行输出l[i]到r[i](包括l[i],r[i])所有质数的f()函数对应值
之前一直不理解f()函数是怎么计算的
后来想通了
例如案例1中的f(2)=2
是因为x[]数组中有两个数可以被2整除

虽然思路没错,但实现起来就比较恶心了,要注意到l[i],r[i]的取值范围有多大。。。
我刚开始写了两个函数,一个是用来判断是否为质数,另一个就是f()函数
交上去第四个案例超时
又改进了一下,使得不用专门判断是否为质数,而是在f()函数中判断若不为质数让其对应f()为0即可
交上去还是第四个超时
又改进了一下,先把每个f()值保存在数组中,免得每次在循环中都要多次调用函数
交上去还是跪了。。。。

先把代码贴上吧
明天再改正:
#include <iostream>
#include <string>
#include <vector>
#define rep(i,j,k) for(int i=(j); i<k; i++)
#define maxn 100100
#define maxm 50010
#define max 20000000
int a[maxn]; 
int l[maxm],r[maxm];
long long int res[maxm];
long long b[max];
using namespace std;

int n,m;

int f(int t){
    int times = 0;
    if(a[1]%t == 0)
     times++;
    for(int i=2; i<=n; ++i){
     if(t>i && t%i==0)
      return 0;
     if(a[i]%t == 0)
      times++;
    }
    return times;
}

int main(void){
    while(cin >> n){
     rep(i,1,n+1)
      cin >> a[i];
     
     cin >> m;
     rep(i,0,m)
      cin >> l[i] >> r[i];
     for(int i=2; i<max; ++i)
      b[i]=f(i);
     for(int i=0; i<m; i++){
      for(int j=l[i]; j<=r[i]; ++j)
        res[i] += b[j];
      cout << res[i] << endl;
     }
    }
    return 0;
}


贴上正确代码以及我的理解:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int kMaxX = 10000000, kMaxP = 664579;
int n, pr[kMaxP + 1];
bool not_pr[kMaxX + 1];//非素数 
int sum[kMaxX + 1];

void init(){
    for(int i=2; i<=kMaxX; ++i){
     if(!not_pr[i]) pr[++pr[0]] = i;
     //找出所有的素数,存入pr数组中,pr[0]表示当前素数个数 
     for(int j=1,k; j<=pr[0] && (k=pr[j]*i)<=kMaxX; ++ j){
      not_pr[k] = true;
      //每个素数的倍数都是非素数,这样可以省去很多判断的时间 
      if(i%pr[j]==0) break;
     }
    }
}

int main(void){
    init();
    scanf("%d", &n);
    for(int x; n--; ){
     scanf("%d", &x);
     for(int i=1; pr[i]*pr[i]<= x; ++i)//用pr[i]*pr[i]<=n降低时间复杂度 
      if(x%pr[i]==0){
       ++sum[pr[i]];
       while(x%pr[i]==0) 
        x /= pr[i];
        //意在把x变小 
      }
     if(x != 1) 
     //例如x刚开始等于14,在上面的for循环中
     //只能找到14%2==0,不能找到14%7==0,使用while循环加判断则可以做到 
      ++sum[x];
    }
    for(int i=3; i<=kMaxX; ++ i) 
     sum[i]+=sum[i-1];
    //sum[i]对应r[i]的f()函数加权和 
    scanf("%d", &n);
    for(int l,r; n--; ){
     scanf("%d%d", &l, &r);
     l = min(l, kMaxX);//为什么??? 
     r = min(r, kMaxX);//因为对x[]数组,最大只有10,000,000
     //当l r大于kMaxX时不可能存在x满足条件 
     printf("%d\n", sum[r]-sum[l-1]);
    }
    return 0;
}


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值