《B3969 [GESP202403 五级] B-smooth 数》

题目背景

对应的选择、判断题:试题 - GESP 202403 C++ 五级 - 洛谷有题

题目描述

小杨同学想寻找一种名为 B-smooth 数的正整数。

如果一个正整数的最大质因子不超过 B,则该正整数为 B-smooth 数。小杨同学想知道,对于给定的 n 和 B,有多少个不超过 n 的 B-smooth 数。

输入格式

第一行包含两个正整数 n 和 B,含义如题面所示。

输出格式

输出一个非负整数,表示不超过 n 的 B-smooth 数的数量。

输入输出样例

输入 #1复制

复制代码
10 3

输出 #1复制

复制代码
7

说明/提示

样例解释

在不超过 10 的正整数中,3-smooth 数有 1,2,3,4,6,8,9,共 7 个。

数据规模与约定

子任务 得分 n≤ B
1 30 103 1≤B≤103
2 30 106 n​≤B≤106
3 40 106 1≤B≤106

对全部的测试数据,保证 1≤n,B≤106。

代码实现:

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1000000;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,B;
    cin>>n>>B;
    vector<int> minp(n+1,0);
    for(int i=2;i<=n;i++)
    {
        if(minp[i]==0)
        {
            minp[i]=i;
            for(long long j=1LL*i*i;j<=n;j+=i)
                if(minp[j]==0) minp[j]=i;
        }
    }
    int ans=1;
    for(int i=2;i<=n;i++)
    {
        int x=i;
        int maxp=0;
        while(x>1)
        {
            int p=minp[x];
            maxp=p;
            while(x%p==0) x/=p;
        }
        if(maxp<=B) ans++;
    }
    cout<<ans<<'\n';
    return 0;
}