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;
}


内容概要:本研究聚焦于绿电直连型电氢氨园区的优化运行,提出一种集成绿色电力直接供给、电解水制氢及氢气合成氨工艺的综合能源系统架构。通过建立包含风光发电、电解槽、氨合成反应器、储氢罐、电网交互及多类型负荷在内的系统模型,综合考虑绿电直供优先、能量梯级利用与多能互补原则,构建以系统综合运行成本最小化为目标的优化调度模型。研究采用Matlab与Python工具进行算法求解和仿真分析,利用实际气象与负荷数据完成案例验证,评估了不同运行策略下系统的经济性、可再生能源消纳能力与碳减排效益,为新型电氢氨一体化园区的规划与运行提供了理论依据和技术支撑。; 适合人群:具备一定电力系统、新能源或化工背景的研究生、科研人员及从事综合能源系统规划与优化工作的工程技术人员。; 使用场景及目标:①用于科研学习,理解电-氢-氨多能转换系统的建模与优化方法;②为工业园区的低碳化、智能化改造提供技术参考与决策支持;③作为开发类似综合能源管理系统的理论基础。; 阅读建议:此资源包含完整的模型代码、数据与论文,使用者应结合代码仔细研读论文中的模型构建部分,重点关注目标函数与约束条件的设计逻辑,并尝试修改参数进行仿真,以深入掌握优化算法在实际系统中的应用。
内容概要:本文深入探讨了RS485通信协议在芯片行业自动化测试系统中的实际开发与应用,涵盖其关键概念、电气特性、通信机制及与Modbus RTU协议的结合使用。文章重点介绍了差分信号完整性设计、主从时序控制、CRC校验与重传机制等核心技术要点,并通过一个基于Python的完整代码实例,展示了如何实现RS485主站对探针台、自动分选机等芯片测试设备的控制与数据采集。此外,还分析了RS485在晶圆探针台、ATE设备集群和环境监控等典型场景的应用,并展望了其与工业以太网融合、智能化诊断、高速化及AI集成的发展趋势。; 适合人群:具备一定嵌入式系统或工业通信基础,从事芯片测试、自动化设备开发及相关领域的研发人员,尤其是工作1-3年希望提升现场总线应用能力的工程师。; 使用场景及目标:①理解RS485在高干扰芯片测试环境中稳定通信的设计原理;②掌握Modbus RTU协议在Python下的实现方法,用于实际控制探针台、Handler等设备;③构建可靠的数据采集与设备控制系统,支持CRC校验、异常处理和日志追踪;④为后续向高速通信和智能诊断系统升级提供技术储备。; 阅读建议:此资源强调实战开发,建议结合硬件环境动手调试代码,重点关注线程锁、CRC计算、帧解析和超时控制等关键环节,在真实产线中验证通信稳定性,并利用日志系统进行故障分析与优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值