CF1646E 题解

· · 题解

注意到第 d,d^2,d^3,\cdots,d^k(k=\lfloor \log_d n\rfloor ) 行可以一起考虑。

对所有第 d,d^2,d^3,\cdots,d^k 行的数对 d 取对数,不难发现贡献是 1\times 1,1\times 2,\cdots,1\times m,\cdots,k\times 1,k\times 2,\cdots,k\times m 中不同数的个数。

上述贡献可以轻松 O(m\log n) 轻松求出。

总复杂度 O(m\log n+n)。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int NR=1e6+10;
int n,m,ans=1,a[30],vis[NR],now;
bool buc[NR*20]; 
unordered_map<int,int>mp;

signed main(){
    cin>>n>>m;
    for(int i=1;i<=20;i++){
        for(int j=1;j<=m;j++)now+=buc[i*j]^1,buc[i*j]=1;
        a[i]=now;
    }
    for(int i=2;i<=n;i++)
        if(!vis[i]){
            int x=i,cnt=0;
            while(x<=n)cnt++,vis[x]=1,x*=i;
            ans+=a[cnt];
        }
    cout<<ans<<endl;
    return 0;
}