题解:Short Task
题意分析
一个显然的性质,\(d(n)\geq n\)。因为 \(n\) 自己一定是自己的因数。因此,若 \(d(n)=c\) 存在,则 \(n\leq c\)。
又发现 \(c\leq10^7\),且多次询问基本相同,因此可以考虑预处理出答案后离线处理。
自己写一下就可以发现,从 \(n\) 找其因数求和并不好做,我太菜了,因此可以考虑从 \(n\) 找其倍数 \(kn\),更新 \(kn\) 的因数和 \(\textit{sumD}_{kn}\)。
枚举 \(n\) 是 \(\mathcal O(N)\) 的,枚举 \(kn\) 是 \(\mathcal O(\log_n N)\) 的,考虑到 \(N=10^7\),可以接受。
最终开一个数组统计答案即可。注意因数和可能会大于 \(N\),这时便不要直接放入数组下标中,否则会越界。
AC 代码
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
constexpr const int N=1e7;
int sumD[N+1],ans[N+1];
void pre(){
for(int i=1;i<=N;i++){
for(int j=i;j<=N;j+=i){
sumD[j]+=i;
}
}
for(int i=1;i<=N;i++){
if(sumD[i]<=N&&!ans[sumD[i]]){
ans[sumD[i]]=i;
}
}
}
int main(){
/*freopen("test.in","r",stdin);
freopen("test.out","w",stdout);*/
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
pre();
int T;
cin>>T;
while(T--){
int c;
cin>>c;
cout<<(ans[c]?ans[c]:-1)<<'\n';
}
cout.flush();
/*fclose(stdin);
fclose(stdout);*/
return 0;
}

浙公网安备 33010602011771号