https://ac.nowcoder.com/acm/contest/331/G

C++版本一

std

题解:

#include <bits/stdc++.h>
using namespace std;
 
const int N = 1e6 + 5;
int min_num[N], T, n;
int fact_count[N];
int main() {
    memset(min_num, 0x3f, sizeof(min_num));
    for (int i = 1; i <= 1000000; i++) {
        for (int j = i; j <= 1000000; j += i) {
            fact_count[j]++;
        }
        min_num[fact_count[i]] = min(min_num[fact_count[i]], i);
    }
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        if (min_num[n] == 0x3f3f3f3f)
            puts("-1");
        else
            printf("%d\n", min_num[n]);
    }
    return 0;
}

C++版本二

参考文章:https://blog.csdn.net/weixin_43272781/article/details/86584218

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int M=1000000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
int a[M];
char str;
int D[M];
int pre[M];
bool prime[M];

int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //
    ios::sync_with_stdio(0);
    D[1]=1;
    a[1]=1;
    prime[0]=prime[1]=1;
    for(int i=2;i<M;i++){
        if(!prime[i]){
            D[i]=2;
            pre[++cnt]=i;
        }
        for(int j=1;j<=cnt&&i*pre[j]<M;j++){
            prime[i*pre[j]]=1;
            D[i*pre[j]]=D[i]*2;
            if(i%pre[j]==0){
                int c=1;
                t=i;
                while(t%pre[j]==0){
                    t/=pre[j];
                    c++;
                }
                D[i*pre[j]]=D[t]*(c+1);
                break;
            }
        }
        if(!a[D[i]]&&D[i]<=1000000){
             a[D[i]]=i;
        //cout<<i<<" "<<D[i]<<endl;
        }

    }
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        if(a[n])
            cout << a[n] << endl;
        else
            cout << -1 << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}