In many programming competitions, we are asked to find (or count the number of) Prime Factors of an integer i. This is boring. This time, let’s count the number of Non-Prime Factors of an integer i, denoted as NPF(i).

For example, integer 100 has the following nine factors: {1,2,4,5,10,20,25,50,100}. The two which are underlined are prime factors of 100 and the rest are non-prime factors. Therefore, NPF(100) = 7.

Input

The first line contains an integer Q (1≤Q≤3⋅10^6) denoting the number of queries. Each of the next Q lines contains one integer i (2≤i≤2⋅10^6).

Output

For each query i, print the value of NPF(i).

Warning

The I/O files are large. Please use fast I/O methods.

Sample Input 1 Sample Output 1
4
100
13
12
2018
7
1
4
2

题目大意:第一行给一个Q,代表Q次查询,接下来Q行,每行一个整数i,求NPF(i)

    拿样例100来说,100的因子有(1,2,4,5,10,20,25,50,100)共9个,其中2和5是质数(一个大于1的自然数,除了1和它本身外,不能被其他自然数整除),应去掉,剩下7个。

    所以NPF(100)= 7

    拿样例13来说,13的因子有(1,13)共2个,其中13是质数,去掉后,剩下1个。

    所以NPF(13)= 1

解题思路:1.先预处理出1-2*10^6的质数。可以用eratosthenes筛法,时间复杂度O(NloglogN)(某位网友说的)

     2.预处理答案,先看代码:

for(int i = ; i <= ; ++i){
int rt = /i;
for(int j = i; j <= rt; ++j){
if(vis[i]){//非质数
++ans[i*j];
}
if(vis[j] && i!=j){
++ans[i*j];
}
}
}

  第一个for循环,表示1到2*10^6的数。

  第二个for循环,对于当前的数i,对 i 到 i*rt 进行处理

  举个栗子,对于100来说,ans【100】初始化是0

  第一个循环到1时

    在第二个循环中:判断1是非质数第一个if中必然会有1*100=100,即ans【100】++;(100<rt,必然出现)

            第二个if中会出现j=100,100是非质数,又100*1=100,即ans【100】++;

  tip:当i=100时,j 从100开始累加而且 j 不会回溯,所以100=1*100这一种分解方法会在i=1的时候处理出来

    即ans【100】+=2;

  第一个循环到2时

    在第二个循环中:第一个if  判断2是质数,跳过(相当于把2这个因子剔除了,即没有加入答案中)

            第二个if  j=50时,50是非质数,又50*2=100,所以ans【100】++;

  第一个循环到4时

    在第二个循环中:第一个if  判断4是非质数,4*25=100,ans【100】++;

            第二个if  j=25时,25是非质数,25*4=100,所以ans【100】++;

  第一个循环到5时

    在第二个循环中:第一个if  判断5是质数,跳过,

            第二个if  j=20时,20是非质数,20*5=100,所以ans【100】++;

  第一个循环到10时

    在第二个循环中:第一个if  判断10是非质数,10*10=100,ans【100】++;

            第二个if  j=10时,10是非质数,但是i=j,跳过(相同因子处理一次即可,在第一个if处理了)

  第一个循环到20时:20*5=100,但是5不会出现,因为j是从20开始不断累加,ans【100】已经处理结束了,从上面分析可以看出ans【100】=7;

  类似的,每个答案都可以在这2个循环中处理出来。

  时间上:当i=1来说,rt=2*10^6,j会从1加到2*10^6

      当i=2,rt=10^6,   j会从2加到10^6;

      ......  

      当i=10,rt=2*10^5,j会从10加到2*10^5(此时数量级已经降了一级)

      ...

      当i=100,rt=2*10^4,j会从1加到2*10^4

      ....

      当i=1000,rt=2*10^3,j会从1000加到2000(共1000次)

      .....

      当i=sqrt(2*10^6) rt=i,第二层循环直接跳过,后面一样,都是1次

把它们加起来,大概也就10^7左右(目测估计法算的)

预处理10^6左右,Q个问题3*10^6,加起来数量级也在10^7

某位大佬说10^7的数量级一般都能在1s跑完,要看测评机

