简单数论题求约数个数;
本题需要用到质因数分解求约数个数,如果枚举一个一个求约数个数的话,你将会发现你已经喜提超时,见图1测试(图片);
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int f[N];
int n;
int sum;
void solve(int x){
int cnt=0;
for(int i=1;i<=x/i;i++){
if(x%i==0){
cnt++;
if(x/i!=i) cnt++;
}
}
sum+=cnt;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
solve(i);
}
cout<<sum;
return 0;
}
图一;
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int f[N];
int n;
long long sum;
unordered_map<int ,int > mp;
void solve(int x){
int m=x;
for(int i=2;i<=x/i;i++){
while(x%i==0){
mp[i]++;
x/=i;
}
}
if(x>1) mp[x]++;
long long res=1;
for(auto it=mp.begin();it!=mp.end();it++){
res*=(it->second+1);
}
sum+=res;
mp.clear();
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
solve(i);
}
cout<<sum;
return 0;
}
图二;
标签:约数,洛谷,int,p1403,sum,long,mp,res From: https://blog.csdn.net/2303_76815666/article/details/137603284