题解:AT_abc400_e [ABC400E] Ringo's Favorite Numbers 3

· · 题解

条件等价于 N 是 X^2,且 X 只有两个质因子。

用埃氏筛即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n=1000000,q,f[1000005],g[1000005],h[1000005];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    for(int i=2;i<=n;i++){
        if(!f[i]){
            g[i]++;
            for(int j=2*i;j<=n;j+=i){
                g[j]++;
                f[j]=1;
            }
        }
        h[i]=h[i-1];
        if(g[i]==2){
            h[i]=i;
        }
    }
    cin>>q;
    while(q--){
        int x;
        cin>>x;
        int t=sqrt(x);
        cout<<h[t]*h[t]<<"\n";
    }
    return 0;
}