Non-Prime Factors (质数筛选+因子分解)

博客转载自丿不落良辰,介绍了Non - Prime Factors问题,涉及质数筛选和因子分解。给出示例,如整数100有9个因子,其中2个是质数因子,7个是非质数因子。还说明了输入输出格式及示例,提醒使用快速I/O方法处理大文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自丿不落良辰的博客

https://cn.vjudge.net/problem/Kattis-nonprimefactors

Non-Prime Factors (质数筛选+因子分解)

For example, integer 100has 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).WarningThe I/O files are large. Please use fast I/O methods.

Sample Input 1     

4
100
13
12
2018

Sample Output 1
7
1
4
2

#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
bool vis[2000005];
int ans[2000005];
void init()
{
    //开始筛,vis=1表示该数不是质数
    vis[1]=1;
    int m=sqrt(2000002+0.5);
    for(int i=2; i<=m; ++i)
    {
        if(!vis[i])
        {
            for(int j=i*i; j<=2000002; j+=i)
                vis[j]=1;
        }
    }
    //筛选结束
    for(int i = 1; i <= 2000000; ++i)
    {
        int rt = 2000000/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 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值