一开始我是对每次枚举每个数的因数(1-sqrt(n)),然后想办法优化,结果都是超时....((╯‵□′)╯︵┻━┻)

启示:优化的时候想想办法让回溯的次数少一点。

AC代码:

#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
bool vis[];
int ans[];
void init()
{
//开始筛,vis=1表示该数不是质数
vis[]=;
int m=sqrt(+0.5);
for(int i=;i<=m;++i)
if(!vis[i]) for(int j=i*i;j<=;j+=i) vis[j]=;
//筛选结束
for(int i = ; i <= ; ++i){
int rt = /i;
for(int j = i; j <= rt; ++j){
if(vis[i]){//非质数
++ans[i*j];
}
if(vis[j] && i!=j){
++ans[i*j];
}
}
}
}
int main()
{
init();
int Q;
scanf("%d",&Q);
while(Q--)
{
int n;
scanf("%d",&n);
printf("%d\n",ans[n]);
}
return ;
}

L - Non-Prime Factors (质数筛选+因子分解)的更多相关文章

  1. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  2. POJ2689 Prime Distance 质数筛选

    题目大意 求区间[L, R]中距离最大和最小的两对相邻质数.R<2^31, R-L<1e6. 总体思路 本题数据很大.求sqrt(R)的所有质数,用这些质数乘以j, j+1, j+2... ...

  3. [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

    7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...

  4. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  5. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  6. 2014辽宁ACM省赛 Prime Factors

    问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...

  7. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  8. PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]

    题目 Given any positive integer N, you are supposed to find all of its prime factors, and write them i ...

  9. PAT-1059 Prime Factors (素数因子)

    1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...

随机推荐

  1. 常见的四种文本自动分词详解及IK Analyze的代码实现

    以下解释来源于网络-百度百科 1.word分词器 word分词 [1]  是一个Java实现的分布式的中文分词组件,提供了多种基于词典的分词算法,并利用ngram模型来消除歧义.能准确识别英文.数字, ...

  2. spider随机请求头和ip

    #创建爬虫 scrapy genspider randomIp_spider "taobao.com" #把需要请求的url放到一个混淆的url请求list中去,避免被监测到总是访 ...

  3. c#中可变参数params关键字学习

    引用 https://www.cnblogs.com/maowp/p/8134342.html 基础知识 1.概念 params 是C#开发语言中关键字, params主要的用处是在给函数传参数的时候 ...

  4. SQL- @@ROWCOUNT -返回上一行执行影响的行行数

    DECLARE @AFFECT INT declare @t table(name nvarchar(20),qy nvarchar(20),je int)insert into @t select ...

  5. Python 上下文管理协议中的__enter__和__exit__基本理解

    所谓上下文管理协议,就是咱们打开文件时常用的一种方法:with __enter__(self):当with开始运行的时候触发此方法的运行 __exit__(self, exc_type, exc_va ...

  6. 搭建rsync服务并同步重要数据

    在主备机器上均安装rsync,在主机上以daemon的模式启动,在备机上定时执行同步命令.安装rsync的命令如下: 1.下载安装包(主备机均执行) [root@localhost home]# wg ...

  7. MQ知识点汇总

    1. MQ是什么 2. MQ能做什么 3. 消息模式 4. 使用MQ的时候需要注意什么 5. 常用MQ 6. MQ的不足 7. 什么时候不适用MQ 8. MQ的组成 9. MQ的关注点 1. MQ是什 ...

  8. 服务器被疑似挖矿程序植入,发现以及解决过程(建议所有使用sonatype/nexus3的用户清查一下)

    此次服务器被植入挖矿程序发现起来较为巧合,首先是上周三开始,我通过sonatype/nexus3搭建的仓库间歇性崩溃,但是每次重新start一下也能直接使用所以没有彻底清查,去docker logs里 ...

  9. 用pdf.js实现在移动端在线预览pdf文件

    用pdf.js实现在移动端在线预览pdf文件1.下载pdf.js    官网地址:https://mozilla.github.io/pdf.js/ 2.配置    下载下来的文件包,就是一个demo ...

  10. mysql_study_1

    创建数据库 创建表 数据类型 插入数据的几种方法 约束 代码 CREATE DATABASE mysql_shiyan; use mysql_shiyan; CREATE TABLE departme ...